blob: 6a6ed0e47de1f523d813b815f3c40522c6f9fae1 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000755{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200756 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000757#if defined(MBEDTLS_USE_PSA_CRYPTO)
758 int psa_fallthrough;
759#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 unsigned char keyblk[256];
762 unsigned char *key1;
763 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100764 unsigned char *mac_enc;
765 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000766 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200767 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000768 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000769 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 const mbedtls_cipher_info_t *cipher_info;
771 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100772
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000773 /* cf. RFC 5246, Section 8.1:
774 * "The master secret is always exactly 48 bytes in length." */
775 size_t const master_secret_len = 48;
776
Hanno Becker35b23c72018-10-23 12:10:41 +0100777#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
778 unsigned char session_hash[48];
779#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_ssl_session *session = ssl->session_negotiate;
782 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
783 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
788 transform->encrypt_then_mac = session->encrypt_then_mac;
789#endif
790 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000791
792 ciphersuite_info = handshake->ciphersuite_info;
793 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 if( cipher_info == NULL )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000797 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 }
800
Hanno Beckere694c3e2017-12-27 21:34:08 +0000801 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100802 if( md_info == NULL )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000805 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100807 }
808
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000810 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3)
813 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000814 {
Paul Bakker48916f92012-09-16 19:57:18 +0000815 handshake->tls_prf = ssl3_prf;
816 handshake->calc_verify = ssl_calc_verify_ssl;
817 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000818 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200819 else
820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
822 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000823 {
Paul Bakker48916f92012-09-16 19:57:18 +0000824 handshake->tls_prf = tls1_prf;
825 handshake->calc_verify = ssl_calc_verify_tls;
826 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000827 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828 else
829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
831#if defined(MBEDTLS_SHA512_C)
832 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000833 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000834 {
Paul Bakker48916f92012-09-16 19:57:18 +0000835 handshake->tls_prf = tls_prf_sha384;
836 handshake->calc_verify = ssl_calc_verify_tls_sha384;
837 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA256_C)
842 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843 {
Paul Bakker48916f92012-09-16 19:57:18 +0000844 handshake->tls_prf = tls_prf_sha256;
845 handshake->calc_verify = ssl_calc_verify_tls_sha256;
846 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000847 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200848 else
849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
853 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200854 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000855
856 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 * SSLv3:
858 * master =
859 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
860 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
861 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200862 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200863 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * master = PRF( premaster, "master secret", randbytes )[0..47]
865 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100866 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
869 }
870 else
871 {
872 /* The label for the KDF used for key expansion.
873 * This is either "master secret" or "extended master secret"
874 * depending on whether the Extended Master Secret extension
875 * is used. */
876 char const *lbl = "master secret";
877
878 /* The salt for the KDF used for key expansion.
879 * - If the Extended Master Secret extension is not used,
880 * this is ClientHello.Random + ServerHello.Random
881 * (see Sect. 8.1 in RFC 5246).
882 * - If the Extended Master Secret extension is used,
883 * this is the transcript of the handshake so far.
884 * (see Sect. 4 in RFC 7627). */
885 unsigned char const *salt = handshake->randbytes;
886 size_t salt_len = 64;
887
888#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200892
Hanno Becker35b23c72018-10-23 12:10:41 +0100893 lbl = "extended master secret";
894 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200895 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
897 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000900 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
901 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100902 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000903 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200904 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100905#endif /* MBEDTLS_SHA512_C */
906 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200907 }
908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100910 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200911
Hanno Becker35b23c72018-10-23 12:10:41 +0100912 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200913 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100914#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
915
Hanno Becker7d0a5692018-10-23 15:26:22 +0100916#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
917 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
918 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
919 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
920 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100921 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100922 /* Perform PSK-to-MS expansion in a single step. */
923 psa_status_t status;
924 psa_algorithm_t alg;
925 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500926 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100927
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
929
930 psk = ssl->conf->psk_opaque;
931 if( ssl->handshake->psk_opaque != 0 )
932 psk = ssl->handshake->psk_opaque;
933
Hanno Becker22bf1452019-04-05 11:21:08 +0100934 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
936 else
937 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
938
939 status = psa_key_derivation( &generator, psk, alg,
940 salt, salt_len,
941 (unsigned char const *) lbl,
942 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000943 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 if( status != PSA_SUCCESS )
945 {
946 psa_generator_abort( &generator );
947 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
948 }
949
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000950 status = psa_generator_read( &generator, session->master,
951 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952 if( status != PSA_SUCCESS )
953 {
954 psa_generator_abort( &generator );
955 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
956 }
957
958 status = psa_generator_abort( &generator );
959 if( status != PSA_SUCCESS )
960 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100961 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100962 else
963#endif
964 {
965 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
966 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000967 session->master,
968 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100969 if( ret != 0 )
970 {
971 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
972 return( ret );
973 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200974
Hanno Becker7d0a5692018-10-23 15:26:22 +0100975 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
976 handshake->premaster,
977 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100978
Hanno Becker7d0a5692018-10-23 15:26:22 +0100979 mbedtls_platform_zeroize( handshake->premaster,
980 sizeof(handshake->premaster) );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
984 /*
985 * Swap the client and server random values.
986 */
Paul Bakker48916f92012-09-16 19:57:18 +0000987 memcpy( tmp, handshake->randbytes, 64 );
988 memcpy( handshake->randbytes, tmp + 32, 32 );
989 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500990 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992 /*
993 * SSLv3:
994 * key block =
995 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
996 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
997 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
998 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
999 * ...
1000 *
1001 * TLSv1:
1002 * key block = PRF( master, "key expansion", randbytes )
1003 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001004 ret = handshake->tls_prf( session->master, 48, "key expansion",
1005 handshake->randbytes, 64, keyblk, 256 );
1006 if( ret != 0 )
1007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001009 return( ret );
1010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1013 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1014 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1015 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1016 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 /*
1019 * Determine the appropriate key, IV and MAC length.
1020 */
Paul Bakker68884e32013-01-07 18:20:04 +01001021
Hanno Becker88aaf652017-12-27 08:17:40 +00001022 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023
Hanno Becker8031d062018-01-03 15:32:31 +00001024#if defined(MBEDTLS_GCM_C) || \
1025 defined(MBEDTLS_CCM_C) || \
1026 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001028 cipher_info->mode == MBEDTLS_MODE_CCM ||
1029 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001031 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001032
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001033 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001034 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001035 transform->taglen =
1036 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001038 /* All modes haves 96-bit IVs;
1039 * GCM and CCM has 4 implicit and 8 explicit bytes
1040 * ChachaPoly has all 12 bytes implicit
1041 */
Paul Bakker68884e32013-01-07 18:20:04 +01001042 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001043 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1044 transform->fixed_ivlen = 12;
1045 else
1046 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001047
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001048 /* Minimum length of encrypted record */
1049 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001050 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001051 }
1052 else
Hanno Becker8031d062018-01-03 15:32:31 +00001053#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1054#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1055 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1056 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001057 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001058 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1060 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001063 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001066 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001067 mac_key_len = mbedtls_md_get_size( md_info );
1068 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001071 /*
1072 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1073 * (rfc 6066 page 13 or rfc 2104 section 4),
1074 * so we only need to adjust the length here.
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001079
1080#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1081 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001082 * HMAC implementation which also truncates the key
1083 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001084 mac_key_len = transform->maclen;
1085#endif
1086 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001088
1089 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001090 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001092 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001094 transform->minlen = transform->maclen;
1095 else
Paul Bakker68884e32013-01-07 18:20:04 +01001096 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001097 /*
1098 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001099 * 1. if EtM is in use: one block plus MAC
1100 * otherwise: * first multiple of blocklen greater than maclen
1101 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1104 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001105 {
1106 transform->minlen = transform->maclen
1107 + cipher_info->block_size;
1108 }
1109 else
1110#endif
1111 {
1112 transform->minlen = transform->maclen
1113 + cipher_info->block_size
1114 - transform->maclen % cipher_info->block_size;
1115 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1118 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1119 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001120 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001121 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1124 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1125 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001126 {
1127 transform->minlen += transform->ivlen;
1128 }
1129 else
1130#endif
1131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001133 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1134 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001135 }
Paul Bakker68884e32013-01-07 18:20:04 +01001136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 }
Hanno Becker8031d062018-01-03 15:32:31 +00001138 else
1139#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1140 {
1141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Hanno Becker88aaf652017-12-27 08:17:40 +00001145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1146 (unsigned) keylen,
1147 (unsigned) transform->minlen,
1148 (unsigned) transform->ivlen,
1149 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 /*
1152 * Finally setup the cipher contexts, IVs and MAC secrets.
1153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001155 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001157 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001158 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker68884e32013-01-07 18:20:04 +01001160 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001163 /*
1164 * This is not used in TLS v1.1.
1165 */
Paul Bakker48916f92012-09-16 19:57:18 +00001166 iv_copy_len = ( transform->fixed_ivlen ) ?
1167 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001168 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1169 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
1172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_SSL_CLI_C */
1174#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001177 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001178 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Hanno Becker81c7b182017-11-09 18:39:33 +00001180 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001181 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001182
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001183 /*
1184 * This is not used in TLS v1.1.
1185 */
Paul Bakker48916f92012-09-16 19:57:18 +00001186 iv_copy_len = ( transform->fixed_ivlen ) ?
1187 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001188 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1189 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001196 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1197 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Hanno Beckerd56ed242018-01-03 15:32:51 +00001200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_SSL_PROTO_SSL3)
1202 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001203 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001204 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001207 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1208 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001209 }
1210
Hanno Becker81c7b182017-11-09 18:39:33 +00001211 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1212 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001213 }
1214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1216#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1217 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001219 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001220 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1221 For AEAD-based ciphersuites, there is nothing to do here. */
1222 if( mac_key_len != 0 )
1223 {
1224 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1225 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1226 }
Paul Bakker68884e32013-01-07 18:20:04 +01001227 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001228 else
1229#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001232 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1233 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001234 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001235#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1238 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001239 {
1240 int ret = 0;
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001243
Hanno Becker88aaf652017-12-27 08:17:40 +00001244 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001245 transform->iv_enc, transform->iv_dec,
1246 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001247 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001251 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1252 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001253 }
1254 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001255#else
1256 ((void) mac_dec);
1257 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001259
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001260#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1261 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001262 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001263 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1264 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001265 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001266 iv_copy_len );
1267 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001268
1269 if( ssl->conf->f_export_keys_ext != NULL )
1270 {
1271 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1272 session->master, keyblk,
1273 mac_key_len, transform->keylen,
1274 iv_copy_len, handshake->tls_prf,
1275 handshake->randbytes + 32,
1276 handshake->randbytes );
1277 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001278#endif
1279
Hanno Beckerf704bef2018-11-16 15:21:18 +00001280#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001281
1282 /* Only use PSA-based ciphers for TLS-1.2.
1283 * That's relevant at least for TLS-1.0, where
1284 * we assume that mbedtls_cipher_crypt() updates
1285 * the structure field for the IV, which the PSA-based
1286 * implementation currently doesn't. */
1287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1288 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001289 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001290 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001291 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001292 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1293 {
1294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001295 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001296 }
1297
1298 if( ret == 0 )
1299 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001301 psa_fallthrough = 0;
1302 }
1303 else
1304 {
1305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1306 psa_fallthrough = 1;
1307 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001308 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001309 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001310 psa_fallthrough = 1;
1311#else
1312 psa_fallthrough = 1;
1313#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001314
Hanno Beckercb1cc802018-11-17 22:27:38 +00001315 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001316#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001317 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001321 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001322 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001323
Hanno Beckerf704bef2018-11-16 15:21:18 +00001324#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001325 /* Only use PSA-based ciphers for TLS-1.2.
1326 * That's relevant at least for TLS-1.0, where
1327 * we assume that mbedtls_cipher_crypt() updates
1328 * the structure field for the IV, which the PSA-based
1329 * implementation currently doesn't. */
1330#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1331 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001332 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001333 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001334 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001335 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1336 {
1337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001338 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001339 }
1340
1341 if( ret == 0 )
1342 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001344 psa_fallthrough = 0;
1345 }
1346 else
1347 {
1348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1349 psa_fallthrough = 1;
1350 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001351 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001352 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001353 psa_fallthrough = 1;
1354#else
1355 psa_fallthrough = 1;
1356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001357
Hanno Beckercb1cc802018-11-17 22:27:38 +00001358 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001359#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001360 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001361 cipher_info ) ) != 0 )
1362 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001364 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001365 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001368 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001372 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001373 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001376 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001380 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001381 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_CIPHER_MODE_CBC)
1384 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1387 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001390 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001391 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1394 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001397 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001404 // Initialize compression
1405 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001407 {
Paul Bakker16770332013-10-11 09:59:44 +02001408 if( ssl->compress_buf == NULL )
1409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001411 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001412 if( ssl->compress_buf == NULL )
1413 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001415 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001416 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1417 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001418 }
1419 }
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001422
Paul Bakker48916f92012-09-16 19:57:18 +00001423 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1424 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001425
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001426 if( deflateInit( &transform->ctx_deflate,
1427 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001428 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001431 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1432 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001433 }
1434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001438end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001439 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1440 mbedtls_platform_zeroize( handshake->randbytes,
1441 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001442 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443}
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_SSL_PROTO_SSL3)
1446void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001448 mbedtls_md5_context md5;
1449 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 unsigned char pad_1[48];
1451 unsigned char pad_2[48];
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001455 mbedtls_md5_init( &md5 );
1456 mbedtls_sha1_init( &sha1 );
1457
1458 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1459 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Paul Bakker380da532012-04-18 16:10:25 +00001461 memset( pad_1, 0x36, 48 );
1462 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1465 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1466 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001468 mbedtls_md5_starts_ret( &md5 );
1469 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1470 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1471 mbedtls_md5_update_ret( &md5, hash, 16 );
1472 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001474 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1475 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1476 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001477
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001478 mbedtls_sha1_starts_ret( &sha1 );
1479 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1480 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1481 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1482 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001487 mbedtls_md5_free( &md5 );
1488 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001489
Paul Bakker380da532012-04-18 16:10:25 +00001490 return;
1491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1495void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001496{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001497 mbedtls_md5_context md5;
1498 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001501
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001502 mbedtls_md5_init( &md5 );
1503 mbedtls_sha1_init( &sha1 );
1504
1505 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1506 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001507
Andrzej Kurekeb342242019-01-29 09:14:33 -05001508 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001509 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001513
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001514 mbedtls_md5_free( &md5 );
1515 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001516
Paul Bakker380da532012-04-18 16:10:25 +00001517 return;
1518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1522#if defined(MBEDTLS_SHA256_C)
1523void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001524{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001525#if defined(MBEDTLS_USE_PSA_CRYPTO)
1526 size_t hash_size;
1527 psa_status_t status;
1528 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1529
1530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1531 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1532 if( status != PSA_SUCCESS )
1533 {
1534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1535 return;
1536 }
1537
1538 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1539 if( status != PSA_SUCCESS )
1540 {
1541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1542 return;
1543 }
1544 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1546#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001547 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001548
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001549 mbedtls_sha256_init( &sha256 );
1550
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001552
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001553 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001554 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001558
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001559 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001560#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001561 return;
1562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_SHA512_C)
1566void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001567{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001568#if defined(MBEDTLS_USE_PSA_CRYPTO)
1569 size_t hash_size;
1570 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001571 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001572
1573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001574 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001575 if( status != PSA_SUCCESS )
1576 {
1577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1578 return;
1579 }
1580
Andrzej Kurek972fba52019-01-30 03:29:12 -05001581 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001582 if( status != PSA_SUCCESS )
1583 {
1584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1585 return;
1586 }
1587 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1589#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001590 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001591
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001592 mbedtls_sha512_init( &sha512 );
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001595
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001596 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001597 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001602 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001603#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 return;
1605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_SHA512_C */
1607#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1610int 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 +02001611{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 unsigned char *p = ssl->handshake->premaster;
1613 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001614 const unsigned char *psk = ssl->conf->psk;
1615 size_t psk_len = ssl->conf->psk_len;
1616
1617 /* If the psk callback was called, use its result */
1618 if( ssl->handshake->psk != NULL )
1619 {
1620 psk = ssl->handshake->psk;
1621 psk_len = ssl->handshake->psk_len;
1622 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623
1624 /*
1625 * PMS = struct {
1626 * opaque other_secret<0..2^16-1>;
1627 * opaque psk<0..2^16-1>;
1628 * };
1629 * with "other_secret" depending on the particular key exchange
1630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1632 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001634 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001636
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001637 *(p++) = (unsigned char)( psk_len >> 8 );
1638 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001639
1640 if( end < p || (size_t)( end - p ) < psk_len )
1641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1642
1643 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001644 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 }
1646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1648#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1649 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001650 {
1651 /*
1652 * other_secret already set by the ClientKeyExchange message,
1653 * and is 48 bytes long
1654 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001655 if( end - p < 2 )
1656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1657
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001658 *p++ = 0;
1659 *p++ = 48;
1660 p += 48;
1661 }
1662 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1664#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1665 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001666 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001667 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001668 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001669
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001670 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001672 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001673 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001676 return( ret );
1677 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001678 *(p++) = (unsigned char)( len >> 8 );
1679 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001680 p += len;
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001683 }
1684 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1686#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1687 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001688 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001689 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001690 size_t zlen;
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001693 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001694 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001697 return( ret );
1698 }
1699
1700 *(p++) = (unsigned char)( zlen >> 8 );
1701 *(p++) = (unsigned char)( zlen );
1702 p += zlen;
1703
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001704 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1705 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001706 }
1707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001712 }
1713
1714 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001715 if( end - p < 2 )
1716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001717
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001718 *(p++) = (unsigned char)( psk_len >> 8 );
1719 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001720
1721 if( end < p || (size_t)( end - p ) < psk_len )
1722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1723
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001724 memcpy( p, psk, psk_len );
1725 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001726
1727 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1728
1729 return( 0 );
1730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001734/*
1735 * SSLv3.0 MAC functions
1736 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001737#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001738static void ssl_mac( mbedtls_md_context_t *md_ctx,
1739 const unsigned char *secret,
1740 const unsigned char *buf, size_t len,
1741 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001742 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743{
1744 unsigned char header[11];
1745 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001746 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1748 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001749
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001750 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001752 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001753 else
Paul Bakker68884e32013-01-07 18:20:04 +01001754 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756 memcpy( header, ctr, 8 );
1757 header[ 8] = (unsigned char) type;
1758 header[ 9] = (unsigned char)( len >> 8 );
1759 header[10] = (unsigned char)( len );
1760
Paul Bakker68884e32013-01-07 18:20:04 +01001761 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_md_starts( md_ctx );
1763 mbedtls_md_update( md_ctx, secret, md_size );
1764 mbedtls_md_update( md_ctx, padding, padlen );
1765 mbedtls_md_update( md_ctx, header, 11 );
1766 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001767 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Paul Bakker68884e32013-01-07 18:20:04 +01001769 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_md_starts( md_ctx );
1771 mbedtls_md_update( md_ctx, secret, md_size );
1772 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001773 mbedtls_md_update( md_ctx, out, md_size );
1774 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001777
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001778/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001779 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001780#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001781 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1782 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1783 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1784/* This function makes sure every byte in the memory region is accessed
1785 * (in ascending addresses order) */
1786static void ssl_read_memory( unsigned char *p, size_t len )
1787{
1788 unsigned char acc = 0;
1789 volatile unsigned char force;
1790
1791 for( ; len != 0; p++, len-- )
1792 acc ^= *p;
1793
1794 force = acc;
1795 (void) force;
1796}
1797#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1798
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001799/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001801 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001802
1803static void ssl_extract_add_data_from_record( unsigned char* add_data,
1804 mbedtls_record *rec )
1805{
1806 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1807 add_data[8] = rec->type;
1808 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1809 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1810 add_data[12] = rec->data_len & 0xFF;
1811}
1812
Hanno Beckera18d1322018-01-03 14:27:32 +00001813int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1814 mbedtls_ssl_transform *transform,
1815 mbedtls_record *rec,
1816 int (*f_rng)(void *, unsigned char *, size_t),
1817 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001820 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001821 unsigned char * data;
1822 unsigned char add_data[13];
1823 size_t post_avail;
1824
1825 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001826#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001827 ((void) ssl);
1828#endif
1829
1830 /* The PRNG is used for dynamic IV generation that's used
1831 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1832#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1833 ( defined(MBEDTLS_AES_C) || \
1834 defined(MBEDTLS_ARIA_C) || \
1835 defined(MBEDTLS_CAMELLIA_C) ) && \
1836 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1837 ((void) f_rng);
1838 ((void) p_rng);
1839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001843 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001844 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1847 }
1848 if( rec == NULL ||
1849 rec->buf == NULL ||
1850 rec->buf_len < rec->data_offset ||
1851 rec->buf_len - rec->data_offset < rec->data_len )
1852 {
1853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001855 }
1856
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001857 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1858 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001860 data, rec->data_len );
1861
1862 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1863
1864 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1865 {
1866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1867 (unsigned) rec->data_len,
1868 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1870 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001871
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001873 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 */
Hanno Becker52344c22018-01-03 15:24:20 +00001875#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 if( mode == MBEDTLS_MODE_STREAM ||
1877 ( mode == MBEDTLS_MODE_CBC
1878#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001879 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880#endif
1881 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001883 if( post_avail < transform->maclen )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1887 }
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001890 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001891 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001892 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001893 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1894 data, rec->data_len, rec->ctr, rec->type, mac );
1895 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001896 }
1897 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1900 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001901 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001902 {
Hanno Becker992b6872017-11-09 18:57:39 +00001903 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1904
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001905 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001906
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001907 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1908 sizeof( add_data ) );
1909 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1910 data, rec->data_len );
1911 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1912 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1913
1914 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001915 }
1916 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001917#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001921 }
1922
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001923 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1924 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001925
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001926 rec->data_len += transform->maclen;
1927 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001928 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001929 }
Hanno Becker52344c22018-01-03 15:24:20 +00001930#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001932 /*
1933 * Encrypt
1934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1936 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001938 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001939 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001941 "including %d bytes of padding",
1942 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001944 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1945 transform->iv_enc, transform->ivlen,
1946 data, rec->data_len,
1947 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001950 return( ret );
1951 }
1952
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001953 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001957 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
Paul Bakker68884e32013-01-07 18:20:04 +01001959 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001961
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001962#if defined(MBEDTLS_GCM_C) || \
1963 defined(MBEDTLS_CCM_C) || \
1964 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001966 mode == MBEDTLS_MODE_CCM ||
1967 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001968 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001969 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001970 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001971 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001972
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001973 /* Check that there's space for both the authentication tag
1974 * and the explicit IV before and after the record content. */
1975 if( post_avail < transform->taglen ||
1976 rec->data_offset < explicit_iv_len )
1977 {
1978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1979 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1980 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001981
Paul Bakker68884e32013-01-07 18:20:04 +01001982 /*
1983 * Generate IV
1984 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001985 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1986 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001987 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001988 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001989 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1990 explicit_iv_len );
1991 /* Prefix record content with explicit IV. */
1992 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001993 }
1994 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1995 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001996 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001997 unsigned char i;
1998
1999 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2000
2001 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002002 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002003 }
2004 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002005 {
2006 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002009 }
2010
Hanno Becker1f10d762019-04-26 13:34:37 +01002011 ssl_extract_add_data_from_record( add_data, rec );
2012
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002013 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2014 iv, transform->ivlen );
2015 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002016 data - explicit_iv_len, explicit_iv_len );
2017 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2018 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002021 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022
Paul Bakker68884e32013-01-07 18:20:04 +01002023 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002024 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002025 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002026
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002027 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002028 iv, transform->ivlen,
2029 add_data, 13, /* add data */
2030 data, rec->data_len, /* source */
2031 data, &rec->data_len, /* destination */
2032 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002035 return( ret );
2036 }
2037
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002038 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2039 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002040
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002041 rec->data_len += transform->taglen + explicit_iv_len;
2042 rec->data_offset -= explicit_iv_len;
2043 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002044 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2048#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002049 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002052 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053 size_t padlen, i;
2054 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002055
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002056 /* Currently we're always using minimal padding
2057 * (up to 255 bytes would be allowed). */
2058 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2059 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 padlen = 0;
2061
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002062 /* Check there's enough space in the buffer for the padding. */
2063 if( post_avail < padlen + 1 )
2064 {
2065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2066 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2067 }
2068
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 rec->data_len += padlen + 1;
2073 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002076 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002077 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2078 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002079 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002080 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002081 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002082 if( f_rng == NULL )
2083 {
2084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2086 }
2087
2088 if( rec->data_offset < transform->ivlen )
2089 {
2090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2091 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2092 }
2093
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002094 /*
2095 * Generate IV
2096 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002097 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002098 if( ret != 0 )
2099 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002100
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002101 memcpy( data - transform->ivlen, transform->iv_enc,
2102 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002103
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002108 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002110 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002112 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2113 transform->iv_enc,
2114 transform->ivlen,
2115 data, rec->data_len,
2116 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002119 return( ret );
2120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002121
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002126 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002129 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002130 {
2131 /*
2132 * Save IV in SSL3 and TLS1
2133 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2135 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002137 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002138#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 {
2140 data -= transform->ivlen;
2141 rec->data_offset -= transform->ivlen;
2142 rec->data_len += transform->ivlen;
2143 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002146 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002148 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2149
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002150 /*
2151 * MAC(MAC_write_key, seq_num +
2152 * TLSCipherText.type +
2153 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002154 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002155 * IV + // except for TLS 1.0
2156 * ENC(content + padding + padding_length));
2157 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158
2159 if( post_avail < transform->maclen)
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2162 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2163 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002164
Hanno Becker1f10d762019-04-26 13:34:37 +01002165 ssl_extract_add_data_from_record( add_data, rec );
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002168 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2169 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002170
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002171 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2172 sizeof( add_data ) );
2173 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2174 data, rec->data_len );
2175 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2176 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002177
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002178 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002179
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002180 rec->data_len += transform->maclen;
2181 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002182 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002188 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002194 /* Make extra sure authentication was performed, exactly once */
2195 if( auth_done != 1 )
2196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002199 }
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
2203 return( 0 );
2204}
2205
Hanno Beckera18d1322018-01-03 14:27:32 +00002206int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2207 mbedtls_ssl_transform *transform,
2208 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002210 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002212 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002213#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002214 size_t padlen = 0, correct = 1;
2215#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002216 unsigned char* data;
2217 unsigned char add_data[13];
2218
Hanno Beckera18d1322018-01-03 14:27:32 +00002219#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002220 ((void) ssl);
2221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002224 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2228 }
2229 if( rec == NULL ||
2230 rec->buf == NULL ||
2231 rec->buf_len < rec->data_offset ||
2232 rec->buf_len - rec->data_offset < rec->data_len )
2233 {
2234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002236 }
2237
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002238 data = rec->buf + rec->data_offset;
2239 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2242 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002243 {
2244 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002245 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2246 transform->iv_dec,
2247 transform->ivlen,
2248 data, rec->data_len,
2249 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002252 return( ret );
2253 }
2254
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002255 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
Paul Bakker68884e32013-01-07 18:20:04 +01002261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002263#if defined(MBEDTLS_GCM_C) || \
2264 defined(MBEDTLS_CCM_C) || \
2265 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002267 mode == MBEDTLS_MODE_CCM ||
2268 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002269 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002270 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002271 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002272
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002273 /*
2274 * Compute and update sizes
2275 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002276 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002279 "+ taglen (%d)", rec->data_len,
2280 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002282 }
Paul Bakker68884e32013-01-07 18:20:04 +01002283
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002284 /*
2285 * Prepare IV
2286 */
2287 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2288 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002289 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002290 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002291 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002292
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002293 }
2294 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2295 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002296 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002297 unsigned char i;
2298
2299 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2300
2301 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002302 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002303 }
2304 else
2305 {
2306 /* Reminder if we ever add an AEAD mode with a different size */
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2309 }
2310
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002311 data += explicit_iv_len;
2312 rec->data_offset += explicit_iv_len;
2313 rec->data_len -= explicit_iv_len + transform->taglen;
2314
2315 ssl_extract_add_data_from_record( add_data, rec );
2316 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2317 add_data, 13 );
2318
2319 memcpy( transform->iv_dec + transform->fixed_ivlen,
2320 data - explicit_iv_len, explicit_iv_len );
2321
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002323 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002324 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002325
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002326
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002327 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002328 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002329 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002330 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2331 iv, transform->ivlen,
2332 add_data, 13,
2333 data, rec->data_len,
2334 data, &olen,
2335 data + rec->data_len,
2336 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2341 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002342
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002343 return( ret );
2344 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002345 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002346
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002347 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002351 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2355#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002356 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002359 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 /*
Paul Bakker45829992013-01-03 14:52:21 +01002362 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002365 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2366 {
2367 /* The ciphertext is prefixed with the CBC IV. */
2368 minlen += transform->ivlen;
2369 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002370#endif
Paul Bakker45829992013-01-03 14:52:21 +01002371
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002372 /* Size considerations:
2373 *
2374 * - The CBC cipher text must not be empty and hence
2375 * at least of size transform->ivlen.
2376 *
2377 * Together with the potential IV-prefix, this explains
2378 * the first of the two checks below.
2379 *
2380 * - The record must contain a MAC, either in plain or
2381 * encrypted, depending on whether Encrypt-then-MAC
2382 * is used or not.
2383 * - If it is, the message contains the IV-prefix,
2384 * the CBC ciphertext, and the MAC.
2385 * - If it is not, the padded plaintext, and hence
2386 * the CBC ciphertext, has at least length maclen + 1
2387 * because there is at least the padding length byte.
2388 *
2389 * As the CBC ciphertext is not empty, both cases give the
2390 * lower bound minlen + maclen + 1 on the record size, which
2391 * we test for in the second check below.
2392 */
2393 if( rec->data_len < minlen + transform->ivlen ||
2394 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002397 "+ 1 ) ( + expl IV )", rec->data_len,
2398 transform->ivlen,
2399 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002401 }
2402
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002403 /*
2404 * Authenticate before decrypt if enabled
2405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002407 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002408 {
Hanno Becker992b6872017-11-09 18:57:39 +00002409 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002412
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002413 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2414 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002415
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002416 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002417
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002418 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2419 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2420 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2421 data, rec->data_len );
2422 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2423 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002424
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002425 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2426 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002427 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002428 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002429
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002430 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2431 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002435 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002436 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002439
2440 /*
2441 * Check length sanity
2442 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002443 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002446 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002448 }
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002451 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002452 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002453 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002454 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002455 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002456 /* This is safe because data_len >= minlen + maclen + 1 initially,
2457 * and at this point we have at most subtracted maclen (note that
2458 * minlen == transform->ivlen here). */
2459 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002460
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002461 data += transform->ivlen;
2462 rec->data_offset += transform->ivlen;
2463 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002466
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002467 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2468 transform->iv_dec, transform->ivlen,
2469 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002472 return( ret );
2473 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002474
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002475 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002479 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002482 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002483 {
2484 /*
2485 * Save IV in SSL3 and TLS1
2486 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002487 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2488 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002492 /* Safe since data_len >= minlen + maclen + 1, so after having
2493 * subtracted at most minlen and maclen up to this point,
2494 * data_len > 0. */
2495 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002496
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002497 if( auth_done == 1 )
2498 {
2499 correct *= ( rec->data_len >= padlen + 1 );
2500 padlen *= ( rec->data_len >= padlen + 1 );
2501 }
2502 else
Paul Bakker45829992013-01-03 14:52:21 +01002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002505 if( rec->data_len < transform->maclen + padlen + 1 )
2506 {
2507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2508 rec->data_len,
2509 transform->maclen,
2510 padlen + 1 ) );
2511 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002512#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002513
2514 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2515 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002518 padlen++;
2519
2520 /* Regardless of the validity of the padding,
2521 * we have data_len >= padlen here. */
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002524 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002526 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#if defined(MBEDTLS_SSL_DEBUG_ALL)
2529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002530 "should be no more than %d",
2531 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002532#endif
Paul Bakker45829992013-01-03 14:52:21 +01002533 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535 }
2536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2538#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2539 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002540 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 /* The padding check involves a series of up to 256
2543 * consecutive memory reads at the end of the record
2544 * plaintext buffer. In order to hide the length and
2545 * validity of the padding, always perform exactly
2546 * `min(256,plaintext_len)` reads (but take into account
2547 * only the last `padlen` bytes for the padding check). */
2548 size_t pad_count = 0;
2549 size_t real_count = 0;
2550 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002551
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 /* Index of first padding byte; it has been ensured above
2553 * that the subtraction is safe. */
2554 size_t const padding_idx = rec->data_len - padlen;
2555 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2556 size_t const start_idx = rec->data_len - num_checks;
2557 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002558
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002560 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 real_count |= ( idx >= padding_idx );
2562 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002563 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002567 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002569#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002570 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002572 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2574 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002578 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002579
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002580 /* If the padding was found to be invalid, padlen == 0
2581 * and the subtraction is safe. If the padding was found valid,
2582 * padlen hasn't been changed and the previous assertion
2583 * data_len >= padlen still holds. */
2584 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002588 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002594#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002596 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
2599 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002600 * Authenticate if not done yet.
2601 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 */
Hanno Becker52344c22018-01-03 15:24:20 +00002603#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002604 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002605 {
Hanno Becker992b6872017-11-09 18:57:39 +00002606 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002607
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002608 /* If the initial value of padlen was such that
2609 * data_len < maclen + padlen + 1, then padlen
2610 * got reset to 1, and the initial check
2611 * data_len >= minlen + maclen + 1
2612 * guarantees that at this point we still
2613 * have at least data_len >= maclen.
2614 *
2615 * If the initial value of padlen was such that
2616 * data_len >= maclen + padlen + 1, then we have
2617 * subtracted either padlen + 1 (if the padding was correct)
2618 * or 0 (if the padding was incorrect) since then,
2619 * hence data_len >= maclen in any case.
2620 */
2621 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002623 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002626 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002627 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 ssl_mac( &transform->md_ctx_dec,
2629 transform->mac_dec,
2630 data, rec->data_len,
2631 rec->ctr, rec->type,
2632 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002633 }
2634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2636#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2637 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 {
2640 /*
2641 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002642 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002643 *
2644 * Known timing attacks:
2645 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2646 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002647 * To compensate for different timings for the MAC calculation
2648 * depending on how much padding was removed (which is determined
2649 * by padlen), process extra_run more blocks through the hash
2650 * function.
2651 *
2652 * The formula in the paper is
2653 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2654 * where L1 is the size of the header plus the decrypted message
2655 * plus CBC padding and L2 is the size of the header plus the
2656 * decrypted message. This is for an underlying hash function
2657 * with 64-byte blocks.
2658 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2659 * correctly. We round down instead of up, so -56 is the correct
2660 * value for our calculations instead of -55.
2661 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002662 * Repeat the formula rather than defining a block_size variable.
2663 * This avoids requiring division by a variable at runtime
2664 * (which would be marginally less efficient and would require
2665 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002666 */
2667 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002669
2670 /*
2671 * The next two sizes are the minimum and maximum values of
2672 * in_msglen over all padlen values.
2673 *
2674 * They're independent of padlen, since we previously did
2675 * in_msglen -= padlen.
2676 *
2677 * Note that max_len + maclen is never more than the buffer
2678 * length, as we previously did in_msglen -= maclen too.
2679 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002681 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 memset( tmp, 0, sizeof( tmp ) );
2684
2685 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002686 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002687#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2688 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002689 case MBEDTLS_MD_MD5:
2690 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002691 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002692 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002693 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2694 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002695 break;
2696#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002697#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002698 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002699 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002700 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2701 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002702 break;
2703#endif
2704 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2707 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002708
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002710
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002711 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2712 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2713 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002714 /* Make sure we access everything even when padlen > 0. This
2715 * makes the synchronisation requirements for just-in-time
2716 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002717 ssl_read_memory( data + rec->data_len, padlen );
2718 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002719
2720 /* Call mbedtls_md_process at least once due to cache attacks
2721 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002722 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002723 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002724
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002725 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002726
2727 /* Make sure we access all the memory that could contain the MAC,
2728 * before we check it in the next code block. This makes the
2729 * synchronisation requirements for just-in-time Prime+Probe
2730 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002731 ssl_read_memory( data + min_len,
2732 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002733 }
2734 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2736 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002742#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2744 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002747 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2748 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#if defined(MBEDTLS_SSL_DEBUG_ALL)
2751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002752#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002753 correct = 0;
2754 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002755 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002756 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002757
2758 /*
2759 * Finally check the correct flag
2760 */
2761 if( correct == 0 )
2762 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002763#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002764
2765 /* Make extra sure authentication was performed, exactly once */
2766 if( auth_done != 1 )
2767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
2774 return( 0 );
2775}
2776
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002777#undef MAC_NONE
2778#undef MAC_PLAINTEXT
2779#undef MAC_CIPHERTEXT
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002782/*
2783 * Compression/decompression functions
2784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002786{
2787 int ret;
2788 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002789 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002790 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002791 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002794
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002795 if( len_pre == 0 )
2796 return( 0 );
2797
Paul Bakker2770fbd2012-07-03 13:30:23 +00002798 memcpy( msg_pre, ssl->out_msg, len_pre );
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002801 ssl->out_msglen ) );
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804 ssl->out_msg, ssl->out_msglen );
2805
Paul Bakker48916f92012-09-16 19:57:18 +00002806 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2807 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2808 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002809 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002810
Paul Bakker48916f92012-09-16 19:57:18 +00002811 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002812 if( ret != Z_OK )
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2815 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002816 }
2817
Angus Grattond8213d02016-05-25 20:56:48 +10002818 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002819 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002822 ssl->out_msglen ) );
2823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002825 ssl->out_msg, ssl->out_msglen );
2826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002828
2829 return( 0 );
2830}
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002833{
2834 int ret;
2835 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002836 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002837 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002838 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002841
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002842 if( len_pre == 0 )
2843 return( 0 );
2844
Paul Bakker2770fbd2012-07-03 13:30:23 +00002845 memcpy( msg_pre, ssl->in_msg, len_pre );
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002848 ssl->in_msglen ) );
2849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002851 ssl->in_msg, ssl->in_msglen );
2852
Paul Bakker48916f92012-09-16 19:57:18 +00002853 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2854 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2855 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002856 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002857 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002858
Paul Bakker48916f92012-09-16 19:57:18 +00002859 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860 if( ret != Z_OK )
2861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2863 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864 }
2865
Angus Grattond8213d02016-05-25 20:56:48 +10002866 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002867 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870 ssl->in_msglen ) );
2871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 ssl->in_msg, ssl->in_msglen );
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876
2877 return( 0 );
2878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2882static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_SSL_PROTO_DTLS)
2885static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002886{
2887 /* If renegotiation is not enforced, retransmit until we would reach max
2888 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002889 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002890 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002891 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002892 unsigned char doublings = 1;
2893
2894 while( ratio != 0 )
2895 {
2896 ++doublings;
2897 ratio >>= 1;
2898 }
2899
2900 if( ++ssl->renego_records_seen > doublings )
2901 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002903 return( 0 );
2904 }
2905 }
2906
2907 return( ssl_write_hello_request( ssl ) );
2908}
2909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002911
Paul Bakker5121ce52009-01-03 21:22:43 +00002912/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002913 * Fill the input message buffer by appending data to it.
2914 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002915 *
2916 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2917 * available (from this read and/or a previous one). Otherwise, an error code
2918 * is returned (possibly EOF or WANT_READ).
2919 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002920 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2921 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2922 * since we always read a whole datagram at once.
2923 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002924 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002925 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002928{
Paul Bakker23986e52011-04-24 08:57:21 +00002929 int ret;
2930 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002934 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002937 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002939 }
2940
Angus Grattond8213d02016-05-25 20:56:48 +10002941 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002945 }
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002950 uint32_t timeout;
2951
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002952 /* Just to be sure */
2953 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2954 {
2955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2956 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2958 }
2959
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002960 /*
2961 * The point is, we need to always read a full datagram at once, so we
2962 * sometimes read more then requested, and handle the additional data.
2963 * It could be the rest of the current record (while fetching the
2964 * header) and/or some other records in the same datagram.
2965 */
2966
2967 /*
2968 * Move to the next record in the already read datagram if applicable
2969 */
2970 if( ssl->next_record_offset != 0 )
2971 {
2972 if( ssl->in_left < ssl->next_record_offset )
2973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002976 }
2977
2978 ssl->in_left -= ssl->next_record_offset;
2979
2980 if( ssl->in_left != 0 )
2981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002983 ssl->next_record_offset ) );
2984 memmove( ssl->in_hdr,
2985 ssl->in_hdr + ssl->next_record_offset,
2986 ssl->in_left );
2987 }
2988
2989 ssl->next_record_offset = 0;
2990 }
2991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002994
2995 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002996 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002997 */
2998 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003001 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003002 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003003
3004 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003005 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003006 * are not at the beginning of a new record, the caller did something
3007 * wrong.
3008 */
3009 if( ssl->in_left != 0 )
3010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003013 }
3014
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003015 /*
3016 * Don't even try to read if time's out already.
3017 * This avoids by-passing the timer when repeatedly receiving messages
3018 * that will end up being dropped.
3019 */
3020 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003021 {
3022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003023 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003024 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003025 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003026 {
Angus Grattond8213d02016-05-25 20:56:48 +10003027 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003030 timeout = ssl->handshake->retransmit_timeout;
3031 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003032 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003035
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003036 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003037 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3038 timeout );
3039 else
3040 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003043
3044 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003046 }
3047
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003048 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003051 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003054 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003055 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003058 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003059 }
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003064 return( ret );
3065 }
3066
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003067 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003070 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003072 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003073 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003076 return( ret );
3077 }
3078
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003079 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003082 }
3083
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 if( ret < 0 )
3085 return( ret );
3086
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003087 ssl->in_left = ret;
3088 }
3089 else
3090#endif
3091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003093 ssl->in_left, nb_want ) );
3094
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 while( ssl->in_left < nb_want )
3096 {
3097 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003098
3099 if( ssl_check_timer( ssl ) != 0 )
3100 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3101 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003102 {
3103 if( ssl->f_recv_timeout != NULL )
3104 {
3105 ret = ssl->f_recv_timeout( ssl->p_bio,
3106 ssl->in_hdr + ssl->in_left, len,
3107 ssl->conf->read_timeout );
3108 }
3109 else
3110 {
3111 ret = ssl->f_recv( ssl->p_bio,
3112 ssl->in_hdr + ssl->in_left, len );
3113 }
3114 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003117 ssl->in_left, nb_want ) );
3118 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003119
3120 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003122
3123 if( ret < 0 )
3124 return( ret );
3125
mohammad160352aecb92018-03-28 23:41:40 -07003126 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003127 {
Darryl Green11999bb2018-03-13 15:22:58 +00003128 MBEDTLS_SSL_DEBUG_MSG( 1,
3129 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003130 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3132 }
3133
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003134 ssl->in_left += ret;
3135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 }
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
3140 return( 0 );
3141}
3142
3143/*
3144 * Flush any data not yet written
3145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003147{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003148 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003149 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003153 if( ssl->f_send == NULL )
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003156 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003158 }
3159
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003160 /* Avoid incrementing counter if data is flushed */
3161 if( ssl->out_left == 0 )
3162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003164 return( 0 );
3165 }
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 while( ssl->out_left > 0 )
3168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3170 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171
Hanno Becker2b1e3542018-08-06 11:19:13 +01003172 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003173 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
3177 if( ret <= 0 )
3178 return( ret );
3179
mohammad160352aecb92018-03-28 23:41:40 -07003180 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003181 {
Darryl Green11999bb2018-03-13 15:22:58 +00003182 MBEDTLS_SSL_DEBUG_MSG( 1,
3183 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003184 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3186 }
3187
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 ssl->out_left -= ret;
3189 }
3190
Hanno Becker2b1e3542018-08-06 11:19:13 +01003191#if defined(MBEDTLS_SSL_PROTO_DTLS)
3192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003193 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003194 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003195 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003196 else
3197#endif
3198 {
3199 ssl->out_hdr = ssl->out_buf + 8;
3200 }
3201 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
3205 return( 0 );
3206}
3207
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003208/*
3209 * Functions to handle the DTLS retransmission state machine
3210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003212/*
3213 * Append current handshake message to current outgoing flight
3214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3219 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3220 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003221
3222 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003223 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003224 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003228 }
3229
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003230 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003231 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003235 }
3236
3237 /* Copy current handshake message with headers */
3238 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3239 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003240 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003241 msg->next = NULL;
3242
3243 /* Append to the current flight */
3244 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003245 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003246 else
3247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003249 while( cur->next != NULL )
3250 cur = cur->next;
3251 cur->next = msg;
3252 }
3253
Hanno Becker3b235902018-08-06 09:54:53 +01003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003255 return( 0 );
3256}
3257
3258/*
3259 * Free the current flight of handshake messages
3260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 mbedtls_ssl_flight_item *cur = flight;
3264 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003265
3266 while( cur != NULL )
3267 {
3268 next = cur->next;
3269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 mbedtls_free( cur->p );
3271 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003272
3273 cur = next;
3274 }
3275}
3276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3278static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003279#endif
3280
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003281/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003282 * Swap transform_out and out_ctr with the alternative ones
3283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003287 unsigned char tmp_out_ctr[8];
3288
3289 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003292 return;
3293 }
3294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003296
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003297 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003298 tmp_transform = ssl->transform_out;
3299 ssl->transform_out = ssl->handshake->alt_transform_out;
3300 ssl->handshake->alt_transform_out = tmp_transform;
3301
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003302 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003303 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3304 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003305 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003306
3307 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003308 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3311 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3316 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003317 }
3318 }
3319#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003320}
3321
3322/*
3323 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003324 */
3325int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3326{
3327 int ret = 0;
3328
3329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3330
3331 ret = mbedtls_ssl_flight_transmit( ssl );
3332
3333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3334
3335 return( ret );
3336}
3337
3338/*
3339 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340 *
3341 * Need to remember the current message in case flush_output returns
3342 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003345int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003347 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003351 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003353
3354 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003355 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003356 ssl_swap_epochs( ssl );
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003359 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003360
3361 while( ssl->handshake->cur_msg != NULL )
3362 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003363 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003364 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003365
Hanno Beckere1dcb032018-08-17 16:47:58 +01003366 int const is_finished =
3367 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3368 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3369
Hanno Becker04da1892018-08-14 13:22:10 +01003370 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3371 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3372
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003373 /* Swap epochs before sending Finished: we can't do it after
3374 * sending ChangeCipherSpec, in case write returns WANT_READ.
3375 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003376 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003377 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003379 ssl_swap_epochs( ssl );
3380 }
3381
Hanno Becker67bc7c32018-08-06 11:33:50 +01003382 ret = ssl_get_remaining_payload_in_datagram( ssl );
3383 if( ret < 0 )
3384 return( ret );
3385 max_frag_len = (size_t) ret;
3386
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003387 /* CCS is copied as is, while HS messages may need fragmentation */
3388 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3389 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003390 if( max_frag_len == 0 )
3391 {
3392 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3393 return( ret );
3394
3395 continue;
3396 }
3397
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003398 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003399 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003400 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003401
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003402 /* Update position inside current message */
3403 ssl->handshake->cur_msg_p += cur->len;
3404 }
3405 else
3406 {
3407 const unsigned char * const p = ssl->handshake->cur_msg_p;
3408 const size_t hs_len = cur->len - 12;
3409 const size_t frag_off = p - ( cur->p + 12 );
3410 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003411 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412
Hanno Beckere1dcb032018-08-17 16:47:58 +01003413 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003414 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003415 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003417
Hanno Becker67bc7c32018-08-06 11:33:50 +01003418 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3419 return( ret );
3420
3421 continue;
3422 }
3423 max_hs_frag_len = max_frag_len - 12;
3424
3425 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3426 max_hs_frag_len : rem_len;
3427
3428 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003429 {
3430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003431 (unsigned) cur_hs_frag_len,
3432 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003433 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003434
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003435 /* Messages are stored with handshake headers as if not fragmented,
3436 * copy beginning of headers then fill fragmentation fields.
3437 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3438 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003439
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003440 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3441 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3442 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3443
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3445 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3446 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003447
3448 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3449
Hanno Becker3f7b9732018-08-28 09:53:25 +01003450 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003451 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3452 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003453 ssl->out_msgtype = cur->type;
3454
3455 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003457 }
3458
3459 /* If done with the current message move to the next one if any */
3460 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3461 {
3462 if( cur->next != NULL )
3463 {
3464 ssl->handshake->cur_msg = cur->next;
3465 ssl->handshake->cur_msg_p = cur->next->p + 12;
3466 }
3467 else
3468 {
3469 ssl->handshake->cur_msg = NULL;
3470 ssl->handshake->cur_msg_p = NULL;
3471 }
3472 }
3473
3474 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003475 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003478 return( ret );
3479 }
3480 }
3481
Hanno Becker67bc7c32018-08-06 11:33:50 +01003482 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3483 return( ret );
3484
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003485 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3487 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003488 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003491 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3492 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003493
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003495
3496 return( 0 );
3497}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003498
3499/*
3500 * To be called when the last message of an incoming flight is received.
3501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003503{
3504 /* We won't need to resend that one any more */
3505 ssl_flight_free( ssl->handshake->flight );
3506 ssl->handshake->flight = NULL;
3507 ssl->handshake->cur_msg = NULL;
3508
3509 /* The next incoming flight will start with this msg_seq */
3510 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3511
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003512 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003513 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003514
Hanno Becker0271f962018-08-16 13:23:47 +01003515 /* Clear future message buffering structure. */
3516 ssl_buffering_free( ssl );
3517
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003518 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003519 ssl_set_timer( ssl, 0 );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3522 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003525 }
3526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003528}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003529
3530/*
3531 * To be called when the last message of an outgoing flight is send.
3532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003534{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003535 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003536 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3539 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003542 }
3543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003547
Paul Bakker5121ce52009-01-03 21:22:43 +00003548/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003549 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003551
3552/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003553 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003554 *
3555 * - fill in handshake headers
3556 * - update handshake checksum
3557 * - DTLS: save message for resending
3558 * - then pass to the record layer
3559 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003560 * DTLS: except for HelloRequest, messages are only queued, and will only be
3561 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003562 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003563 * Inputs:
3564 * - ssl->out_msglen: 4 + actual handshake message len
3565 * (4 is the size of handshake headers for TLS)
3566 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3567 * - ssl->out_msg + 4: the handshake message body
3568 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003569 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003570 * - ssl->out_msglen: the length of the record contents
3571 * (including handshake headers but excluding record headers)
3572 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003574int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003575{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003576 int ret;
3577 const size_t hs_len = ssl->out_msglen - 4;
3578 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3581
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003582 /*
3583 * Sanity checks
3584 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003585 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003586 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3587 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003588 /* In SSLv3, the client might send a NoCertificate alert. */
3589#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3590 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3591 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3592 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3593#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3594 {
3595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3597 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003600 /* Whenever we send anything different from a
3601 * HelloRequest we should be in a handshake - double check. */
3602 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3603 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003604 ssl->handshake == NULL )
3605 {
3606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003611 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003617 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003619
Hanno Beckerb50a2532018-08-06 11:52:54 +01003620 /* Double-check that we did not exceed the bounds
3621 * of the outgoing record buffer.
3622 * This should never fail as the various message
3623 * writing functions must obey the bounds of the
3624 * outgoing record buffer, but better be safe.
3625 *
3626 * Note: We deliberately do not check for the MTU or MFL here.
3627 */
3628 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3629 {
3630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3631 "size %u, maximum %u",
3632 (unsigned) ssl->out_msglen,
3633 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3635 }
3636
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003637 /*
3638 * Fill handshake headers
3639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003642 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3643 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3644 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003646 /*
3647 * DTLS has additional fields in the Handshake layer,
3648 * between the length field and the actual payload:
3649 * uint16 message_seq;
3650 * uint24 fragment_offset;
3651 * uint24 fragment_length;
3652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003654 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003655 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003656 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003657 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003658 {
3659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3660 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003661 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003662 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3664 }
3665
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003666 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003667 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003668
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003669 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003670 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003671 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003672 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3673 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3674 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003675 }
3676 else
3677 {
3678 ssl->out_msg[4] = 0;
3679 ssl->out_msg[5] = 0;
3680 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003681
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003682 /* Handshake hashes are computed without fragmentation,
3683 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003684 memset( ssl->out_msg + 6, 0x00, 3 );
3685 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003688
Hanno Becker0207e532018-08-28 10:28:28 +01003689 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3691 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 }
3693
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003694 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003697 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3698 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699 {
3700 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 return( ret );
3704 }
3705 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003706 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003708 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003709 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003710 {
3711 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3712 return( ret );
3713 }
3714 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003715
3716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3717
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003718 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003719}
3720
3721/*
3722 * Record layer functions
3723 */
3724
3725/*
3726 * Write current record.
3727 *
3728 * Uses:
3729 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3730 * - ssl->out_msglen: length of the record content (excl headers)
3731 * - ssl->out_msg: record content
3732 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003733int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003734{
3735 int ret, done = 0;
3736 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003737 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003738
3739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003742 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003744 {
3745 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003748 return( ret );
3749 }
3750
3751 len = ssl->out_msglen;
3752 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3756 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 ret = mbedtls_ssl_hw_record_write( ssl );
3761 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3764 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003765 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003766
3767 if( ret == 0 )
3768 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003771 if( !done )
3772 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003773 unsigned i;
3774 size_t protected_record_size;
3775
Paul Bakker05ef8352012-05-08 09:17:57 +00003776 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003778 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003779
Hanno Becker19859472018-08-06 09:40:20 +01003780 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003781 ssl->out_len[0] = (unsigned char)( len >> 8 );
3782 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003783
Paul Bakker48916f92012-09-16 19:57:18 +00003784 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003785 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003786 mbedtls_record rec;
3787
3788 rec.buf = ssl->out_iv;
3789 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3790 ( ssl->out_iv - ssl->out_buf );
3791 rec.data_len = ssl->out_msglen;
3792 rec.data_offset = ssl->out_msg - rec.buf;
3793
3794 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3795 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3796 ssl->conf->transport, rec.ver );
3797 rec.type = ssl->out_msgtype;
3798
Hanno Beckera18d1322018-01-03 14:27:32 +00003799 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003800 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003803 return( ret );
3804 }
3805
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003806 if( rec.data_offset != 0 )
3807 {
3808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3810 }
3811
Hanno Becker78f839d2019-03-14 12:56:23 +00003812 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003813 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3814 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003815 }
3816
Hanno Becker2b1e3542018-08-06 11:19:13 +01003817 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3818
3819#if defined(MBEDTLS_SSL_PROTO_DTLS)
3820 /* In case of DTLS, double-check that we don't exceed
3821 * the remaining space in the datagram. */
3822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3823 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003824 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003825 if( ret < 0 )
3826 return( ret );
3827
3828 if( protected_record_size > (size_t) ret )
3829 {
3830 /* Should never happen */
3831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3832 }
3833 }
3834#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003837 "version = [%d:%d], msglen = %d",
3838 ssl->out_hdr[0], ssl->out_hdr[1],
3839 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003842 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003843
3844 ssl->out_left += protected_record_size;
3845 ssl->out_hdr += protected_record_size;
3846 ssl_update_out_pointers( ssl, ssl->transform_out );
3847
Hanno Becker04484622018-08-06 09:49:38 +01003848 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3849 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3850 break;
3851
3852 /* The loop goes to its end iff the counter is wrapping */
3853 if( i == ssl_ep_len( ssl ) )
3854 {
3855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3856 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 }
3859
Hanno Becker67bc7c32018-08-06 11:33:50 +01003860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3862 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003863 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003864 size_t remaining;
3865 ret = ssl_get_remaining_payload_in_datagram( ssl );
3866 if( ret < 0 )
3867 {
3868 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3869 ret );
3870 return( ret );
3871 }
3872
3873 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003874 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003875 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003876 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003877 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003878 else
3879 {
Hanno Becker513815a2018-08-20 11:56:09 +01003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003881 }
3882 }
3883#endif /* MBEDTLS_SSL_PROTO_DTLS */
3884
3885 if( ( flush == SSL_FORCE_FLUSH ) &&
3886 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 return( ret );
3890 }
3891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003893
3894 return( 0 );
3895}
3896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003898
3899static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3900{
3901 if( ssl->in_msglen < ssl->in_hslen ||
3902 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3903 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3904 {
3905 return( 1 );
3906 }
3907 return( 0 );
3908}
Hanno Becker44650b72018-08-16 12:51:11 +01003909
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003910static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003911{
3912 return( ( ssl->in_msg[9] << 16 ) |
3913 ( ssl->in_msg[10] << 8 ) |
3914 ssl->in_msg[11] );
3915}
3916
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003917static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003918{
3919 return( ( ssl->in_msg[6] << 16 ) |
3920 ( ssl->in_msg[7] << 8 ) |
3921 ssl->in_msg[8] );
3922}
3923
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003924static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003925{
3926 uint32_t msg_len, frag_off, frag_len;
3927
3928 msg_len = ssl_get_hs_total_len( ssl );
3929 frag_off = ssl_get_hs_frag_off( ssl );
3930 frag_len = ssl_get_hs_frag_len( ssl );
3931
3932 if( frag_off > msg_len )
3933 return( -1 );
3934
3935 if( frag_len > msg_len - frag_off )
3936 return( -1 );
3937
3938 if( frag_len + 12 > ssl->in_msglen )
3939 return( -1 );
3940
3941 return( 0 );
3942}
3943
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003944/*
3945 * Mark bits in bitmask (used for DTLS HS reassembly)
3946 */
3947static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3948{
3949 unsigned int start_bits, end_bits;
3950
3951 start_bits = 8 - ( offset % 8 );
3952 if( start_bits != 8 )
3953 {
3954 size_t first_byte_idx = offset / 8;
3955
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003956 /* Special case */
3957 if( len <= start_bits )
3958 {
3959 for( ; len != 0; len-- )
3960 mask[first_byte_idx] |= 1 << ( start_bits - len );
3961
3962 /* Avoid potential issues with offset or len becoming invalid */
3963 return;
3964 }
3965
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003966 offset += start_bits; /* Now offset % 8 == 0 */
3967 len -= start_bits;
3968
3969 for( ; start_bits != 0; start_bits-- )
3970 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3971 }
3972
3973 end_bits = len % 8;
3974 if( end_bits != 0 )
3975 {
3976 size_t last_byte_idx = ( offset + len ) / 8;
3977
3978 len -= end_bits; /* Now len % 8 == 0 */
3979
3980 for( ; end_bits != 0; end_bits-- )
3981 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3982 }
3983
3984 memset( mask + offset / 8, 0xFF, len / 8 );
3985}
3986
3987/*
3988 * Check that bitmask is full
3989 */
3990static int ssl_bitmask_check( unsigned char *mask, size_t len )
3991{
3992 size_t i;
3993
3994 for( i = 0; i < len / 8; i++ )
3995 if( mask[i] != 0xFF )
3996 return( -1 );
3997
3998 for( i = 0; i < len % 8; i++ )
3999 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4000 return( -1 );
4001
4002 return( 0 );
4003}
4004
Hanno Becker56e205e2018-08-16 09:06:12 +01004005/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004006static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004007 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004008{
Hanno Becker56e205e2018-08-16 09:06:12 +01004009 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004010
Hanno Becker56e205e2018-08-16 09:06:12 +01004011 alloc_len = 12; /* Handshake header */
4012 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004013
Hanno Beckerd07df862018-08-16 09:14:58 +01004014 if( add_bitmap )
4015 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004016
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004017 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004018}
Hanno Becker56e205e2018-08-16 09:06:12 +01004019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004021
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004022static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004023{
4024 return( ( ssl->in_msg[1] << 16 ) |
4025 ( ssl->in_msg[2] << 8 ) |
4026 ssl->in_msg[3] );
4027}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004028
Simon Butcher99000142016-10-13 17:21:01 +01004029int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004034 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004036 }
4037
Hanno Becker12555c62018-08-16 12:47:53 +01004038 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004041 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004042 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004046 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004047 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004048 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004049
Hanno Becker44650b72018-08-16 12:51:11 +01004050 if( ssl_check_hs_header( ssl ) != 0 )
4051 {
4052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4053 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4054 }
4055
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004056 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004057 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4058 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4059 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4060 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004061 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004062 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4063 {
4064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4065 recv_msg_seq,
4066 ssl->handshake->in_msg_seq ) );
4067 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4068 }
4069
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004070 /* Retransmit only on last message from previous flight, to avoid
4071 * too many retransmissions.
4072 * Besides, No sane server ever retransmits HelloVerifyRequest */
4073 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004077 "message_seq = %d, start_of_flight = %d",
4078 recv_msg_seq,
4079 ssl->handshake->in_flight_start_seq ) );
4080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004084 return( ret );
4085 }
4086 }
4087 else
4088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004090 "message_seq = %d, expected = %d",
4091 recv_msg_seq,
4092 ssl->handshake->in_msg_seq ) );
4093 }
4094
Hanno Becker90333da2017-10-10 11:27:13 +01004095 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004096 }
4097 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004098
Hanno Becker6d97ef52018-08-16 13:09:04 +01004099 /* Message reassembly is handled alongside buffering of future
4100 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004101 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004102 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004103 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004106 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004107 }
4108 }
4109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004111 /* With TLS we don't handle fragmentation (for now) */
4112 if( ssl->in_msglen < ssl->in_hslen )
4113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4115 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004116 }
4117
Simon Butcher99000142016-10-13 17:21:01 +01004118 return( 0 );
4119}
4120
4121void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4122{
Hanno Becker0271f962018-08-16 13:23:47 +01004123 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004124
Hanno Becker0271f962018-08-16 13:23:47 +01004125 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004126 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004127 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004128 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004129
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004130 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004132 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004133 ssl->handshake != NULL )
4134 {
Hanno Becker0271f962018-08-16 13:23:47 +01004135 unsigned offset;
4136 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004137
Hanno Becker0271f962018-08-16 13:23:47 +01004138 /* Increment handshake sequence number */
4139 hs->in_msg_seq++;
4140
4141 /*
4142 * Clear up handshake buffering and reassembly structure.
4143 */
4144
4145 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004146 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004147
4148 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004149 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4150 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004151 offset++, hs_buf++ )
4152 {
4153 *hs_buf = *(hs_buf + 1);
4154 }
4155
4156 /* Create a fresh last entry */
4157 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004158 }
4159#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004160}
4161
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004162/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004163 * DTLS anti-replay: RFC 6347 4.1.2.6
4164 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004165 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4166 * Bit n is set iff record number in_window_top - n has been seen.
4167 *
4168 * Usually, in_window_top is the last record number seen and the lsb of
4169 * in_window is set. The only exception is the initial state (record number 0
4170 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4173static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004174{
4175 ssl->in_window_top = 0;
4176 ssl->in_window = 0;
4177}
4178
4179static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4180{
4181 return( ( (uint64_t) buf[0] << 40 ) |
4182 ( (uint64_t) buf[1] << 32 ) |
4183 ( (uint64_t) buf[2] << 24 ) |
4184 ( (uint64_t) buf[3] << 16 ) |
4185 ( (uint64_t) buf[4] << 8 ) |
4186 ( (uint64_t) buf[5] ) );
4187}
4188
4189/*
4190 * Return 0 if sequence number is acceptable, -1 otherwise
4191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004193{
4194 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4195 uint64_t bit;
4196
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004197 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004198 return( 0 );
4199
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004200 if( rec_seqnum > ssl->in_window_top )
4201 return( 0 );
4202
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004203 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004204
4205 if( bit >= 64 )
4206 return( -1 );
4207
4208 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4209 return( -1 );
4210
4211 return( 0 );
4212}
4213
4214/*
4215 * Update replay window on new validated record
4216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004218{
4219 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4220
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004221 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004222 return;
4223
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004224 if( rec_seqnum > ssl->in_window_top )
4225 {
4226 /* Update window_top and the contents of the window */
4227 uint64_t shift = rec_seqnum - ssl->in_window_top;
4228
4229 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004230 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004231 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004232 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004233 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004234 ssl->in_window |= 1;
4235 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004236
4237 ssl->in_window_top = rec_seqnum;
4238 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004239 else
4240 {
4241 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004242 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004243
4244 if( bit < 64 ) /* Always true, but be extra sure */
4245 ssl->in_window |= (uint64_t) 1 << bit;
4246 }
4247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004249
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004250#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004251/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004252static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4253
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004254/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004255 * Without any SSL context, check if a datagram looks like a ClientHello with
4256 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004257 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004258 *
4259 * - if cookie is valid, return 0
4260 * - if ClientHello looks superficially valid but cookie is not,
4261 * fill obuf and set olen, then
4262 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4263 * - otherwise return a specific error code
4264 */
4265static int ssl_check_dtls_clihlo_cookie(
4266 mbedtls_ssl_cookie_write_t *f_cookie_write,
4267 mbedtls_ssl_cookie_check_t *f_cookie_check,
4268 void *p_cookie,
4269 const unsigned char *cli_id, size_t cli_id_len,
4270 const unsigned char *in, size_t in_len,
4271 unsigned char *obuf, size_t buf_len, size_t *olen )
4272{
4273 size_t sid_len, cookie_len;
4274 unsigned char *p;
4275
4276 if( f_cookie_write == NULL || f_cookie_check == NULL )
4277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4278
4279 /*
4280 * Structure of ClientHello with record and handshake headers,
4281 * and expected values. We don't need to check a lot, more checks will be
4282 * done when actually parsing the ClientHello - skipping those checks
4283 * avoids code duplication and does not make cookie forging any easier.
4284 *
4285 * 0-0 ContentType type; copied, must be handshake
4286 * 1-2 ProtocolVersion version; copied
4287 * 3-4 uint16 epoch; copied, must be 0
4288 * 5-10 uint48 sequence_number; copied
4289 * 11-12 uint16 length; (ignored)
4290 *
4291 * 13-13 HandshakeType msg_type; (ignored)
4292 * 14-16 uint24 length; (ignored)
4293 * 17-18 uint16 message_seq; copied
4294 * 19-21 uint24 fragment_offset; copied, must be 0
4295 * 22-24 uint24 fragment_length; (ignored)
4296 *
4297 * 25-26 ProtocolVersion client_version; (ignored)
4298 * 27-58 Random random; (ignored)
4299 * 59-xx SessionID session_id; 1 byte len + sid_len content
4300 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4301 * ...
4302 *
4303 * Minimum length is 61 bytes.
4304 */
4305 if( in_len < 61 ||
4306 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4307 in[3] != 0 || in[4] != 0 ||
4308 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4309 {
4310 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4311 }
4312
4313 sid_len = in[59];
4314 if( sid_len > in_len - 61 )
4315 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4316
4317 cookie_len = in[60 + sid_len];
4318 if( cookie_len > in_len - 60 )
4319 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4320
4321 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4322 cli_id, cli_id_len ) == 0 )
4323 {
4324 /* Valid cookie */
4325 return( 0 );
4326 }
4327
4328 /*
4329 * If we get here, we've got an invalid cookie, let's prepare HVR.
4330 *
4331 * 0-0 ContentType type; copied
4332 * 1-2 ProtocolVersion version; copied
4333 * 3-4 uint16 epoch; copied
4334 * 5-10 uint48 sequence_number; copied
4335 * 11-12 uint16 length; olen - 13
4336 *
4337 * 13-13 HandshakeType msg_type; hello_verify_request
4338 * 14-16 uint24 length; olen - 25
4339 * 17-18 uint16 message_seq; copied
4340 * 19-21 uint24 fragment_offset; copied
4341 * 22-24 uint24 fragment_length; olen - 25
4342 *
4343 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4344 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4345 *
4346 * Minimum length is 28.
4347 */
4348 if( buf_len < 28 )
4349 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4350
4351 /* Copy most fields and adapt others */
4352 memcpy( obuf, in, 25 );
4353 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4354 obuf[25] = 0xfe;
4355 obuf[26] = 0xff;
4356
4357 /* Generate and write actual cookie */
4358 p = obuf + 28;
4359 if( f_cookie_write( p_cookie,
4360 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4361 {
4362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4363 }
4364
4365 *olen = p - obuf;
4366
4367 /* Go back and fill length fields */
4368 obuf[27] = (unsigned char)( *olen - 28 );
4369
4370 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4371 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4372 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4373
4374 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4375 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4376
4377 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4378}
4379
4380/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004381 * Handle possible client reconnect with the same UDP quadruplet
4382 * (RFC 6347 Section 4.2.8).
4383 *
4384 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4385 * that looks like a ClientHello.
4386 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004387 * - if the input looks like a ClientHello without cookies,
4388 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004389 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390 * - if the input looks like a ClientHello with a valid cookie,
4391 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004392 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004393 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004394 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004395 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004396 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4397 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004398 */
4399static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4400{
4401 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004402 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004403
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004404 ret = ssl_check_dtls_clihlo_cookie(
4405 ssl->conf->f_cookie_write,
4406 ssl->conf->f_cookie_check,
4407 ssl->conf->p_cookie,
4408 ssl->cli_id, ssl->cli_id_len,
4409 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004410 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004411
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004412 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4413
4414 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004415 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004416 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004417 * If the error is permanent we'll catch it later,
4418 * if it's not, then hopefully it'll work next time. */
4419 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4420
4421 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004422 }
4423
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004424 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004425 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004426 /* Got a valid cookie, partially reset context */
4427 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4428 {
4429 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4430 return( ret );
4431 }
4432
4433 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004434 }
4435
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004436 return( ret );
4437}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004438#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004439
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004440/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004441 * ContentType type;
4442 * ProtocolVersion version;
4443 * uint16 epoch; // DTLS only
4444 * uint48 sequence_number; // DTLS only
4445 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004446 *
4447 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004448 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004449 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4450 *
4451 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004452 * 1. proceed with the record if this function returns 0
4453 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4454 * 3. return CLIENT_RECONNECT if this function return that value
4455 * 4. drop the whole datagram if this function returns anything else.
4456 * Point 2 is needed when the peer is resending, and we have already received
4457 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004461 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463 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 +02004464
Paul Bakker5121ce52009-01-03 21:22:43 +00004465 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004466 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004467 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004471 ssl->in_msgtype,
4472 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004474 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4476 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4477 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4478 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004481
4482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004483 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4484 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004485 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4486#endif /* MBEDTLS_SSL_PROTO_DTLS */
4487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4488 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004491 }
4492
4493 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004494 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4497 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 }
4499
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004500 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4503 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
4505
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004506 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004507 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004508 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4511 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004512 }
4513
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004514 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004515 * DTLS-related tests.
4516 * Check epoch before checking length constraint because
4517 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4518 * message gets duplicated before the corresponding Finished message,
4519 * the second ChangeCipherSpec should be discarded because it belongs
4520 * to an old epoch, but not because its length is shorter than
4521 * the minimum record length for packets using the new record transform.
4522 * Note that these two kinds of failures are handled differently,
4523 * as an unexpected record is silently skipped but an invalid
4524 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004525 */
4526#if defined(MBEDTLS_SSL_PROTO_DTLS)
4527 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4528 {
4529 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4530
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004531 /* Check epoch (and sequence number) with DTLS */
4532 if( rec_epoch != ssl->in_epoch )
4533 {
4534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4535 "expected %d, received %d",
4536 ssl->in_epoch, rec_epoch ) );
4537
4538#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4539 /*
4540 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4541 * access the first byte of record content (handshake type), as we
4542 * have an active transform (possibly iv_len != 0), so use the
4543 * fact that the record header len is 13 instead.
4544 */
4545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4546 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4547 rec_epoch == 0 &&
4548 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4549 ssl->in_left > 13 &&
4550 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4551 {
4552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4553 "from the same port" ) );
4554 return( ssl_handle_possible_reconnect( ssl ) );
4555 }
4556 else
4557#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004558 {
4559 /* Consider buffering the record. */
4560 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4561 {
4562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4563 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4564 }
4565
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004566 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004567 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 }
4569
4570#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4571 /* Replay detection only works for the current epoch */
4572 if( rec_epoch == ssl->in_epoch &&
4573 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4574 {
4575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4576 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4577 }
4578#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004579
Hanno Becker52c6dc62017-05-26 16:07:36 +01004580 /* Drop unexpected ApplicationData records,
4581 * except at the beginning of renegotiations */
4582 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4583 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4584#if defined(MBEDTLS_SSL_RENEGOTIATION)
4585 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4586 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4587#endif
4588 )
4589 {
4590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4591 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4592 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004593 }
4594#endif /* MBEDTLS_SSL_PROTO_DTLS */
4595
Hanno Becker52c6dc62017-05-26 16:07:36 +01004596
4597 /* Check length against bounds of the current transform and version */
4598 if( ssl->transform_in == NULL )
4599 {
4600 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004601 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004602 {
4603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4604 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4605 }
4606 }
4607 else
4608 {
4609 if( ssl->in_msglen < ssl->transform_in->minlen )
4610 {
4611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4612 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4613 }
4614
4615#if defined(MBEDTLS_SSL_PROTO_SSL3)
4616 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004617 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004618 {
4619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4621 }
4622#endif
4623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4625 /*
4626 * TLS encrypted messages can have up to 256 bytes of padding
4627 */
4628 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4629 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004630 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004631 {
4632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4633 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4634 }
4635#endif
4636 }
4637
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004638 return( 0 );
4639}
Paul Bakker5121ce52009-01-03 21:22:43 +00004640
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004641/*
4642 * If applicable, decrypt (and decompress) record content
4643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004645{
4646 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4649 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4652 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 ret = mbedtls_ssl_hw_record_read( ssl );
4657 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004661 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004662
4663 if( ret == 0 )
4664 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004667 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004669 mbedtls_record rec;
4670
4671 rec.buf = ssl->in_iv;
4672 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4673 - ( ssl->in_iv - ssl->in_buf );
4674 rec.data_len = ssl->in_msglen;
4675 rec.data_offset = 0;
4676
4677 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4678 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4679 ssl->conf->transport, rec.ver );
4680 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004681 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4682 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 return( ret );
4686 }
4687
Hanno Becker29800d22018-08-07 14:30:18 +01004688 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4689 {
4690 /* Should never happen */
4691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004694 ssl->in_msglen = rec.data_len;
4695 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4696 ssl->in_len[1] = (unsigned char)( rec.data_len );
4697
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004698 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4699 ssl->in_msg, ssl->in_msglen );
4700
Angus Grattond8213d02016-05-25 20:56:48 +10004701 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4704 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004706 else if( ssl->in_msglen == 0 )
4707 {
4708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4709 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4710 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4711 {
4712 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4714 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4715 }
4716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4717
4718 ssl->nb_zero++;
4719
4720 /*
4721 * Three or more empty messages may be a DoS attack
4722 * (excessive CPU consumption).
4723 */
4724 if( ssl->nb_zero > 3 )
4725 {
4726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4727 "messages, possible DoS attack" ) );
4728 /* Q: Is that the right error code? */
4729 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4730 }
4731 }
4732 else
4733 ssl->nb_zero = 0;
4734
4735#if defined(MBEDTLS_SSL_PROTO_DTLS)
4736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4737 {
4738 ; /* in_ctr read from peer, not maintained internally */
4739 }
4740 else
4741#endif
4742 {
4743 unsigned i;
4744 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4745 if( ++ssl->in_ctr[i - 1] != 0 )
4746 break;
4747
4748 /* The loop goes to its end iff the counter is wrapping */
4749 if( i == ssl_ep_len( ssl ) )
4750 {
4751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4752 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4753 }
4754 }
4755
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 }
4757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004759 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004761 {
4762 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004765 return( ret );
4766 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004767 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004774 }
4775#endif
4776
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004777 return( 0 );
4778}
4779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004781
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004782/*
4783 * Read a record.
4784 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004785 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4786 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4787 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004788 */
Hanno Becker1097b342018-08-15 14:09:41 +01004789
4790/* Helper functions for mbedtls_ssl_read_record(). */
4791static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004792static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4793static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004794
Hanno Becker327c93b2018-08-15 13:56:18 +01004795int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004796 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004797{
4798 int ret;
4799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004801
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004802 if( ssl->keep_current_message == 0 )
4803 {
4804 do {
Simon Butcher99000142016-10-13 17:21:01 +01004805
Hanno Becker26994592018-08-15 14:14:59 +01004806 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004807 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004808 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004809
Hanno Beckere74d5562018-08-15 14:26:08 +01004810 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004811 {
Hanno Becker40f50842018-08-15 14:48:01 +01004812#if defined(MBEDTLS_SSL_PROTO_DTLS)
4813 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004814
Hanno Becker40f50842018-08-15 14:48:01 +01004815 /* We only check for buffered messages if the
4816 * current datagram is fully consumed. */
4817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004818 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004819 {
Hanno Becker40f50842018-08-15 14:48:01 +01004820 if( ssl_load_buffered_message( ssl ) == 0 )
4821 have_buffered = 1;
4822 }
4823
4824 if( have_buffered == 0 )
4825#endif /* MBEDTLS_SSL_PROTO_DTLS */
4826 {
4827 ret = ssl_get_next_record( ssl );
4828 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4829 continue;
4830
4831 if( ret != 0 )
4832 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004833 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004834 return( ret );
4835 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004836 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004837 }
4838
4839 ret = mbedtls_ssl_handle_message_type( ssl );
4840
Hanno Becker40f50842018-08-15 14:48:01 +01004841#if defined(MBEDTLS_SSL_PROTO_DTLS)
4842 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4843 {
4844 /* Buffer future message */
4845 ret = ssl_buffer_message( ssl );
4846 if( ret != 0 )
4847 return( ret );
4848
4849 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4850 }
4851#endif /* MBEDTLS_SSL_PROTO_DTLS */
4852
Hanno Becker90333da2017-10-10 11:27:13 +01004853 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4854 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004855
4856 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004857 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004858 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004859 return( ret );
4860 }
4861
Hanno Becker327c93b2018-08-15 13:56:18 +01004862 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004863 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004864 {
4865 mbedtls_ssl_update_handshake_status( ssl );
4866 }
Simon Butcher99000142016-10-13 17:21:01 +01004867 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004868 else
Simon Butcher99000142016-10-13 17:21:01 +01004869 {
Hanno Becker02f59072018-08-15 14:00:24 +01004870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004871 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004872 }
4873
4874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4875
4876 return( 0 );
4877}
4878
Hanno Becker40f50842018-08-15 14:48:01 +01004879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004880static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004881{
Hanno Becker40f50842018-08-15 14:48:01 +01004882 if( ssl->in_left > ssl->next_record_offset )
4883 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004884
Hanno Becker40f50842018-08-15 14:48:01 +01004885 return( 0 );
4886}
4887
4888static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4889{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004890 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004891 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004892 int ret = 0;
4893
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004894 if( hs == NULL )
4895 return( -1 );
4896
Hanno Beckere00ae372018-08-20 09:39:42 +01004897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4898
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004899 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4900 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4901 {
4902 /* Check if we have seen a ChangeCipherSpec before.
4903 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004904 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004905 {
4906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4907 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004908 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004909 }
4910
Hanno Becker39b8bc92018-08-28 17:17:13 +01004911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004912 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4913 ssl->in_msglen = 1;
4914 ssl->in_msg[0] = 1;
4915
4916 /* As long as they are equal, the exact value doesn't matter. */
4917 ssl->in_left = 0;
4918 ssl->next_record_offset = 0;
4919
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004920 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004921 goto exit;
4922 }
Hanno Becker37f95322018-08-16 13:55:32 +01004923
Hanno Beckerb8f50142018-08-28 10:01:34 +01004924#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004925 /* Debug only */
4926 {
4927 unsigned offset;
4928 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4929 {
4930 hs_buf = &hs->buffering.hs[offset];
4931 if( hs_buf->is_valid == 1 )
4932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4934 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004935 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004936 }
4937 }
4938 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004939#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004940
4941 /* Check if we have buffered and/or fully reassembled the
4942 * next handshake message. */
4943 hs_buf = &hs->buffering.hs[0];
4944 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4945 {
4946 /* Synthesize a record containing the buffered HS message. */
4947 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4948 ( hs_buf->data[2] << 8 ) |
4949 hs_buf->data[3];
4950
4951 /* Double-check that we haven't accidentally buffered
4952 * a message that doesn't fit into the input buffer. */
4953 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4957 }
4958
4959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4960 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4961 hs_buf->data, msg_len + 12 );
4962
4963 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4964 ssl->in_hslen = msg_len + 12;
4965 ssl->in_msglen = msg_len + 12;
4966 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4967
4968 ret = 0;
4969 goto exit;
4970 }
4971 else
4972 {
4973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4974 hs->in_msg_seq ) );
4975 }
4976
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004977 ret = -1;
4978
4979exit:
4980
4981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4982 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004983}
4984
Hanno Beckera02b0b42018-08-21 17:20:27 +01004985static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4986 size_t desired )
4987{
4988 int offset;
4989 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4991 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004992
Hanno Becker01315ea2018-08-21 17:22:17 +01004993 /* Get rid of future records epoch first, if such exist. */
4994 ssl_free_buffered_record( ssl );
4995
4996 /* Check if we have enough space available now. */
4997 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4998 hs->buffering.total_bytes_buffered ) )
4999 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005001 return( 0 );
5002 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005003
Hanno Becker4f432ad2018-08-28 10:02:32 +01005004 /* We don't have enough space to buffer the next expected handshake
5005 * message. Remove buffers used for future messages to gain space,
5006 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005007 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5008 offset >= 0; offset-- )
5009 {
5010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5011 offset ) );
5012
Hanno Beckerb309b922018-08-23 13:18:05 +01005013 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005014
5015 /* Check if we have enough space available now. */
5016 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5017 hs->buffering.total_bytes_buffered ) )
5018 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005020 return( 0 );
5021 }
5022 }
5023
5024 return( -1 );
5025}
5026
Hanno Becker40f50842018-08-15 14:48:01 +01005027static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5028{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005029 int ret = 0;
5030 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5031
5032 if( hs == NULL )
5033 return( 0 );
5034
5035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5036
5037 switch( ssl->in_msgtype )
5038 {
5039 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005041
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005042 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005043 break;
5044
5045 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005046 {
5047 unsigned recv_msg_seq_offset;
5048 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5049 mbedtls_ssl_hs_buffer *hs_buf;
5050 size_t msg_len = ssl->in_hslen - 12;
5051
5052 /* We should never receive an old handshake
5053 * message - double-check nonetheless. */
5054 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5055 {
5056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5058 }
5059
5060 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5061 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5062 {
5063 /* Silently ignore -- message too far in the future */
5064 MBEDTLS_SSL_DEBUG_MSG( 2,
5065 ( "Ignore future HS message with sequence number %u, "
5066 "buffering window %u - %u",
5067 recv_msg_seq, ssl->handshake->in_msg_seq,
5068 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5069
5070 goto exit;
5071 }
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5074 recv_msg_seq, recv_msg_seq_offset ) );
5075
5076 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5077
5078 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005079 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005080 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005081 size_t reassembly_buf_sz;
5082
Hanno Becker37f95322018-08-16 13:55:32 +01005083 hs_buf->is_fragmented =
5084 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5085
5086 /* We copy the message back into the input buffer
5087 * after reassembly, so check that it's not too large.
5088 * This is an implementation-specific limitation
5089 * and not one from the standard, hence it is not
5090 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005091 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005092 {
5093 /* Ignore message */
5094 goto exit;
5095 }
5096
Hanno Beckere0b150f2018-08-21 15:51:03 +01005097 /* Check if we have enough space to buffer the message. */
5098 if( hs->buffering.total_bytes_buffered >
5099 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5100 {
5101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5103 }
5104
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005105 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5106 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005107
5108 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5109 hs->buffering.total_bytes_buffered ) )
5110 {
5111 if( recv_msg_seq_offset > 0 )
5112 {
5113 /* If we can't buffer a future message because
5114 * of space limitations -- ignore. */
5115 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",
5116 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5117 (unsigned) hs->buffering.total_bytes_buffered ) );
5118 goto exit;
5119 }
Hanno Beckere1801392018-08-21 16:51:05 +01005120 else
5121 {
5122 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",
5123 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5124 (unsigned) hs->buffering.total_bytes_buffered ) );
5125 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005126
Hanno Beckera02b0b42018-08-21 17:20:27 +01005127 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005128 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005129 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",
5130 (unsigned) msg_len,
5131 (unsigned) reassembly_buf_sz,
5132 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005133 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005134 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5135 goto exit;
5136 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005137 }
5138
5139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5140 msg_len ) );
5141
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005142 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5143 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005144 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005145 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005146 goto exit;
5147 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005148 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005149
5150 /* Prepare final header: copy msg_type, length and message_seq,
5151 * then add standardised fragment_offset and fragment_length */
5152 memcpy( hs_buf->data, ssl->in_msg, 6 );
5153 memset( hs_buf->data + 6, 0, 3 );
5154 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5155
5156 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005157
5158 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005159 }
5160 else
5161 {
5162 /* Make sure msg_type and length are consistent */
5163 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5164 {
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5166 /* Ignore */
5167 goto exit;
5168 }
5169 }
5170
Hanno Becker4422bbb2018-08-20 09:40:19 +01005171 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005172 {
5173 size_t frag_len, frag_off;
5174 unsigned char * const msg = hs_buf->data + 12;
5175
5176 /*
5177 * Check and copy current fragment
5178 */
5179
5180 /* Validation of header fields already done in
5181 * mbedtls_ssl_prepare_handshake_record(). */
5182 frag_off = ssl_get_hs_frag_off( ssl );
5183 frag_len = ssl_get_hs_frag_len( ssl );
5184
5185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5186 frag_off, frag_len ) );
5187 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5188
5189 if( hs_buf->is_fragmented )
5190 {
5191 unsigned char * const bitmask = msg + msg_len;
5192 ssl_bitmask_set( bitmask, frag_off, frag_len );
5193 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5194 msg_len ) == 0 );
5195 }
5196 else
5197 {
5198 hs_buf->is_complete = 1;
5199 }
5200
5201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5202 hs_buf->is_complete ? "" : "not yet " ) );
5203 }
5204
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005205 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005206 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005207
5208 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005209 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005210 break;
5211 }
5212
5213exit:
5214
5215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5216 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005217}
5218#endif /* MBEDTLS_SSL_PROTO_DTLS */
5219
Hanno Becker1097b342018-08-15 14:09:41 +01005220static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005221{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005222 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005223 * Consume last content-layer message and potentially
5224 * update in_msglen which keeps track of the contents'
5225 * consumption state.
5226 *
5227 * (1) Handshake messages:
5228 * Remove last handshake message, move content
5229 * and adapt in_msglen.
5230 *
5231 * (2) Alert messages:
5232 * Consume whole record content, in_msglen = 0.
5233 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005234 * (3) Change cipher spec:
5235 * Consume whole record content, in_msglen = 0.
5236 *
5237 * (4) Application data:
5238 * Don't do anything - the record layer provides
5239 * the application data as a stream transport
5240 * and consumes through mbedtls_ssl_read only.
5241 *
5242 */
5243
5244 /* Case (1): Handshake messages */
5245 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005247 /* Hard assertion to be sure that no application data
5248 * is in flight, as corrupting ssl->in_msglen during
5249 * ssl->in_offt != NULL is fatal. */
5250 if( ssl->in_offt != NULL )
5251 {
5252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5254 }
5255
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005256 /*
5257 * Get next Handshake message in the current record
5258 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005259
Hanno Becker4a810fb2017-05-24 16:27:30 +01005260 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005261 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005262 * current handshake content: If DTLS handshake
5263 * fragmentation is used, that's the fragment
5264 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005265 * size here is faulty and should be changed at
5266 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005267 * (2) While it doesn't seem to cause problems, one
5268 * has to be very careful not to assume that in_hslen
5269 * is always <= in_msglen in a sensible communication.
5270 * Again, it's wrong for DTLS handshake fragmentation.
5271 * The following check is therefore mandatory, and
5272 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005273 * Additionally, ssl->in_hslen might be arbitrarily out of
5274 * bounds after handling a DTLS message with an unexpected
5275 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005276 */
5277 if( ssl->in_hslen < ssl->in_msglen )
5278 {
5279 ssl->in_msglen -= ssl->in_hslen;
5280 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5281 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005282
Hanno Becker4a810fb2017-05-24 16:27:30 +01005283 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5284 ssl->in_msg, ssl->in_msglen );
5285 }
5286 else
5287 {
5288 ssl->in_msglen = 0;
5289 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005290
Hanno Becker4a810fb2017-05-24 16:27:30 +01005291 ssl->in_hslen = 0;
5292 }
5293 /* Case (4): Application data */
5294 else if( ssl->in_offt != NULL )
5295 {
5296 return( 0 );
5297 }
5298 /* Everything else (CCS & Alerts) */
5299 else
5300 {
5301 ssl->in_msglen = 0;
5302 }
5303
Hanno Becker1097b342018-08-15 14:09:41 +01005304 return( 0 );
5305}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005306
Hanno Beckere74d5562018-08-15 14:26:08 +01005307static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5308{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005309 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005310 return( 1 );
5311
5312 return( 0 );
5313}
5314
Hanno Becker5f066e72018-08-16 14:56:31 +01005315#if defined(MBEDTLS_SSL_PROTO_DTLS)
5316
5317static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5318{
5319 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5320 if( hs == NULL )
5321 return;
5322
Hanno Becker01315ea2018-08-21 17:22:17 +01005323 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005324 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005325 hs->buffering.total_bytes_buffered -=
5326 hs->buffering.future_record.len;
5327
5328 mbedtls_free( hs->buffering.future_record.data );
5329 hs->buffering.future_record.data = NULL;
5330 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005331}
5332
5333static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5334{
5335 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5336 unsigned char * rec;
5337 size_t rec_len;
5338 unsigned rec_epoch;
5339
5340 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5341 return( 0 );
5342
5343 if( hs == NULL )
5344 return( 0 );
5345
Hanno Becker5f066e72018-08-16 14:56:31 +01005346 rec = hs->buffering.future_record.data;
5347 rec_len = hs->buffering.future_record.len;
5348 rec_epoch = hs->buffering.future_record.epoch;
5349
5350 if( rec == NULL )
5351 return( 0 );
5352
Hanno Becker4cb782d2018-08-20 11:19:05 +01005353 /* Only consider loading future records if the
5354 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005355 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005356 return( 0 );
5357
Hanno Becker5f066e72018-08-16 14:56:31 +01005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5359
5360 if( rec_epoch != ssl->in_epoch )
5361 {
5362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5363 goto exit;
5364 }
5365
5366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5367
5368 /* Double-check that the record is not too large */
5369 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5370 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5371 {
5372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5374 }
5375
5376 memcpy( ssl->in_hdr, rec, rec_len );
5377 ssl->in_left = rec_len;
5378 ssl->next_record_offset = 0;
5379
5380 ssl_free_buffered_record( ssl );
5381
5382exit:
5383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5384 return( 0 );
5385}
5386
5387static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5388{
5389 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5390 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005391 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005392
5393 /* Don't buffer future records outside handshakes. */
5394 if( hs == NULL )
5395 return( 0 );
5396
5397 /* Only buffer handshake records (we are only interested
5398 * in Finished messages). */
5399 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5400 return( 0 );
5401
5402 /* Don't buffer more than one future epoch record. */
5403 if( hs->buffering.future_record.data != NULL )
5404 return( 0 );
5405
Hanno Becker01315ea2018-08-21 17:22:17 +01005406 /* Don't buffer record if there's not enough buffering space remaining. */
5407 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5408 hs->buffering.total_bytes_buffered ) )
5409 {
5410 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",
5411 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5412 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005413 return( 0 );
5414 }
5415
Hanno Becker5f066e72018-08-16 14:56:31 +01005416 /* Buffer record */
5417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5418 ssl->in_epoch + 1 ) );
5419 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5420 rec_hdr_len + ssl->in_msglen );
5421
5422 /* ssl_parse_record_header() only considers records
5423 * of the next epoch as candidates for buffering. */
5424 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005425 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005426
5427 hs->buffering.future_record.data =
5428 mbedtls_calloc( 1, hs->buffering.future_record.len );
5429 if( hs->buffering.future_record.data == NULL )
5430 {
5431 /* If we run out of RAM trying to buffer a
5432 * record from the next epoch, just ignore. */
5433 return( 0 );
5434 }
5435
Hanno Becker01315ea2018-08-21 17:22:17 +01005436 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005437
Hanno Becker01315ea2018-08-21 17:22:17 +01005438 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005439 return( 0 );
5440}
5441
5442#endif /* MBEDTLS_SSL_PROTO_DTLS */
5443
Hanno Beckere74d5562018-08-15 14:26:08 +01005444static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005445{
5446 int ret;
5447
Hanno Becker5f066e72018-08-16 14:56:31 +01005448#if defined(MBEDTLS_SSL_PROTO_DTLS)
5449 /* We might have buffered a future record; if so,
5450 * and if the epoch matches now, load it.
5451 * On success, this call will set ssl->in_left to
5452 * the length of the buffered record, so that
5453 * the calls to ssl_fetch_input() below will
5454 * essentially be no-ops. */
5455 ret = ssl_load_buffered_record( ssl );
5456 if( ret != 0 )
5457 return( ret );
5458#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005463 return( ret );
5464 }
5465
5466 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5470 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005471 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005472 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5473 {
5474 ret = ssl_buffer_future_record( ssl );
5475 if( ret != 0 )
5476 return( ret );
5477
5478 /* Fall through to handling of unexpected records */
5479 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5480 }
5481
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005482 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5483 {
5484 /* Skip unexpected record (but not whole datagram) */
5485 ssl->next_record_offset = ssl->in_msglen
5486 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005487
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5489 "(header)" ) );
5490 }
5491 else
5492 {
5493 /* Skip invalid record and the rest of the datagram */
5494 ssl->next_record_offset = 0;
5495 ssl->in_left = 0;
5496
5497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5498 "(header)" ) );
5499 }
5500
5501 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005502 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005503 }
5504#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005505 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005506 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005507
5508 /*
5509 * Read and optionally decrypt the message contents
5510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5512 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005515 return( ret );
5516 }
5517
5518 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005523 if( ssl->next_record_offset < ssl->in_left )
5524 {
5525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5526 }
5527 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005528 else
5529#endif
5530 ssl->in_left = 0;
5531
5532 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005536 {
5537 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5539 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005540 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005541 /* Except when waiting for Finished as a bad mac here
5542 * probably means something went wrong in the handshake
5543 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5544 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5545 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5546 {
5547#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5548 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5549 {
5550 mbedtls_ssl_send_alert_message( ssl,
5551 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5552 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5553 }
5554#endif
5555 return( ret );
5556 }
5557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005559 if( ssl->conf->badmac_limit != 0 &&
5560 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5563 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005564 }
5565#endif
5566
Hanno Becker4a810fb2017-05-24 16:27:30 +01005567 /* As above, invalid records cause
5568 * dismissal of the whole datagram. */
5569
5570 ssl->next_record_offset = 0;
5571 ssl->in_left = 0;
5572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005574 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005575 }
5576
5577 return( ret );
5578 }
5579 else
5580#endif
5581 {
5582 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5584 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 mbedtls_ssl_send_alert_message( ssl,
5587 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5588 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005589 }
5590#endif
5591 return( ret );
5592 }
5593 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005594
Simon Butcher99000142016-10-13 17:21:01 +01005595 return( 0 );
5596}
5597
5598int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5599{
5600 int ret;
5601
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005602 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005603 * Handle particular types of records
5604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 {
Simon Butcher99000142016-10-13 17:21:01 +01005607 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5608 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005609 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 }
5612
Hanno Beckere678eaa2018-08-21 14:57:46 +01005613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005614 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005615 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005616 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5618 ssl->in_msglen ) );
5619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005620 }
5621
Hanno Beckere678eaa2018-08-21 14:57:46 +01005622 if( ssl->in_msg[0] != 1 )
5623 {
5624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5625 ssl->in_msg[0] ) );
5626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5627 }
5628
5629#if defined(MBEDTLS_SSL_PROTO_DTLS)
5630 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5631 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5632 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5633 {
5634 if( ssl->handshake == NULL )
5635 {
5636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5637 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5638 }
5639
5640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5641 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5642 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005643#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005644 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005648 if( ssl->in_msglen != 2 )
5649 {
5650 /* Note: Standard allows for more than one 2 byte alert
5651 to be packed in a single message, but Mbed TLS doesn't
5652 currently support this. */
5653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5654 ssl->in_msglen ) );
5655 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5656 }
5657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 ssl->in_msg[0], ssl->in_msg[1] ) );
5660
5661 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005662 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005667 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 }
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5672 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5675 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005677
5678#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5679 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5680 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5681 {
Hanno Becker90333da2017-10-10 11:27:13 +01005682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005683 /* Will be handled when trying to parse ServerHello */
5684 return( 0 );
5685 }
5686#endif
5687
5688#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5689 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5690 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5691 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5692 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5693 {
5694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5695 /* Will be handled in mbedtls_ssl_parse_certificate() */
5696 return( 0 );
5697 }
5698#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5699
5700 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005701 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 }
5703
Hanno Beckerc76c6192017-06-06 10:03:17 +01005704#if defined(MBEDTLS_SSL_PROTO_DTLS)
5705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5706 ssl->handshake != NULL &&
5707 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5708 {
5709 ssl_handshake_wrapup_free_hs_transform( ssl );
5710 }
5711#endif
5712
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 return( 0 );
5714}
5715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005717{
5718 int ret;
5719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5721 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5722 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005723 {
5724 return( ret );
5725 }
5726
5727 return( 0 );
5728}
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005731 unsigned char level,
5732 unsigned char message )
5733{
5734 int ret;
5735
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005736 if( ssl == NULL || ssl->conf == NULL )
5737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005743 ssl->out_msglen = 2;
5744 ssl->out_msg[0] = level;
5745 ssl->out_msg[1] = message;
5746
Hanno Becker67bc7c32018-08-06 11:33:50 +01005747 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005750 return( ret );
5751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005753
5754 return( 0 );
5755}
5756
Hanno Beckerb9d44792019-02-08 07:19:04 +00005757#if defined(MBEDTLS_X509_CRT_PARSE_C)
5758static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5759{
5760#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5761 if( session->peer_cert != NULL )
5762 {
5763 mbedtls_x509_crt_free( session->peer_cert );
5764 mbedtls_free( session->peer_cert );
5765 session->peer_cert = NULL;
5766 }
5767#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5768 if( session->peer_cert_digest != NULL )
5769 {
5770 /* Zeroization is not necessary. */
5771 mbedtls_free( session->peer_cert_digest );
5772 session->peer_cert_digest = NULL;
5773 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5774 session->peer_cert_digest_len = 0;
5775 }
5776#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5777}
5778#endif /* MBEDTLS_X509_CRT_PARSE_C */
5779
Paul Bakker5121ce52009-01-03 21:22:43 +00005780/*
5781 * Handshake functions
5782 */
Hanno Becker21489932019-02-05 13:20:55 +00005783#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005784/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005786{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005787 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5788 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005791
Hanno Becker7177a882019-02-05 13:36:46 +00005792 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005795 ssl->state++;
5796 return( 0 );
5797 }
5798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005801}
5802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005804{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005805 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5806 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005809
Hanno Becker7177a882019-02-05 13:36:46 +00005810 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005813 ssl->state++;
5814 return( 0 );
5815 }
5816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005819}
Gilles Peskinef9828522017-05-03 12:28:43 +02005820
Hanno Becker21489932019-02-05 13:20:55 +00005821#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005822/* Some certificate support -> implement write and parse */
5823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005827 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005829 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5830 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005833
Hanno Becker7177a882019-02-05 13:36:46 +00005834 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005837 ssl->state++;
5838 return( 0 );
5839 }
5840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005842 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 {
5844 if( ssl->client_auth == 0 )
5845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 ssl->state++;
5848 return( 0 );
5849 }
5850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005852 /*
5853 * If using SSLv3 and got no cert, send an Alert message
5854 * (otherwise an empty Certificate message will be sent).
5855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5857 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 {
5859 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5861 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5862 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005865 goto write_msg;
5866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005868 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#endif /* MBEDTLS_SSL_CLI_C */
5870#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5876 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 }
5878 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005879#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882
5883 /*
5884 * 0 . 0 handshake type
5885 * 1 . 3 handshake length
5886 * 4 . 6 length of all certs
5887 * 7 . 9 length of cert. 1
5888 * 10 . n-1 peer certificate
5889 * n . n+2 length of cert. 2
5890 * n+3 . ... upper level cert, etc.
5891 */
5892 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894
Paul Bakker29087132010-03-21 21:03:34 +00005895 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 {
5897 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005898 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005901 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 }
5904
5905 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5906 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5907 ssl->out_msg[i + 2] = (unsigned char)( n );
5908
5909 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5910 i += n; crt = crt->next;
5911 }
5912
5913 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5914 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5915 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5916
5917 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5919 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005920
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005921#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005922write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005924
5925 ssl->state++;
5926
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005927 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005930 return( ret );
5931 }
5932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
Paul Bakkered27a042013-04-18 22:46:23 +02005935 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005936}
5937
Hanno Becker84879e32019-01-31 07:44:03 +00005938#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005939
5940#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005941static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5942 unsigned char *crt_buf,
5943 size_t crt_buf_len )
5944{
5945 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5946
5947 if( peer_crt == NULL )
5948 return( -1 );
5949
5950 if( peer_crt->raw.len != crt_buf_len )
5951 return( -1 );
5952
Hanno Becker46f34d02019-02-08 14:00:04 +00005953 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005954}
Hanno Becker177475a2019-02-05 17:02:46 +00005955#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5956static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5957 unsigned char *crt_buf,
5958 size_t crt_buf_len )
5959{
5960 int ret;
5961 unsigned char const * const peer_cert_digest =
5962 ssl->session->peer_cert_digest;
5963 mbedtls_md_type_t const peer_cert_digest_type =
5964 ssl->session->peer_cert_digest_type;
5965 mbedtls_md_info_t const * const digest_info =
5966 mbedtls_md_info_from_type( peer_cert_digest_type );
5967 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5968 size_t digest_len;
5969
5970 if( peer_cert_digest == NULL || digest_info == NULL )
5971 return( -1 );
5972
5973 digest_len = mbedtls_md_get_size( digest_info );
5974 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5975 return( -1 );
5976
5977 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5978 if( ret != 0 )
5979 return( -1 );
5980
5981 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5982}
5983#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005984#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005985
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005986/*
5987 * Once the certificate message is read, parse it into a cert chain and
5988 * perform basic checks, but leave actual verification to the caller
5989 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005990static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5991 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005993 int ret;
5994#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5995 int crt_cnt=0;
5996#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005997 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005998 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006003 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6004 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006006 }
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6009 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006012 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6013 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 }
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006018
Paul Bakker5121ce52009-01-03 21:22:43 +00006019 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006022 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006023
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006024 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006028 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6029 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 }
6032
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006033 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6034 i += 3;
6035
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006036 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 while( i < ssl->in_hslen )
6038 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006039 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006040 if ( i + 3 > ssl->in_hslen ) {
6041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006042 mbedtls_ssl_send_alert_message( ssl,
6043 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6044 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006045 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6046 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006047 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6048 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 if( ssl->in_msg[i] != 0 )
6050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006052 mbedtls_ssl_send_alert_message( ssl,
6053 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6054 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 }
6057
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006058 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6060 | (unsigned int) ssl->in_msg[i + 2];
6061 i += 3;
6062
6063 if( n < 128 || i + n > ssl->in_hslen )
6064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006066 mbedtls_ssl_send_alert_message( ssl,
6067 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6068 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 }
6071
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006072 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006073#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6074 if( crt_cnt++ == 0 &&
6075 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6076 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006077 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006078 /* During client-side renegotiation, check that the server's
6079 * end-CRTs hasn't changed compared to the initial handshake,
6080 * mitigating the triple handshake attack. On success, reuse
6081 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6083 if( ssl_check_peer_crt_unchanged( ssl,
6084 &ssl->in_msg[i],
6085 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006086 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6088 mbedtls_ssl_send_alert_message( ssl,
6089 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6090 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6091 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006092 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006093
6094 /* Now we can safely free the original chain. */
6095 ssl_clear_peer_cert( ssl->session );
6096 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006097#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6098
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006099 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006100#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006101 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006102#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006103 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006104 * it in-place from the input buffer instead of making a copy. */
6105 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6106#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006107 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006109 case 0: /*ok*/
6110 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6111 /* Ignore certificate with an unknown algorithm: maybe a
6112 prior certificate was already trusted. */
6113 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006114
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006115 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6116 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6117 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006118
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006119 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6120 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6121 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006122
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006123 default:
6124 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6125 crt_parse_der_failed:
6126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6127 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6128 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 }
6130
6131 i += n;
6132 }
6133
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006134 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006135 return( 0 );
6136}
6137
Hanno Becker4a55f632019-02-05 12:49:06 +00006138#if defined(MBEDTLS_SSL_SRV_C)
6139static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6140{
6141 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6142 return( -1 );
6143
6144#if defined(MBEDTLS_SSL_PROTO_SSL3)
6145 /*
6146 * Check if the client sent an empty certificate
6147 */
6148 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6149 {
6150 if( ssl->in_msglen == 2 &&
6151 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6152 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6153 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6156 return( 0 );
6157 }
6158
6159 return( -1 );
6160 }
6161#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6162
6163#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6164 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6165 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6166 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6167 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6168 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6169 {
6170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6171 return( 0 );
6172 }
6173
6174 return( -1 );
6175#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6176 MBEDTLS_SSL_PROTO_TLS1_2 */
6177}
6178#endif /* MBEDTLS_SSL_SRV_C */
6179
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006180/* Check if a certificate message is expected.
6181 * Return either
6182 * - SSL_CERTIFICATE_EXPECTED, or
6183 * - SSL_CERTIFICATE_SKIP
6184 * indicating whether a Certificate message is expected or not.
6185 */
6186#define SSL_CERTIFICATE_EXPECTED 0
6187#define SSL_CERTIFICATE_SKIP 1
6188static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6189 int authmode )
6190{
6191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006192 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006193
6194 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6195 return( SSL_CERTIFICATE_SKIP );
6196
6197#if defined(MBEDTLS_SSL_SRV_C)
6198 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6199 {
6200 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6201 return( SSL_CERTIFICATE_SKIP );
6202
6203 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6204 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006205 ssl->session_negotiate->verify_result =
6206 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6207 return( SSL_CERTIFICATE_SKIP );
6208 }
6209 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006210#else
6211 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006212#endif /* MBEDTLS_SSL_SRV_C */
6213
6214 return( SSL_CERTIFICATE_EXPECTED );
6215}
6216
Hanno Becker68636192019-02-05 14:36:34 +00006217static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6218 int authmode,
6219 mbedtls_x509_crt *chain,
6220 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006221{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006222 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006223 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006224 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006225 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006226
Hanno Becker8927c832019-04-03 12:52:50 +01006227 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6228 void *p_vrfy;
6229
Hanno Becker68636192019-02-05 14:36:34 +00006230 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6231 return( 0 );
6232
Hanno Becker8927c832019-04-03 12:52:50 +01006233 if( ssl->f_vrfy != NULL )
6234 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006236 f_vrfy = ssl->f_vrfy;
6237 p_vrfy = ssl->p_vrfy;
6238 }
6239 else
6240 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006242 f_vrfy = ssl->conf->f_vrfy;
6243 p_vrfy = ssl->conf->p_vrfy;
6244 }
6245
Hanno Becker68636192019-02-05 14:36:34 +00006246 /*
6247 * Main check: verify certificate
6248 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006249#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6250 if( ssl->conf->f_ca_cb != NULL )
6251 {
6252 ((void) rs_ctx);
6253 have_ca_chain = 1;
6254
6255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006256 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006257 chain,
6258 ssl->conf->f_ca_cb,
6259 ssl->conf->p_ca_cb,
6260 ssl->conf->cert_profile,
6261 ssl->hostname,
6262 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006263 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006264 }
6265 else
6266#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6267 {
6268 mbedtls_x509_crt *ca_chain;
6269 mbedtls_x509_crl *ca_crl;
6270
6271#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6272 if( ssl->handshake->sni_ca_chain != NULL )
6273 {
6274 ca_chain = ssl->handshake->sni_ca_chain;
6275 ca_crl = ssl->handshake->sni_ca_crl;
6276 }
6277 else
6278#endif
6279 {
6280 ca_chain = ssl->conf->ca_chain;
6281 ca_crl = ssl->conf->ca_crl;
6282 }
6283
6284 if( ca_chain != NULL )
6285 have_ca_chain = 1;
6286
6287 ret = mbedtls_x509_crt_verify_restartable(
6288 chain,
6289 ca_chain, ca_crl,
6290 ssl->conf->cert_profile,
6291 ssl->hostname,
6292 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006293 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006294 }
Hanno Becker68636192019-02-05 14:36:34 +00006295
6296 if( ret != 0 )
6297 {
6298 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6299 }
6300
6301#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6302 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6303 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6304#endif
6305
6306 /*
6307 * Secondary checks: always done, but change 'ret' only if it was 0
6308 */
6309
6310#if defined(MBEDTLS_ECP_C)
6311 {
6312 const mbedtls_pk_context *pk = &chain->pk;
6313
6314 /* If certificate uses an EC key, make sure the curve is OK */
6315 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6316 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6317 {
6318 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6319
6320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6321 if( ret == 0 )
6322 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6323 }
6324 }
6325#endif /* MBEDTLS_ECP_C */
6326
6327 if( mbedtls_ssl_check_cert_usage( chain,
6328 ciphersuite_info,
6329 ! ssl->conf->endpoint,
6330 &ssl->session_negotiate->verify_result ) != 0 )
6331 {
6332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6333 if( ret == 0 )
6334 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6335 }
6336
6337 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6338 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6339 * with details encoded in the verification flags. All other kinds
6340 * of error codes, including those from the user provided f_vrfy
6341 * functions, are treated as fatal and lead to a failure of
6342 * ssl_parse_certificate even if verification was optional. */
6343 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6344 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6345 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6346 {
6347 ret = 0;
6348 }
6349
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006350 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6353 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6354 }
6355
6356 if( ret != 0 )
6357 {
6358 uint8_t alert;
6359
6360 /* The certificate may have been rejected for several reasons.
6361 Pick one and send the corresponding alert. Which alert to send
6362 may be a subject of debate in some cases. */
6363 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6364 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6365 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6366 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6367 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6368 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6369 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6370 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6371 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6372 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6373 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6374 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6375 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6376 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6377 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6378 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6379 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6380 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6381 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6382 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6383 else
6384 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6386 alert );
6387 }
6388
6389#if defined(MBEDTLS_DEBUG_C)
6390 if( ssl->session_negotiate->verify_result != 0 )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6393 ssl->session_negotiate->verify_result ) );
6394 }
6395 else
6396 {
6397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6398 }
6399#endif /* MBEDTLS_DEBUG_C */
6400
6401 return( ret );
6402}
6403
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006404#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6405static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6406 unsigned char *start, size_t len )
6407{
6408 int ret;
6409 /* Remember digest of the peer's end-CRT. */
6410 ssl->session_negotiate->peer_cert_digest =
6411 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6412 if( ssl->session_negotiate->peer_cert_digest == NULL )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6415 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6416 mbedtls_ssl_send_alert_message( ssl,
6417 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6418 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6419
6420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6421 }
6422
6423 ret = mbedtls_md( mbedtls_md_info_from_type(
6424 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6425 start, len,
6426 ssl->session_negotiate->peer_cert_digest );
6427
6428 ssl->session_negotiate->peer_cert_digest_type =
6429 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6430 ssl->session_negotiate->peer_cert_digest_len =
6431 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6432
6433 return( ret );
6434}
6435
6436static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6437 unsigned char *start, size_t len )
6438{
6439 unsigned char *end = start + len;
6440 int ret;
6441
6442 /* Make a copy of the peer's raw public key. */
6443 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6444 ret = mbedtls_pk_parse_subpubkey( &start, end,
6445 &ssl->handshake->peer_pubkey );
6446 if( ret != 0 )
6447 {
6448 /* We should have parsed the public key before. */
6449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6450 }
6451
6452 return( 0 );
6453}
6454#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6455
Hanno Becker68636192019-02-05 14:36:34 +00006456int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6457{
6458 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006459 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006460#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6461 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6462 ? ssl->handshake->sni_authmode
6463 : ssl->conf->authmode;
6464#else
6465 const int authmode = ssl->conf->authmode;
6466#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006467 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006468 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006469
6470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6471
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006472 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6473 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006474 {
6475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006476 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006477 }
6478
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006479#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6480 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006481 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006482 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006483 chain = ssl->handshake->ecrs_peer_cert;
6484 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006485 goto crt_verify;
6486 }
6487#endif
6488
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006489 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006490 {
6491 /* mbedtls_ssl_read_record may have sent an alert already. We
6492 let it decide whether to alert. */
6493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006494 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006495 }
6496
Hanno Becker4a55f632019-02-05 12:49:06 +00006497#if defined(MBEDTLS_SSL_SRV_C)
6498 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6499 {
6500 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006501
6502 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006503 ret = 0;
6504 else
6505 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006506
Hanno Becker6bdfab22019-02-05 13:11:17 +00006507 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006508 }
6509#endif /* MBEDTLS_SSL_SRV_C */
6510
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006511 /* Clear existing peer CRT structure in case we tried to
6512 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006513 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006514
6515 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6516 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006517 {
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6519 sizeof( mbedtls_x509_crt ) ) );
6520 mbedtls_ssl_send_alert_message( ssl,
6521 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6522 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006523
Hanno Becker3dad3112019-02-05 17:19:52 +00006524 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6525 goto exit;
6526 }
6527 mbedtls_x509_crt_init( chain );
6528
6529 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006530 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006531 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006532
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
6537crt_verify:
6538 if( ssl->handshake->ecrs_enabled)
6539 rs_ctx = &ssl->handshake->ecrs_ctx;
6540#endif
6541
Hanno Becker68636192019-02-05 14:36:34 +00006542 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006543 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006544 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006545 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006547#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006548 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006549 unsigned char *crt_start, *pk_start;
6550 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006551
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006552 /* We parse the CRT chain without copying, so
6553 * these pointers point into the input buffer,
6554 * and are hence still valid after freeing the
6555 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006556
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006557 crt_start = chain->raw.p;
6558 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006559
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006560 pk_start = chain->pk_raw.p;
6561 pk_len = chain->pk_raw.len;
6562
6563 /* Free the CRT structures before computing
6564 * digest and copying the peer's public key. */
6565 mbedtls_x509_crt_free( chain );
6566 mbedtls_free( chain );
6567 chain = NULL;
6568
6569 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006570 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006571 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006572
6573 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6574 if( ret != 0 )
6575 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006576 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006577#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6578 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006579 ssl->session_negotiate->peer_cert = chain;
6580 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006581#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
Hanno Becker6bdfab22019-02-05 13:11:17 +00006585exit:
6586
Hanno Becker3dad3112019-02-05 17:19:52 +00006587 if( ret == 0 )
6588 ssl->state++;
6589
6590#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6591 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6592 {
6593 ssl->handshake->ecrs_peer_cert = chain;
6594 chain = NULL;
6595 }
6596#endif
6597
6598 if( chain != NULL )
6599 {
6600 mbedtls_x509_crt_free( chain );
6601 mbedtls_free( chain );
6602 }
6603
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 return( ret );
6605}
Hanno Becker21489932019-02-05 13:20:55 +00006606#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006609{
6610 int ret;
6611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 ssl->out_msglen = 1;
6616 ssl->out_msg[0] = 1;
6617
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 ssl->state++;
6619
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006620 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006623 return( ret );
6624 }
6625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006627
6628 return( 0 );
6629}
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632{
6633 int ret;
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Hanno Becker327c93b2018-08-15 13:56:18 +01006637 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 return( ret );
6641 }
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006646 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 }
6650
Hanno Beckere678eaa2018-08-21 14:57:46 +01006651 /* CCS records are only accepted if they have length 1 and content '1',
6652 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006654 /*
6655 * Switch to our negotiated transform and session parameters for inbound
6656 * data.
6657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006659 ssl->transform_in = ssl->transform_negotiate;
6660 ssl->session_in = ssl->session_negotiate;
6661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006666 ssl_dtls_replay_reset( ssl );
6667#endif
6668
6669 /* Increment epoch */
6670 if( ++ssl->in_epoch == 0 )
6671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006673 /* This is highly unlikely to happen for legitimate reasons, so
6674 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006676 }
6677 }
6678 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006680 memset( ssl->in_ctr, 0, 8 );
6681
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006682 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6685 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006690 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6691 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006693 }
6694 }
6695#endif
6696
Paul Bakker5121ce52009-01-03 21:22:43 +00006697 ssl->state++;
6698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700
6701 return( 0 );
6702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6705 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006706{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006707 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6710 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6711 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006712 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006713 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6716#if defined(MBEDTLS_SHA512_C)
6717 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006718 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6719 else
6720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_SHA256_C)
6722 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006723 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006724 else
6725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006729 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006730 }
Paul Bakker380da532012-04-18 16:10:25 +00006731}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6736 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6738 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6741#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006743 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006744 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6745#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006746 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006747#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006750#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006751 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006752 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006753#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006754 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006755#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006758}
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006761 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006762{
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)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006765 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6766 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6769#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006770#if defined(MBEDTLS_USE_PSA_CRYPTO)
6771 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6772#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006773 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006774#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006777#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006778 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006779#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006780 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006781#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006784}
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6787 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6788static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006789 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006790{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006791 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6792 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006793}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006794#endif
Paul Bakker380da532012-04-18 16:10:25 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6797#if defined(MBEDTLS_SHA256_C)
6798static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006799 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006800{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006801#if defined(MBEDTLS_USE_PSA_CRYPTO)
6802 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6803#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006804 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006805#endif
Paul Bakker380da532012-04-18 16:10:25 +00006806}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006807#endif
Paul Bakker380da532012-04-18 16:10:25 +00006808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SHA512_C)
6810static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006811 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006812{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006813#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006814 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006815#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006816 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006817#endif
Paul Bakker380da532012-04-18 16:10:25 +00006818}
Paul Bakker769075d2012-11-24 11:26:46 +01006819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006823static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006826 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006827 mbedtls_md5_context md5;
6828 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006829
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 unsigned char padbuf[48];
6831 unsigned char md5sum[16];
6832 unsigned char sha1sum[20];
6833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006835 if( !session )
6836 session = ssl->session;
6837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006840 mbedtls_md5_init( &md5 );
6841 mbedtls_sha1_init( &sha1 );
6842
6843 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6844 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006845
6846 /*
6847 * SSLv3:
6848 * hash =
6849 * MD5( master + pad2 +
6850 * MD5( handshake + sender + master + pad1 ) )
6851 * + SHA1( master + pad2 +
6852 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 */
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006856 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6857 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006861 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6862 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006866 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Paul Bakker1ef83d62012-04-11 12:09:53 +00006868 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006869
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006870 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6871 mbedtls_md5_update_ret( &md5, session->master, 48 );
6872 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6873 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006874
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006875 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6876 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6877 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6878 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006879
Paul Bakker1ef83d62012-04-11 12:09:53 +00006880 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006881
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006882 mbedtls_md5_starts_ret( &md5 );
6883 mbedtls_md5_update_ret( &md5, session->master, 48 );
6884 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6885 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6886 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006887
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006888 mbedtls_sha1_starts_ret( &sha1 );
6889 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6890 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6891 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6892 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006895
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006896 mbedtls_md5_free( &md5 );
6897 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006898
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006899 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6900 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6901 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006908static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006910{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006912 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 mbedtls_md5_context md5;
6914 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006915 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006918 if( !session )
6919 session = ssl->session;
6920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006922
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006923 mbedtls_md5_init( &md5 );
6924 mbedtls_sha1_init( &sha1 );
6925
6926 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6927 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006928
Paul Bakker1ef83d62012-04-11 12:09:53 +00006929 /*
6930 * TLSv1:
6931 * hash = PRF( master, finished_label,
6932 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6933 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006936 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6937 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006938#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006941 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6942 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006943#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006946 ? "client finished"
6947 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006948
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006949 mbedtls_md5_finish_ret( &md5, padbuf );
6950 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006952 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006953 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006956
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006957 mbedtls_md5_free( &md5 );
6958 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006959
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006960 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6967#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006968static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006970{
6971 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006972 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006973 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006974#if defined(MBEDTLS_USE_PSA_CRYPTO)
6975 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006976 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006977 psa_status_t status;
6978#else
6979 mbedtls_sha256_context sha256;
6980#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006983 if( !session )
6984 session = ssl->session;
6985
Andrzej Kurekeb342242019-01-29 09:14:33 -05006986 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6987 ? "client finished"
6988 : "server finished";
6989
6990#if defined(MBEDTLS_USE_PSA_CRYPTO)
6991 sha256_psa = psa_hash_operation_init();
6992
6993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6994
6995 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6996 if( status != PSA_SUCCESS )
6997 {
6998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6999 return;
7000 }
7001
7002 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7003 if( status != PSA_SUCCESS )
7004 {
7005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7006 return;
7007 }
7008 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7009#else
7010
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007011 mbedtls_sha256_init( &sha256 );
7012
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007015 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007016
7017 /*
7018 * TLSv1.2:
7019 * hash = PRF( master, finished_label,
7020 * Hash( handshake ) )[0.11]
7021 */
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if !defined(MBEDTLS_SHA256_ALT)
7024 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007025 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007026#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007027
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007028 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007029 mbedtls_sha256_free( &sha256 );
7030#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007031
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007032 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007033 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007036
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007037 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007044static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007046{
7047 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007048 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007049 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007050#if defined(MBEDTLS_USE_PSA_CRYPTO)
7051 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007052 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007053 psa_status_t status;
7054#else
7055 mbedtls_sha512_context sha512;
7056#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007059 if( !session )
7060 session = ssl->session;
7061
Andrzej Kurekeb342242019-01-29 09:14:33 -05007062 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7063 ? "client finished"
7064 : "server finished";
7065
7066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007067 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007068
7069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7070
Andrzej Kurek972fba52019-01-30 03:29:12 -05007071 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007072 if( status != PSA_SUCCESS )
7073 {
7074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7075 return;
7076 }
7077
Andrzej Kurek972fba52019-01-30 03:29:12 -05007078 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007079 if( status != PSA_SUCCESS )
7080 {
7081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7082 return;
7083 }
7084 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7085#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007086 mbedtls_sha512_init( &sha512 );
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007089
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007090 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007091
7092 /*
7093 * TLSv1.2:
7094 * hash = PRF( master, finished_label,
7095 * Hash( handshake ) )[0.11]
7096 */
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007099 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7100 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007101#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007102
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007103 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104 mbedtls_sha512_free( &sha512 );
7105#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007106
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007107 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007108 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007112 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#endif /* MBEDTLS_SHA512_C */
7117#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007122
7123 /*
7124 * Free our handshake params
7125 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007126 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007128 ssl->handshake = NULL;
7129
7130 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007131 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007132 */
7133 if( ssl->transform )
7134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 mbedtls_ssl_transform_free( ssl->transform );
7136 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007137 }
7138 ssl->transform = ssl->transform_negotiate;
7139 ssl->transform_negotiate = NULL;
7140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007142}
7143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007145{
7146 int resume = ssl->handshake->resume;
7147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_RENEGOTIATION)
7151 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007154 ssl->renego_records_seen = 0;
7155 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007156#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007157
7158 /*
7159 * Free the previous session and switch in the current one
7160 */
Paul Bakker0a597072012-09-25 21:55:46 +00007161 if( ssl->session )
7162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007164 /* RFC 7366 3.1: keep the EtM state */
7165 ssl->session_negotiate->encrypt_then_mac =
7166 ssl->session->encrypt_then_mac;
7167#endif
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 mbedtls_ssl_session_free( ssl->session );
7170 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007171 }
7172 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007173 ssl->session_negotiate = NULL;
7174
Paul Bakker0a597072012-09-25 21:55:46 +00007175 /*
7176 * Add cache entry
7177 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007178 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007179 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007180 resume == 0 )
7181 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007182 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007184 }
Paul Bakker0a597072012-09-25 21:55:46 +00007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007188 ssl->handshake->flight != NULL )
7189 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007190 /* Cancel handshake timer */
7191 ssl_set_timer( ssl, 0 );
7192
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007193 /* Keep last flight around in case we need to resend it:
7194 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007196 }
7197 else
7198#endif
7199 ssl_handshake_wrapup_free_hs_transform( ssl );
7200
Paul Bakker48916f92012-09-16 19:57:18 +00007201 ssl->state++;
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007204}
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007207{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007208 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007211
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007212 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007214 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007215
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007216 /*
7217 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7218 * may define some other value. Currently (early 2016), no defined
7219 * ciphersuite does this (and this is unlikely to change as activity has
7220 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007225 ssl->verify_data_len = hash_len;
7226 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007227#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007228
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7231 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
7233 /*
7234 * In case of session resuming, invert the client and server
7235 * ChangeCipherSpec messages order.
7236 */
Paul Bakker0a597072012-09-25 21:55:46 +00007237 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007240 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007244 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007247 }
7248 else
7249 ssl->state++;
7250
Paul Bakker48916f92012-09-16 19:57:18 +00007251 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007252 * Switch to our negotiated transform and session parameters for outbound
7253 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007259 {
7260 unsigned char i;
7261
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007262 /* Remember current epoch settings for resending */
7263 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007264 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007265
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007266 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007267 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007268
7269 /* Increment epoch */
7270 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007271 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007272 break;
7273
7274 /* The loop goes to its end iff the counter is wrapping */
7275 if( i == 0 )
7276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7278 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007279 }
7280 }
7281 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007283 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007284
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007285 ssl->transform_out = ssl->transform_negotiate;
7286 ssl->session_out = ssl->session_negotiate;
7287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7289 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7294 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007295 }
7296 }
7297#endif
7298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007302#endif
7303
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007304 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007305 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007307 return( ret );
7308 }
7309
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007310#if defined(MBEDTLS_SSL_PROTO_DTLS)
7311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7312 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7313 {
7314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7315 return( ret );
7316 }
7317#endif
7318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007320
7321 return( 0 );
7322}
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007325#define SSL_MAX_HASH_LEN 36
7326#else
7327#define SSL_MAX_HASH_LEN 12
7328#endif
7329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007331{
Paul Bakker23986e52011-04-24 08:57:21 +00007332 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007333 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007334 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007337
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007338 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Hanno Becker327c93b2018-08-15 13:56:18 +01007340 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 return( ret );
7344 }
7345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007349 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7350 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007352 }
7353
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007354 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#if defined(MBEDTLS_SSL_PROTO_SSL3)
7356 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007357 hash_len = 36;
7358 else
7359#endif
7360 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7363 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007366 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7367 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369 }
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007372 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7376 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007378 }
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007381 ssl->verify_data_len = hash_len;
7382 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007383#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007384
Paul Bakker0a597072012-09-25 21:55:46 +00007385 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007392 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007394#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007395 }
7396 else
7397 ssl->state++;
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007402#endif
7403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007405
7406 return( 0 );
7407}
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7414 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7415 mbedtls_md5_init( &handshake->fin_md5 );
7416 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007417 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7418 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7421#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007422#if defined(MBEDTLS_USE_PSA_CRYPTO)
7423 handshake->fin_sha256_psa = psa_hash_operation_init();
7424 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7425#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007427 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007428#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007431#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007432 handshake->fin_sha384_psa = psa_hash_operation_init();
7433 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007434#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007436 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007440
7441 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007442
7443#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7444 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7445 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7446#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448#if defined(MBEDTLS_DHM_C)
7449 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_ECDH_C)
7452 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007453#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007454#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007455 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007456#if defined(MBEDTLS_SSL_CLI_C)
7457 handshake->ecjpake_cache = NULL;
7458 handshake->ecjpake_cache_len = 0;
7459#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007460#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007461
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007462#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007463 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007464#endif
7465
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007466#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7467 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7468#endif
Hanno Becker75173122019-02-06 16:18:31 +00007469
7470#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7471 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7472 mbedtls_pk_init( &handshake->peer_pubkey );
7473#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007474}
7475
Hanno Beckera18d1322018-01-03 14:27:32 +00007476void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7481 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007482
Hanno Beckerd56ed242018-01-03 15:32:51 +00007483#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 mbedtls_md_init( &transform->md_ctx_enc );
7485 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007486#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007487}
7488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007492}
7493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007495{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007496 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007497 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007499 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007501 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007502 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007503
7504 /*
7505 * Either the pointers are now NULL or cleared properly and can be freed.
7506 * Now allocate missing structures.
7507 */
7508 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007509 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007510 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007511 }
Paul Bakker48916f92012-09-16 19:57:18 +00007512
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007513 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007514 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007515 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007516 }
Paul Bakker48916f92012-09-16 19:57:18 +00007517
Paul Bakker82788fb2014-10-20 13:59:19 +02007518 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007519 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007520 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007521 }
Paul Bakker48916f92012-09-16 19:57:18 +00007522
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007523 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007524 if( ssl->handshake == NULL ||
7525 ssl->transform_negotiate == NULL ||
7526 ssl->session_negotiate == NULL )
7527 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 mbedtls_free( ssl->handshake );
7531 mbedtls_free( ssl->transform_negotiate );
7532 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007533
7534 ssl->handshake = NULL;
7535 ssl->transform_negotiate = NULL;
7536 ssl->session_negotiate = NULL;
7537
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007538 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007539 }
7540
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007541 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007543 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007544 ssl_handshake_params_init( ssl->handshake );
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007547 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7548 {
7549 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007550
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007551 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7552 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7553 else
7554 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007555
7556 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007557 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007558#endif
7559
Paul Bakker48916f92012-09-16 19:57:18 +00007560 return( 0 );
7561}
7562
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007563#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007564/* Dummy cookie callbacks for defaults */
7565static int ssl_cookie_write_dummy( void *ctx,
7566 unsigned char **p, unsigned char *end,
7567 const unsigned char *cli_id, size_t cli_id_len )
7568{
7569 ((void) ctx);
7570 ((void) p);
7571 ((void) end);
7572 ((void) cli_id);
7573 ((void) cli_id_len);
7574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007576}
7577
7578static int ssl_cookie_check_dummy( void *ctx,
7579 const unsigned char *cookie, size_t cookie_len,
7580 const unsigned char *cli_id, size_t cli_id_len )
7581{
7582 ((void) ctx);
7583 ((void) cookie);
7584 ((void) cookie_len);
7585 ((void) cli_id);
7586 ((void) cli_id_len);
7587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007589}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007590#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007591
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007592/* Once ssl->out_hdr as the address of the beginning of the
7593 * next outgoing record is set, deduce the other pointers.
7594 *
7595 * Note: For TLS, we save the implicit record sequence number
7596 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7597 * and the caller has to make sure there's space for this.
7598 */
7599
7600static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7601 mbedtls_ssl_transform *transform )
7602{
7603#if defined(MBEDTLS_SSL_PROTO_DTLS)
7604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7605 {
7606 ssl->out_ctr = ssl->out_hdr + 3;
7607 ssl->out_len = ssl->out_hdr + 11;
7608 ssl->out_iv = ssl->out_hdr + 13;
7609 }
7610 else
7611#endif
7612 {
7613 ssl->out_ctr = ssl->out_hdr - 8;
7614 ssl->out_len = ssl->out_hdr + 3;
7615 ssl->out_iv = ssl->out_hdr + 5;
7616 }
7617
7618 /* Adjust out_msg to make space for explicit IV, if used. */
7619 if( transform != NULL &&
7620 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7621 {
7622 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7623 }
7624 else
7625 ssl->out_msg = ssl->out_iv;
7626}
7627
7628/* Once ssl->in_hdr as the address of the beginning of the
7629 * next incoming record is set, deduce the other pointers.
7630 *
7631 * Note: For TLS, we save the implicit record sequence number
7632 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7633 * and the caller has to make sure there's space for this.
7634 */
7635
7636static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7637 mbedtls_ssl_transform *transform )
7638{
7639#if defined(MBEDTLS_SSL_PROTO_DTLS)
7640 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7641 {
7642 ssl->in_ctr = ssl->in_hdr + 3;
7643 ssl->in_len = ssl->in_hdr + 11;
7644 ssl->in_iv = ssl->in_hdr + 13;
7645 }
7646 else
7647#endif
7648 {
7649 ssl->in_ctr = ssl->in_hdr - 8;
7650 ssl->in_len = ssl->in_hdr + 3;
7651 ssl->in_iv = ssl->in_hdr + 5;
7652 }
7653
7654 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7655 if( transform != NULL &&
7656 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7657 {
7658 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7659 }
7660 else
7661 ssl->in_msg = ssl->in_iv;
7662}
7663
Paul Bakker5121ce52009-01-03 21:22:43 +00007664/*
7665 * Initialize an SSL context
7666 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007667void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7668{
7669 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7670}
7671
7672/*
7673 * Setup an SSL context
7674 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007675
7676static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7677{
7678 /* Set the incoming and outgoing record pointers. */
7679#if defined(MBEDTLS_SSL_PROTO_DTLS)
7680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7681 {
7682 ssl->out_hdr = ssl->out_buf;
7683 ssl->in_hdr = ssl->in_buf;
7684 }
7685 else
7686#endif /* MBEDTLS_SSL_PROTO_DTLS */
7687 {
7688 ssl->out_hdr = ssl->out_buf + 8;
7689 ssl->in_hdr = ssl->in_buf + 8;
7690 }
7691
7692 /* Derive other internal pointers. */
7693 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7694 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7695}
7696
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007697int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007698 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007699{
Paul Bakker48916f92012-09-16 19:57:18 +00007700 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007702 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007703
7704 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007705 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007706 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007707
7708 /* Set to NULL in case of an error condition */
7709 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007710
Angus Grattond8213d02016-05-25 20:56:48 +10007711 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7712 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 {
Angus Grattond8213d02016-05-25 20:56:48 +10007714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007715 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007716 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007717 }
7718
7719 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7720 if( ssl->out_buf == NULL )
7721 {
7722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007723 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007724 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 }
7726
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007727 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007728
Paul Bakker48916f92012-09-16 19:57:18 +00007729 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007730 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007731
7732 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007733
7734error:
7735 mbedtls_free( ssl->in_buf );
7736 mbedtls_free( ssl->out_buf );
7737
7738 ssl->conf = NULL;
7739
7740 ssl->in_buf = NULL;
7741 ssl->out_buf = NULL;
7742
7743 ssl->in_hdr = NULL;
7744 ssl->in_ctr = NULL;
7745 ssl->in_len = NULL;
7746 ssl->in_iv = NULL;
7747 ssl->in_msg = NULL;
7748
7749 ssl->out_hdr = NULL;
7750 ssl->out_ctr = NULL;
7751 ssl->out_len = NULL;
7752 ssl->out_iv = NULL;
7753 ssl->out_msg = NULL;
7754
k-stachowiak9f7798e2018-07-31 16:52:32 +02007755 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007756}
7757
7758/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007759 * Reset an initialized and used SSL context for re-use while retaining
7760 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007761 *
7762 * If partial is non-zero, keep data in the input buffer and client ID.
7763 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007764 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007765static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007766{
Paul Bakker48916f92012-09-16 19:57:18 +00007767 int ret;
7768
Hanno Becker7e772132018-08-10 12:38:21 +01007769#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7770 !defined(MBEDTLS_SSL_SRV_C)
7771 ((void) partial);
7772#endif
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007775
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007776 /* Cancel any possibly running timer */
7777 ssl_set_timer( ssl, 0 );
7778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779#if defined(MBEDTLS_SSL_RENEGOTIATION)
7780 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007781 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007782
7783 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7785 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007788
Paul Bakker7eb013f2011-10-06 12:37:39 +00007789 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007790 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007791
7792 ssl->in_msgtype = 0;
7793 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007795 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007796 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007799 ssl_dtls_replay_reset( ssl );
7800#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007801
7802 ssl->in_hslen = 0;
7803 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007804
7805 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007806
7807 ssl->out_msgtype = 0;
7808 ssl->out_msglen = 0;
7809 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7811 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007812 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007813#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007814
Hanno Becker19859472018-08-06 09:40:20 +01007815 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7816
Paul Bakker48916f92012-09-16 19:57:18 +00007817 ssl->transform_in = NULL;
7818 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007819
Hanno Becker78640902018-08-13 16:35:15 +01007820 ssl->session_in = NULL;
7821 ssl->session_out = NULL;
7822
Angus Grattond8213d02016-05-25 20:56:48 +10007823 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007824
7825#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007826 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007827#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7828 {
7829 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007830 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007831 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7834 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7837 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7840 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007841 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007842 }
7843#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007844
Paul Bakker48916f92012-09-16 19:57:18 +00007845 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 mbedtls_ssl_transform_free( ssl->transform );
7848 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007849 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007850 }
Paul Bakker48916f92012-09-16 19:57:18 +00007851
Paul Bakkerc0463502013-02-14 11:19:38 +01007852 if( ssl->session )
7853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 mbedtls_ssl_session_free( ssl->session );
7855 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007856 ssl->session = NULL;
7857 }
7858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007860 ssl->alpn_chosen = NULL;
7861#endif
7862
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007863#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007864#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007865 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007866#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007867 {
7868 mbedtls_free( ssl->cli_id );
7869 ssl->cli_id = NULL;
7870 ssl->cli_id_len = 0;
7871 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007872#endif
7873
Paul Bakker48916f92012-09-16 19:57:18 +00007874 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7875 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007876
7877 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007878}
7879
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007880/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007881 * Reset an initialized and used SSL context for re-use while retaining
7882 * all application-set variables, function pointers and data.
7883 */
7884int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7885{
7886 return( ssl_session_reset_int( ssl, 0 ) );
7887}
7888
7889/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007890 * SSL set accessors
7891 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007892void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007893{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007894 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007895}
7896
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007897void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007898{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007899 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007900}
7901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007903void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007904{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007905 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007906}
7907#endif
7908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007910void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007912 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007913}
7914#endif
7915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007917
Hanno Becker1841b0a2018-08-24 11:13:57 +01007918void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7919 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007920{
7921 ssl->disable_datagram_packing = !allow_packing;
7922}
7923
7924void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7925 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007926{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007927 conf->hs_timeout_min = min;
7928 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007929}
7930#endif
7931
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007932void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007934 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007935}
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007939 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007940 void *p_vrfy )
7941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007942 conf->f_vrfy = f_vrfy;
7943 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007946
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007947void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007948 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007949 void *p_rng )
7950{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007951 conf->f_rng = f_rng;
7952 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007953}
7954
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007955void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007956 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007957 void *p_dbg )
7958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007959 conf->f_dbg = f_dbg;
7960 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007961}
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007964 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007965 mbedtls_ssl_send_t *f_send,
7966 mbedtls_ssl_recv_t *f_recv,
7967 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007968{
7969 ssl->p_bio = p_bio;
7970 ssl->f_send = f_send;
7971 ssl->f_recv = f_recv;
7972 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007973}
7974
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007975#if defined(MBEDTLS_SSL_PROTO_DTLS)
7976void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7977{
7978 ssl->mtu = mtu;
7979}
7980#endif
7981
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007982void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007983{
7984 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007985}
7986
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007987void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7988 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007989 mbedtls_ssl_set_timer_t *f_set_timer,
7990 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007991{
7992 ssl->p_timer = p_timer;
7993 ssl->f_set_timer = f_set_timer;
7994 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007995
7996 /* Make sure we start with no timer running */
7997 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007998}
7999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008001void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008002 void *p_cache,
8003 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8004 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008005{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008006 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008007 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008008 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_SSL_CLI_C)
8013int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008014{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008015 int ret;
8016
8017 if( ssl == NULL ||
8018 session == NULL ||
8019 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008020 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008023 }
8024
Hanno Becker52055ae2019-02-06 14:30:46 +00008025 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8026 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008027 return( ret );
8028
Paul Bakker0a597072012-09-25 21:55:46 +00008029 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008030
8031 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008034
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008035void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008036 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008037{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008038 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8039 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8040 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8041 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008042}
8043
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008044void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008045 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008046 int major, int minor )
8047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008049 return;
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008052 return;
8053
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008054 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008055}
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008058void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008059 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008060{
8061 conf->cert_profile = profile;
8062}
8063
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008064/* Append a new keycert entry to a (possibly empty) list */
8065static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8066 mbedtls_x509_crt *cert,
8067 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008068{
niisato8ee24222018-06-25 19:05:48 +09008069 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008070
niisato8ee24222018-06-25 19:05:48 +09008071 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8072 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008074
niisato8ee24222018-06-25 19:05:48 +09008075 new_cert->cert = cert;
8076 new_cert->key = key;
8077 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008078
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008079 /* Update head is the list was null, else add to the end */
8080 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008081 {
niisato8ee24222018-06-25 19:05:48 +09008082 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008083 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008084 else
8085 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008086 mbedtls_ssl_key_cert *cur = *head;
8087 while( cur->next != NULL )
8088 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008089 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008090 }
8091
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008092 return( 0 );
8093}
8094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008095int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008096 mbedtls_x509_crt *own_cert,
8097 mbedtls_pk_context *pk_key )
8098{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008099 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008100}
8101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008102void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008103 mbedtls_x509_crt *ca_chain,
8104 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008105{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008106 conf->ca_chain = ca_chain;
8107 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008108
8109#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8110 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8111 * cannot be used together. */
8112 conf->f_ca_cb = NULL;
8113 conf->p_ca_cb = NULL;
8114#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008115}
Hanno Becker5adaad92019-03-27 16:54:37 +00008116
8117#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8118void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008119 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008120 void *p_ca_cb )
8121{
8122 conf->f_ca_cb = f_ca_cb;
8123 conf->p_ca_cb = p_ca_cb;
8124
8125 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8126 * cannot be used together. */
8127 conf->ca_chain = NULL;
8128 conf->ca_crl = NULL;
8129}
8130#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008132
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008133#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8134int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8135 mbedtls_x509_crt *own_cert,
8136 mbedtls_pk_context *pk_key )
8137{
8138 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8139 own_cert, pk_key ) );
8140}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008141
8142void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8143 mbedtls_x509_crt *ca_chain,
8144 mbedtls_x509_crl *ca_crl )
8145{
8146 ssl->handshake->sni_ca_chain = ca_chain;
8147 ssl->handshake->sni_ca_crl = ca_crl;
8148}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008149
8150void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8151 int authmode )
8152{
8153 ssl->handshake->sni_authmode = authmode;
8154}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008155#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8156
Hanno Becker8927c832019-04-03 12:52:50 +01008157#if defined(MBEDTLS_X509_CRT_PARSE_C)
8158void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8159 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8160 void *p_vrfy )
8161{
8162 ssl->f_vrfy = f_vrfy;
8163 ssl->p_vrfy = p_vrfy;
8164}
8165#endif
8166
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008167#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008168/*
8169 * Set EC J-PAKE password for current handshake
8170 */
8171int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8172 const unsigned char *pw,
8173 size_t pw_len )
8174{
8175 mbedtls_ecjpake_role role;
8176
Janos Follath8eb64132016-06-03 15:40:57 +01008177 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8179
8180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8181 role = MBEDTLS_ECJPAKE_SERVER;
8182 else
8183 role = MBEDTLS_ECJPAKE_CLIENT;
8184
8185 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8186 role,
8187 MBEDTLS_MD_SHA256,
8188 MBEDTLS_ECP_DP_SECP256R1,
8189 pw, pw_len ) );
8190}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008191#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008194
8195static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8196{
8197 /* Remove reference to existing PSK, if any. */
8198#if defined(MBEDTLS_USE_PSA_CRYPTO)
8199 if( conf->psk_opaque != 0 )
8200 {
8201 /* The maintenance of the PSK key slot is the
8202 * user's responsibility. */
8203 conf->psk_opaque = 0;
8204 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008205 /* This and the following branch should never
8206 * be taken simultaenously as we maintain the
8207 * invariant that raw and opaque PSKs are never
8208 * configured simultaneously. As a safeguard,
8209 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008210#endif /* MBEDTLS_USE_PSA_CRYPTO */
8211 if( conf->psk != NULL )
8212 {
8213 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8214
8215 mbedtls_free( conf->psk );
8216 conf->psk = NULL;
8217 conf->psk_len = 0;
8218 }
8219
8220 /* Remove reference to PSK identity, if any. */
8221 if( conf->psk_identity != NULL )
8222 {
8223 mbedtls_free( conf->psk_identity );
8224 conf->psk_identity = NULL;
8225 conf->psk_identity_len = 0;
8226 }
8227}
8228
Hanno Becker7390c712018-11-15 13:33:04 +00008229/* This function assumes that PSK identity in the SSL config is unset.
8230 * It checks that the provided identity is well-formed and attempts
8231 * to make a copy of it in the SSL config.
8232 * On failure, the PSK identity in the config remains unset. */
8233static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8234 unsigned char const *psk_identity,
8235 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008236{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008237 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008238 if( psk_identity == NULL ||
8239 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008240 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008241 {
8242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8243 }
8244
Hanno Becker7390c712018-11-15 13:33:04 +00008245 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8246 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008248
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008249 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008250 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008251
8252 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008253}
8254
Hanno Becker7390c712018-11-15 13:33:04 +00008255int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8256 const unsigned char *psk, size_t psk_len,
8257 const unsigned char *psk_identity, size_t psk_identity_len )
8258{
8259 int ret;
8260 /* Remove opaque/raw PSK + PSK Identity */
8261 ssl_conf_remove_psk( conf );
8262
8263 /* Check and set raw PSK */
8264 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8266 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8268 conf->psk_len = psk_len;
8269 memcpy( conf->psk, psk, conf->psk_len );
8270
8271 /* Check and set PSK Identity */
8272 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8273 if( ret != 0 )
8274 ssl_conf_remove_psk( conf );
8275
8276 return( ret );
8277}
8278
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008279static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8280{
8281#if defined(MBEDTLS_USE_PSA_CRYPTO)
8282 if( ssl->handshake->psk_opaque != 0 )
8283 {
8284 ssl->handshake->psk_opaque = 0;
8285 }
8286 else
8287#endif /* MBEDTLS_USE_PSA_CRYPTO */
8288 if( ssl->handshake->psk != NULL )
8289 {
8290 mbedtls_platform_zeroize( ssl->handshake->psk,
8291 ssl->handshake->psk_len );
8292 mbedtls_free( ssl->handshake->psk );
8293 ssl->handshake->psk_len = 0;
8294 }
8295}
8296
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008297int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8298 const unsigned char *psk, size_t psk_len )
8299{
8300 if( psk == NULL || ssl->handshake == NULL )
8301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302
8303 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8305
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008306 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008307
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008308 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008309 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008310
8311 ssl->handshake->psk_len = psk_len;
8312 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8313
8314 return( 0 );
8315}
8316
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008317#if defined(MBEDTLS_USE_PSA_CRYPTO)
8318int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008319 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008320 const unsigned char *psk_identity,
8321 size_t psk_identity_len )
8322{
Hanno Becker7390c712018-11-15 13:33:04 +00008323 int ret;
8324 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008325 ssl_conf_remove_psk( conf );
8326
Hanno Becker7390c712018-11-15 13:33:04 +00008327 /* Check and set opaque PSK */
8328 if( psk_slot == 0 )
8329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008330 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008331
8332 /* Check and set PSK Identity */
8333 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8334 psk_identity_len );
8335 if( ret != 0 )
8336 ssl_conf_remove_psk( conf );
8337
8338 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008339}
8340
8341int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008342 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008343{
8344 if( psk_slot == 0 || ssl->handshake == NULL )
8345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8346
8347 ssl_remove_psk( ssl );
8348 ssl->handshake->psk_opaque = psk_slot;
8349 return( 0 );
8350}
8351#endif /* MBEDTLS_USE_PSA_CRYPTO */
8352
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008353void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008355 size_t),
8356 void *p_psk )
8357{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008358 conf->f_psk = f_psk;
8359 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008362
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008363#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008364
8365#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008366int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008367{
8368 int ret;
8369
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008370 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8371 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8372 {
8373 mbedtls_mpi_free( &conf->dhm_P );
8374 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008375 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008377
8378 return( 0 );
8379}
Hanno Becker470a8c42017-10-04 15:28:46 +01008380#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008381
Hanno Beckera90658f2017-10-04 15:29:08 +01008382int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8383 const unsigned char *dhm_P, size_t P_len,
8384 const unsigned char *dhm_G, size_t G_len )
8385{
8386 int ret;
8387
8388 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8389 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8390 {
8391 mbedtls_mpi_free( &conf->dhm_P );
8392 mbedtls_mpi_free( &conf->dhm_G );
8393 return( ret );
8394 }
8395
8396 return( 0 );
8397}
Paul Bakker5121ce52009-01-03 21:22:43 +00008398
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008399int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008400{
8401 int ret;
8402
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008403 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8404 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8405 {
8406 mbedtls_mpi_free( &conf->dhm_P );
8407 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008408 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008409 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008410
8411 return( 0 );
8412}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008413#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008414
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008415#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8416/*
8417 * Set the minimum length for Diffie-Hellman parameters
8418 */
8419void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8420 unsigned int bitlen )
8421{
8422 conf->dhm_min_bitlen = bitlen;
8423}
8424#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8425
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008426#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008427/*
8428 * Set allowed/preferred hashes for handshake signatures
8429 */
8430void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8431 const int *hashes )
8432{
8433 conf->sig_hashes = hashes;
8434}
Hanno Becker947194e2017-04-07 13:25:49 +01008435#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008436
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008437#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008438/*
8439 * Set the allowed elliptic curves
8440 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008441void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008442 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008444 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008445}
Hanno Becker947194e2017-04-07 13:25:49 +01008446#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008447
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008448#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008450{
Hanno Becker947194e2017-04-07 13:25:49 +01008451 /* Initialize to suppress unnecessary compiler warning */
8452 size_t hostname_len = 0;
8453
8454 /* Check if new hostname is valid before
8455 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008456 if( hostname != NULL )
8457 {
8458 hostname_len = strlen( hostname );
8459
8460 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8462 }
8463
8464 /* Now it's clear that we will overwrite the old hostname,
8465 * so we can free it safely */
8466
8467 if( ssl->hostname != NULL )
8468 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008469 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008470 mbedtls_free( ssl->hostname );
8471 }
8472
8473 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008474
Paul Bakker5121ce52009-01-03 21:22:43 +00008475 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008476 {
8477 ssl->hostname = NULL;
8478 }
8479 else
8480 {
8481 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008482 if( ssl->hostname == NULL )
8483 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008484
Hanno Becker947194e2017-04-07 13:25:49 +01008485 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008486
Hanno Becker947194e2017-04-07 13:25:49 +01008487 ssl->hostname[hostname_len] = '\0';
8488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008489
8490 return( 0 );
8491}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008492#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008493
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008494#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008497 const unsigned char *, size_t),
8498 void *p_sni )
8499{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008500 conf->f_sni = f_sni;
8501 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008507{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008508 size_t cur_len, tot_len;
8509 const char **p;
8510
8511 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008512 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8513 * MUST NOT be truncated."
8514 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008515 */
8516 tot_len = 0;
8517 for( p = protos; *p != NULL; p++ )
8518 {
8519 cur_len = strlen( *p );
8520 tot_len += cur_len;
8521
8522 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008524 }
8525
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008526 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008527
8528 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008529}
8530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008532{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008533 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008536
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008537void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008539 conf->max_major_ver = major;
8540 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008541}
8542
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008543void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008545 conf->min_major_ver = major;
8546 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008547}
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008551{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008552 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008553}
8554#endif
8555
Janos Follath088ce432017-04-10 12:42:31 +01008556#if defined(MBEDTLS_SSL_SRV_C)
8557void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8558 char cert_req_ca_list )
8559{
8560 conf->cert_req_ca_list = cert_req_ca_list;
8561}
8562#endif
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008565void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008567 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008568}
8569#endif
8570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008575}
8576#endif
8577
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008578#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008581 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008582}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008583#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008586int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008589 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008592 }
8593
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008594 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008595
8596 return( 0 );
8597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008601void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008603 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008608void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008609{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008610 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008611}
8612#endif
8613
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008616 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008617}
8618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008623}
8624
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008625void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008627 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008628}
8629
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008630void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008631 const unsigned char period[8] )
8632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008633 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008638#if defined(MBEDTLS_SSL_CLI_C)
8639void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008640{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008641 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008642}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008643#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008644
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008646void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8647 mbedtls_ssl_ticket_write_t *f_ticket_write,
8648 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8649 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008650{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008651 conf->f_ticket_write = f_ticket_write;
8652 conf->f_ticket_parse = f_ticket_parse;
8653 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008654}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008655#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008657
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008658#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8659void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8660 mbedtls_ssl_export_keys_t *f_export_keys,
8661 void *p_export_keys )
8662{
8663 conf->f_export_keys = f_export_keys;
8664 conf->p_export_keys = p_export_keys;
8665}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008666
8667void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8668 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8669 void *p_export_keys )
8670{
8671 conf->f_export_keys_ext = f_export_keys_ext;
8672 conf->p_export_keys = p_export_keys;
8673}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008674#endif
8675
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008676#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008677void mbedtls_ssl_conf_async_private_cb(
8678 mbedtls_ssl_config *conf,
8679 mbedtls_ssl_async_sign_t *f_async_sign,
8680 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8681 mbedtls_ssl_async_resume_t *f_async_resume,
8682 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008683 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008684{
8685 conf->f_async_sign_start = f_async_sign;
8686 conf->f_async_decrypt_start = f_async_decrypt;
8687 conf->f_async_resume = f_async_resume;
8688 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008689 conf->p_async_config_data = async_config_data;
8690}
8691
Gilles Peskine8f97af72018-04-26 11:46:10 +02008692void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8693{
8694 return( conf->p_async_config_data );
8695}
8696
Gilles Peskine1febfef2018-04-30 11:54:39 +02008697void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008698{
8699 if( ssl->handshake == NULL )
8700 return( NULL );
8701 else
8702 return( ssl->handshake->user_async_ctx );
8703}
8704
Gilles Peskine1febfef2018-04-30 11:54:39 +02008705void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008706 void *ctx )
8707{
8708 if( ssl->handshake != NULL )
8709 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008710}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008711#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008712
Paul Bakker5121ce52009-01-03 21:22:43 +00008713/*
8714 * SSL get accessors
8715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008717{
8718 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8719}
8720
Hanno Becker8b170a02017-10-10 11:51:19 +01008721int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8722{
8723 /*
8724 * Case A: We're currently holding back
8725 * a message for further processing.
8726 */
8727
8728 if( ssl->keep_current_message == 1 )
8729 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008731 return( 1 );
8732 }
8733
8734 /*
8735 * Case B: Further records are pending in the current datagram.
8736 */
8737
8738#if defined(MBEDTLS_SSL_PROTO_DTLS)
8739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8740 ssl->in_left > ssl->next_record_offset )
8741 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008743 return( 1 );
8744 }
8745#endif /* MBEDTLS_SSL_PROTO_DTLS */
8746
8747 /*
8748 * Case C: A handshake message is being processed.
8749 */
8750
Hanno Becker8b170a02017-10-10 11:51:19 +01008751 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8752 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008754 return( 1 );
8755 }
8756
8757 /*
8758 * Case D: An application data message is being processed
8759 */
8760 if( ssl->in_offt != NULL )
8761 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008763 return( 1 );
8764 }
8765
8766 /*
8767 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008768 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008769 * we implement support for multiple alerts in single records.
8770 */
8771
8772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8773 return( 0 );
8774}
8775
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008776uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008777{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008778 if( ssl->session != NULL )
8779 return( ssl->session->verify_result );
8780
8781 if( ssl->session_negotiate != NULL )
8782 return( ssl->session_negotiate->verify_result );
8783
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008784 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008785}
8786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008788{
Paul Bakker926c8e42013-03-06 10:23:34 +01008789 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008790 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008793}
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008798 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008799 {
8800 switch( ssl->minor_ver )
8801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008803 return( "DTLSv1.0" );
8804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008806 return( "DTLSv1.2" );
8807
8808 default:
8809 return( "unknown (DTLS)" );
8810 }
8811 }
8812#endif
8813
Paul Bakker43ca69c2011-01-15 17:35:19 +00008814 switch( ssl->minor_ver )
8815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008817 return( "SSLv3.0" );
8818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008820 return( "TLSv1.0" );
8821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008823 return( "TLSv1.1" );
8824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008826 return( "TLSv1.2" );
8827
Paul Bakker43ca69c2011-01-15 17:35:19 +00008828 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008829 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008830 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008831}
8832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008834{
Hanno Becker3136ede2018-08-17 15:28:19 +01008835 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008837 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008838
Hanno Becker78640902018-08-13 16:35:15 +01008839 if( transform == NULL )
8840 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842#if defined(MBEDTLS_ZLIB_SUPPORT)
8843 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8844 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008845#endif
8846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 case MBEDTLS_MODE_GCM:
8850 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008851 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008853 transform_expansion = transform->minlen;
8854 break;
8855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008857
8858 block_size = mbedtls_cipher_get_block_size(
8859 &transform->cipher_ctx_enc );
8860
Hanno Becker3136ede2018-08-17 15:28:19 +01008861 /* Expansion due to the addition of the MAC. */
8862 transform_expansion += transform->maclen;
8863
8864 /* Expansion due to the addition of CBC padding;
8865 * Theoretically up to 256 bytes, but we never use
8866 * more than the block size of the underlying cipher. */
8867 transform_expansion += block_size;
8868
8869 /* For TLS 1.1 or higher, an explicit IV is added
8870 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008871#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8872 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008873 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008874#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008875
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008876 break;
8877
8878 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008881 }
8882
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008883 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008884}
8885
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008886#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8887size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8888{
8889 size_t max_len;
8890
8891 /*
8892 * Assume mfl_code is correct since it was checked when set
8893 */
Angus Grattond8213d02016-05-25 20:56:48 +10008894 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008895
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008896 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008897 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008898 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008899 {
Angus Grattond8213d02016-05-25 20:56:48 +10008900 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008901 }
8902
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008903 /* During a handshake, use the value being negotiated */
8904 if( ssl->session_negotiate != NULL &&
8905 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8906 {
8907 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8908 }
8909
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008910 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008911}
8912#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8913
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008914#if defined(MBEDTLS_SSL_PROTO_DTLS)
8915static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8916{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008917 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8919 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8920 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8921 return ( 0 );
8922
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008923 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8924 return( ssl->mtu );
8925
8926 if( ssl->mtu == 0 )
8927 return( ssl->handshake->mtu );
8928
8929 return( ssl->mtu < ssl->handshake->mtu ?
8930 ssl->mtu : ssl->handshake->mtu );
8931}
8932#endif /* MBEDTLS_SSL_PROTO_DTLS */
8933
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008934int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8935{
8936 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8937
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008938#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8939 !defined(MBEDTLS_SSL_PROTO_DTLS)
8940 (void) ssl;
8941#endif
8942
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008943#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8944 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8945
8946 if( max_len > mfl )
8947 max_len = mfl;
8948#endif
8949
8950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008951 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008952 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008953 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008954 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8955 const size_t overhead = (size_t) ret;
8956
8957 if( ret < 0 )
8958 return( ret );
8959
8960 if( mtu <= overhead )
8961 {
8962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8963 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8964 }
8965
8966 if( max_len > mtu - overhead )
8967 max_len = mtu - overhead;
8968 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008969#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008970
Hanno Becker0defedb2018-08-10 12:35:02 +01008971#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8972 !defined(MBEDTLS_SSL_PROTO_DTLS)
8973 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008974#endif
8975
8976 return( (int) max_len );
8977}
8978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008979#if defined(MBEDTLS_X509_CRT_PARSE_C)
8980const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008981{
8982 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008983 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008984
Hanno Beckere6824572019-02-07 13:18:46 +00008985#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008986 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008987#else
8988 return( NULL );
8989#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008994int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8995 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008996{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008997 if( ssl == NULL ||
8998 dst == NULL ||
8999 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009000 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009003 }
9004
Hanno Becker52055ae2019-02-06 14:30:46 +00009005 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009008
Paul Bakker5121ce52009-01-03 21:22:43 +00009009/*
Paul Bakker1961b702013-01-25 14:49:24 +01009010 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009015
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009016 if( ssl == NULL || ssl->conf == NULL )
9017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009020 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009024 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009026#endif
9027
Paul Bakker1961b702013-01-25 14:49:24 +01009028 return( ret );
9029}
9030
9031/*
9032 * Perform the SSL handshake
9033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009035{
9036 int ret = 0;
9037
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009038 if( ssl == NULL || ssl->conf == NULL )
9039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009046
9047 if( ret != 0 )
9048 break;
9049 }
9050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009052
9053 return( ret );
9054}
9055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_SSL_RENEGOTIATION)
9057#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009058/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009059 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009062{
9063 int ret;
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009066
9067 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9069 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009070
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009071 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009072 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009074 return( ret );
9075 }
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009078
9079 return( 0 );
9080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009082
9083/*
9084 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 * - any side: calling mbedtls_ssl_renegotiate(),
9086 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9087 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009088 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009089 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009093{
9094 int ret;
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009097
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009098 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9099 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009100
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009101 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9102 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009106 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009107 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009108 ssl->handshake->out_msg_seq = 1;
9109 else
9110 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009111 }
9112#endif
9113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9115 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009117 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009120 return( ret );
9121 }
9122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009124
9125 return( 0 );
9126}
9127
9128/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009129 * Renegotiate current connection on client,
9130 * or request renegotiation on server
9131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009135
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009136 if( ssl == NULL || ssl->conf == NULL )
9137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009140 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009141 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009147
9148 /* Did we already try/start sending HelloRequest? */
9149 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009151
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009152 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009157 /*
9158 * On client, either start the renegotiation process or,
9159 * if already in progress, continue the handshake
9160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009165
9166 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009169 return( ret );
9170 }
9171 }
9172 else
9173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009177 return( ret );
9178 }
9179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009181
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009182 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009183}
9184
9185/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009186 * Check record counters and renegotiate if they're above the limit.
9187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009189{
Andres AG2196c7f2016-12-15 17:01:16 +00009190 size_t ep_len = ssl_ep_len( ssl );
9191 int in_ctr_cmp;
9192 int out_ctr_cmp;
9193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9195 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009196 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009197 {
9198 return( 0 );
9199 }
9200
Andres AG2196c7f2016-12-15 17:01:16 +00009201 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9202 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009203 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009204 ssl->conf->renego_period + ep_len, 8 - ep_len );
9205
9206 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009207 {
9208 return( 0 );
9209 }
9210
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009215
9216/*
9217 * Receive application data decrypted from the SSL layer
9218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009220{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009221 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009222 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009223
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009224 if( ssl == NULL || ssl->conf == NULL )
9225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009233 return( ret );
9234
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009235 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009237 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009238 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009239 return( ret );
9240 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009241 }
9242#endif
9243
Hanno Becker4a810fb2017-05-24 16:27:30 +01009244 /*
9245 * Check if renegotiation is necessary and/or handshake is
9246 * in process. If yes, perform/continue, and fall through
9247 * if an unexpected packet is received while the client
9248 * is waiting for the ServerHello.
9249 *
9250 * (There is no equivalent to the last condition on
9251 * the server-side as it is not treated as within
9252 * a handshake while waiting for the ClientHello
9253 * after a renegotiation request.)
9254 */
9255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009257 ret = ssl_check_ctr_renegotiate( ssl );
9258 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9259 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009262 return( ret );
9263 }
9264#endif
9265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009269 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9270 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009273 return( ret );
9274 }
9275 }
9276
Hanno Beckere41158b2017-10-23 13:30:32 +01009277 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009278 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009279 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009280 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009281 if( ssl->f_get_timer != NULL &&
9282 ssl->f_get_timer( ssl->p_timer ) == -1 )
9283 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009284 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009285 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009286
Hanno Becker327c93b2018-08-15 13:56:18 +01009287 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009288 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009289 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9290 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009291
Hanno Becker4a810fb2017-05-24 16:27:30 +01009292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9293 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009294 }
9295
9296 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009298 {
9299 /*
9300 * OpenSSL sends empty messages to randomize the IV
9301 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009302 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009305 return( 0 );
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009308 return( ret );
9309 }
9310 }
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009315
Hanno Becker4a810fb2017-05-24 16:27:30 +01009316 /*
9317 * - For client-side, expect SERVER_HELLO_REQUEST.
9318 * - For server-side, expect CLIENT_HELLO.
9319 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9320 */
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009325 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009328
9329 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009332 {
9333 continue;
9334 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009337 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009338#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009339
Hanno Becker4a810fb2017-05-24 16:27:30 +01009340#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009345
9346 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009349 {
9350 continue;
9351 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009354 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009355#endif /* MBEDTLS_SSL_SRV_C */
9356
Hanno Becker21df7f92017-10-17 11:03:26 +01009357#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009358 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009359 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9360 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9361 ssl->conf->allow_legacy_renegotiation ==
9362 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9363 {
9364 /*
9365 * Accept renegotiation request
9366 */
Paul Bakker48916f92012-09-16 19:57:18 +00009367
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009368 /* DTLS clients need to know renego is server-initiated */
9369#if defined(MBEDTLS_SSL_PROTO_DTLS)
9370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9371 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9372 {
9373 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9374 }
9375#endif
9376 ret = ssl_start_renegotiation( ssl );
9377 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9378 ret != 0 )
9379 {
9380 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9381 return( ret );
9382 }
9383 }
9384 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009385#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009386 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009387 /*
9388 * Refuse renegotiation
9389 */
9390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393#if defined(MBEDTLS_SSL_PROTO_SSL3)
9394 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009395 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009396 /* SSLv3 does not have a "no_renegotiation" warning, so
9397 we send a fatal alert and abort the connection. */
9398 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9400 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009401 }
9402 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9406 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9409 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9410 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009411 {
9412 return( ret );
9413 }
Paul Bakker48916f92012-09-16 19:57:18 +00009414 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009415 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9417 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009421 }
Paul Bakker48916f92012-09-16 19:57:18 +00009422 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009423
Hanno Becker90333da2017-10-10 11:27:13 +01009424 /* At this point, we don't know whether the renegotiation has been
9425 * completed or not. The cases to consider are the following:
9426 * 1) The renegotiation is complete. In this case, no new record
9427 * has been read yet.
9428 * 2) The renegotiation is incomplete because the client received
9429 * an application data record while awaiting the ServerHello.
9430 * 3) The renegotiation is incomplete because the client received
9431 * a non-handshake, non-application data message while awaiting
9432 * the ServerHello.
9433 * In each of these case, looping will be the proper action:
9434 * - For 1), the next iteration will read a new record and check
9435 * if it's application data.
9436 * - For 2), the loop condition isn't satisfied as application data
9437 * is present, hence continue is the same as break
9438 * - For 3), the loop condition is satisfied and read_record
9439 * will re-deliver the message that was held back by the client
9440 * when expecting the ServerHello.
9441 */
9442 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009443 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009444#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009446 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009447 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009448 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009449 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009452 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009454 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009455 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9460 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009463 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009464 }
9465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9469 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009470 }
9471
9472 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009473
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009474 /* We're going to return something now, cancel timer,
9475 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009477 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009478
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009480 /* If we requested renego but received AppData, resend HelloRequest.
9481 * Do it now, after setting in_offt, to avoid taking this branch
9482 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009486 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009487 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009490 return( ret );
9491 }
9492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009495 }
9496
9497 n = ( len < ssl->in_msglen )
9498 ? len : ssl->in_msglen;
9499
9500 memcpy( buf, ssl->in_offt, n );
9501 ssl->in_msglen -= n;
9502
9503 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009504 {
9505 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009506 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009507 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009509 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009510 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009511 /* more data available */
9512 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009516
Paul Bakker23986e52011-04-24 08:57:21 +00009517 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009518}
9519
9520/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009521 * Send application data to be encrypted by the SSL layer, taking care of max
9522 * fragment length and buffer size.
9523 *
9524 * According to RFC 5246 Section 6.2.1:
9525 *
9526 * Zero-length fragments of Application data MAY be sent as they are
9527 * potentially useful as a traffic analysis countermeasure.
9528 *
9529 * Therefore, it is possible that the input message length is 0 and the
9530 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009531 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009532static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009533 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009534{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009535 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9536 const size_t max_len = (size_t) ret;
9537
9538 if( ret < 0 )
9539 {
9540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9541 return( ret );
9542 }
9543
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009544 if( len > max_len )
9545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009547 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009550 "maximum fragment length: %d > %d",
9551 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009553 }
9554 else
9555#endif
9556 len = max_len;
9557 }
Paul Bakker887bd502011-06-08 13:10:54 +00009558
Paul Bakker5121ce52009-01-03 21:22:43 +00009559 if( ssl->out_left != 0 )
9560 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009561 /*
9562 * The user has previously tried to send the data and
9563 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9564 * written. In this case, we expect the high-level write function
9565 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009570 return( ret );
9571 }
9572 }
Paul Bakker887bd502011-06-08 13:10:54 +00009573 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009574 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009575 /*
9576 * The user is trying to send a message the first time, so we need to
9577 * copy the data into the internal buffers and setup the data structure
9578 * to keep track of partial writes
9579 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009580 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009582 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009583
Hanno Becker67bc7c32018-08-06 11:33:50 +01009584 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009587 return( ret );
9588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009589 }
9590
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009591 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009592}
9593
9594/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009595 * Write application data, doing 1/n-1 splitting if necessary.
9596 *
9597 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009598 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009599 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009602static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009603 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009604{
9605 int ret;
9606
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009607 if( ssl->conf->cbc_record_splitting ==
9608 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009609 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9611 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9612 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009613 {
9614 return( ssl_write_real( ssl, buf, len ) );
9615 }
9616
9617 if( ssl->split_done == 0 )
9618 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009619 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009620 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009621 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009622 }
9623
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009624 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9625 return( ret );
9626 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009627
9628 return( ret + 1 );
9629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009631
9632/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009633 * Write application data (public-facing wrapper)
9634 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009635int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009636{
9637 int ret;
9638
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009641 if( ssl == NULL || ssl->conf == NULL )
9642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9643
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009644#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009645 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9646 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009647 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009648 return( ret );
9649 }
9650#endif
9651
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009652 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009653 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009654 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009655 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009657 return( ret );
9658 }
9659 }
9660
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009661#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009662 ret = ssl_write_split( ssl, buf, len );
9663#else
9664 ret = ssl_write_real( ssl, buf, len );
9665#endif
9666
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009668
9669 return( ret );
9670}
9671
9672/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009673 * Notify the peer that the connection is being closed
9674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009676{
9677 int ret;
9678
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009679 if( ssl == NULL || ssl->conf == NULL )
9680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009683
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009684 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9690 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9691 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009694 return( ret );
9695 }
9696 }
9697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009699
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009700 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009701}
9702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009704{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009705 if( transform == NULL )
9706 return;
9707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009709 deflateEnd( &transform->ctx_deflate );
9710 inflateEnd( &transform->ctx_inflate );
9711#endif
9712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9714 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009715
Hanno Beckerd56ed242018-01-03 15:32:51 +00009716#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 mbedtls_md_free( &transform->md_ctx_enc );
9718 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009719#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009720
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009721 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009722}
9723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724#if defined(MBEDTLS_X509_CRT_PARSE_C)
9725static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009728
9729 while( cur != NULL )
9730 {
9731 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009733 cur = next;
9734 }
9735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009737
Hanno Becker0271f962018-08-16 13:23:47 +01009738#if defined(MBEDTLS_SSL_PROTO_DTLS)
9739
9740static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9741{
9742 unsigned offset;
9743 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9744
9745 if( hs == NULL )
9746 return;
9747
Hanno Becker283f5ef2018-08-24 09:34:47 +01009748 ssl_free_buffered_record( ssl );
9749
Hanno Becker0271f962018-08-16 13:23:47 +01009750 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009751 ssl_buffering_free_slot( ssl, offset );
9752}
9753
9754static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9755 uint8_t slot )
9756{
9757 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9758 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009759
9760 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9761 return;
9762
Hanno Beckere605b192018-08-21 15:59:07 +01009763 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009764 {
Hanno Beckere605b192018-08-21 15:59:07 +01009765 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009766 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009767 mbedtls_free( hs_buf->data );
9768 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009769 }
9770}
9771
9772#endif /* MBEDTLS_SSL_PROTO_DTLS */
9773
Gilles Peskine9b562d52018-04-25 20:32:43 +02009774void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009775{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009776 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9777
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009778 if( handshake == NULL )
9779 return;
9780
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009781#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9782 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9783 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009784 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009785 handshake->async_in_progress = 0;
9786 }
9787#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9788
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009789#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9790 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9791 mbedtls_md5_free( &handshake->fin_md5 );
9792 mbedtls_sha1_free( &handshake->fin_sha1 );
9793#endif
9794#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9795#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009796#if defined(MBEDTLS_USE_PSA_CRYPTO)
9797 psa_hash_abort( &handshake->fin_sha256_psa );
9798#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009799 mbedtls_sha256_free( &handshake->fin_sha256 );
9800#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009801#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009802#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009803#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009804 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009805#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009806 mbedtls_sha512_free( &handshake->fin_sha512 );
9807#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009808#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009809#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811#if defined(MBEDTLS_DHM_C)
9812 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#if defined(MBEDTLS_ECDH_C)
9815 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009816#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009817#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009818 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009819#if defined(MBEDTLS_SSL_CLI_C)
9820 mbedtls_free( handshake->ecjpake_cache );
9821 handshake->ecjpake_cache = NULL;
9822 handshake->ecjpake_cache_len = 0;
9823#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009824#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009825
Janos Follath4ae5c292016-02-10 11:27:43 +00009826#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9827 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009828 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009830#endif
9831
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009832#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9833 if( handshake->psk != NULL )
9834 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009835 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009836 mbedtls_free( handshake->psk );
9837 }
9838#endif
9839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9841 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009842 /*
9843 * Free only the linked list wrapper, not the keys themselves
9844 * since the belong to the SNI callback
9845 */
9846 if( handshake->sni_key_cert != NULL )
9847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009849
9850 while( cur != NULL )
9851 {
9852 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009854 cur = next;
9855 }
9856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009858
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009859#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009860 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009861 if( handshake->ecrs_peer_cert != NULL )
9862 {
9863 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9864 mbedtls_free( handshake->ecrs_peer_cert );
9865 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009866#endif
9867
Hanno Becker75173122019-02-06 16:18:31 +00009868#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9869 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9870 mbedtls_pk_free( &handshake->peer_pubkey );
9871#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873#if defined(MBEDTLS_SSL_PROTO_DTLS)
9874 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009875 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009876 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009877#endif
9878
Hanno Becker4a63ed42019-01-08 11:39:35 +00009879#if defined(MBEDTLS_ECDH_C) && \
9880 defined(MBEDTLS_USE_PSA_CRYPTO)
9881 psa_destroy_key( handshake->ecdh_psa_privkey );
9882#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9883
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009884 mbedtls_platform_zeroize( handshake,
9885 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009886}
9887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009889{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009890 if( session == NULL )
9891 return;
9892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009894 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009895#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009896
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009897#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009899#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009900
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009901 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009902}
9903
Paul Bakker5121ce52009-01-03 21:22:43 +00009904/*
9905 * Free an SSL context
9906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009908{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009909 if( ssl == NULL )
9910 return;
9911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009913
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009914 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 {
Angus Grattond8213d02016-05-25 20:56:48 +10009916 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009918 }
9919
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009920 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 {
Angus Grattond8213d02016-05-25 20:56:48 +10009922 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009924 }
9925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009927 if( ssl->compress_buf != NULL )
9928 {
Angus Grattond8213d02016-05-25 20:56:48 +10009929 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009931 }
9932#endif
9933
Paul Bakker48916f92012-09-16 19:57:18 +00009934 if( ssl->transform )
9935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 mbedtls_ssl_transform_free( ssl->transform );
9937 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009938 }
9939
9940 if( ssl->handshake )
9941 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009942 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9944 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 mbedtls_free( ssl->handshake );
9947 mbedtls_free( ssl->transform_negotiate );
9948 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009949 }
9950
Paul Bakkerc0463502013-02-14 11:19:38 +01009951 if( ssl->session )
9952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 mbedtls_ssl_session_free( ssl->session );
9954 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009955 }
9956
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009957#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009958 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009959 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009960 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009962 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009963#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9966 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9969 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009970 }
9971#endif
9972
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009973#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009975#endif
9976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009978
Paul Bakker86f04f42013-02-14 11:20:09 +01009979 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009980 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009981}
9982
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009983/*
9984 * Initialze mbedtls_ssl_config
9985 */
9986void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9987{
9988 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9989}
9990
Simon Butcherc97b6972015-12-27 23:48:17 +00009991#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009992static int ssl_preset_default_hashes[] = {
9993#if defined(MBEDTLS_SHA512_C)
9994 MBEDTLS_MD_SHA512,
9995 MBEDTLS_MD_SHA384,
9996#endif
9997#if defined(MBEDTLS_SHA256_C)
9998 MBEDTLS_MD_SHA256,
9999 MBEDTLS_MD_SHA224,
10000#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010001#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010002 MBEDTLS_MD_SHA1,
10003#endif
10004 MBEDTLS_MD_NONE
10005};
Simon Butcherc97b6972015-12-27 23:48:17 +000010006#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010007
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010008static int ssl_preset_suiteb_ciphersuites[] = {
10009 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10010 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10011 0
10012};
10013
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010014#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010015static int ssl_preset_suiteb_hashes[] = {
10016 MBEDTLS_MD_SHA256,
10017 MBEDTLS_MD_SHA384,
10018 MBEDTLS_MD_NONE
10019};
10020#endif
10021
10022#if defined(MBEDTLS_ECP_C)
10023static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10024 MBEDTLS_ECP_DP_SECP256R1,
10025 MBEDTLS_ECP_DP_SECP384R1,
10026 MBEDTLS_ECP_DP_NONE
10027};
10028#endif
10029
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010030/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010031 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010032 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010033int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010034 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010035{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010036#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010037 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010038#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010039
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010040 /* Use the functions here so that they are covered in tests,
10041 * but otherwise access member directly for efficiency */
10042 mbedtls_ssl_conf_endpoint( conf, endpoint );
10043 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010044
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010045 /*
10046 * Things that are common to all presets
10047 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010048#if defined(MBEDTLS_SSL_CLI_C)
10049 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10050 {
10051 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10053 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10054#endif
10055 }
10056#endif
10057
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010058#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010059 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010060#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010061
10062#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10063 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10064#endif
10065
10066#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10067 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10068#endif
10069
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010070#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10071 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10072#endif
10073
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010074#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010075 conf->f_cookie_write = ssl_cookie_write_dummy;
10076 conf->f_cookie_check = ssl_cookie_check_dummy;
10077#endif
10078
10079#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10080 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10081#endif
10082
Janos Follath088ce432017-04-10 12:42:31 +010010083#if defined(MBEDTLS_SSL_SRV_C)
10084 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10085#endif
10086
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010087#if defined(MBEDTLS_SSL_PROTO_DTLS)
10088 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10089 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10090#endif
10091
10092#if defined(MBEDTLS_SSL_RENEGOTIATION)
10093 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010094 memset( conf->renego_period, 0x00, 2 );
10095 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010096#endif
10097
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010098#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10099 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10100 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010101 const unsigned char dhm_p[] =
10102 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10103 const unsigned char dhm_g[] =
10104 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10105
Hanno Beckera90658f2017-10-04 15:29:08 +010010106 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10107 dhm_p, sizeof( dhm_p ),
10108 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010109 {
10110 return( ret );
10111 }
10112 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010113#endif
10114
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010115 /*
10116 * Preset-specific defaults
10117 */
10118 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010119 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010120 /*
10121 * NSA Suite B
10122 */
10123 case MBEDTLS_SSL_PRESET_SUITEB:
10124 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10125 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10126 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10127 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10128
10129 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10130 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10131 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10132 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10133 ssl_preset_suiteb_ciphersuites;
10134
10135#if defined(MBEDTLS_X509_CRT_PARSE_C)
10136 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010137#endif
10138
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010139#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010140 conf->sig_hashes = ssl_preset_suiteb_hashes;
10141#endif
10142
10143#if defined(MBEDTLS_ECP_C)
10144 conf->curve_list = ssl_preset_suiteb_curves;
10145#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010146 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010147
10148 /*
10149 * Default
10150 */
10151 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010152 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10153 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10154 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10155 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10156 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10157 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10158 MBEDTLS_SSL_MIN_MINOR_VERSION :
10159 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010160 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10161 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10162
10163#if defined(MBEDTLS_SSL_PROTO_DTLS)
10164 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10165 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10166#endif
10167
10168 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10169 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10170 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10171 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10172 mbedtls_ssl_list_ciphersuites();
10173
10174#if defined(MBEDTLS_X509_CRT_PARSE_C)
10175 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10176#endif
10177
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010178#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010179 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010180#endif
10181
10182#if defined(MBEDTLS_ECP_C)
10183 conf->curve_list = mbedtls_ecp_grp_id_list();
10184#endif
10185
10186#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10187 conf->dhm_min_bitlen = 1024;
10188#endif
10189 }
10190
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010191 return( 0 );
10192}
10193
10194/*
10195 * Free mbedtls_ssl_config
10196 */
10197void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10198{
10199#if defined(MBEDTLS_DHM_C)
10200 mbedtls_mpi_free( &conf->dhm_P );
10201 mbedtls_mpi_free( &conf->dhm_G );
10202#endif
10203
10204#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10205 if( conf->psk != NULL )
10206 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010207 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010208 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010209 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010210 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010211 }
10212
10213 if( conf->psk_identity != NULL )
10214 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010215 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010216 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010217 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010218 conf->psk_identity_len = 0;
10219 }
10220#endif
10221
10222#if defined(MBEDTLS_X509_CRT_PARSE_C)
10223 ssl_key_cert_free( conf->key_cert );
10224#endif
10225
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010226 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010227}
10228
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010229#if defined(MBEDTLS_PK_C) && \
10230 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010231/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_RSA_C)
10237 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10238 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_ECDSA_C)
10241 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10242 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010245}
10246
Hanno Becker7e5437a2017-04-28 17:15:26 +010010247unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10248{
10249 switch( type ) {
10250 case MBEDTLS_PK_RSA:
10251 return( MBEDTLS_SSL_SIG_RSA );
10252 case MBEDTLS_PK_ECDSA:
10253 case MBEDTLS_PK_ECKEY:
10254 return( MBEDTLS_SSL_SIG_ECDSA );
10255 default:
10256 return( MBEDTLS_SSL_SIG_ANON );
10257 }
10258}
10259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010261{
10262 switch( sig )
10263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264#if defined(MBEDTLS_RSA_C)
10265 case MBEDTLS_SSL_SIG_RSA:
10266 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268#if defined(MBEDTLS_ECDSA_C)
10269 case MBEDTLS_SSL_SIG_ECDSA:
10270 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010271#endif
10272 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010273 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010274 }
10275}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010276#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010277
Hanno Becker7e5437a2017-04-28 17:15:26 +010010278#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10279 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10280
10281/* Find an entry in a signature-hash set matching a given hash algorithm. */
10282mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10283 mbedtls_pk_type_t sig_alg )
10284{
10285 switch( sig_alg )
10286 {
10287 case MBEDTLS_PK_RSA:
10288 return( set->rsa );
10289 case MBEDTLS_PK_ECDSA:
10290 return( set->ecdsa );
10291 default:
10292 return( MBEDTLS_MD_NONE );
10293 }
10294}
10295
10296/* Add a signature-hash-pair to a signature-hash set */
10297void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10298 mbedtls_pk_type_t sig_alg,
10299 mbedtls_md_type_t md_alg )
10300{
10301 switch( sig_alg )
10302 {
10303 case MBEDTLS_PK_RSA:
10304 if( set->rsa == MBEDTLS_MD_NONE )
10305 set->rsa = md_alg;
10306 break;
10307
10308 case MBEDTLS_PK_ECDSA:
10309 if( set->ecdsa == MBEDTLS_MD_NONE )
10310 set->ecdsa = md_alg;
10311 break;
10312
10313 default:
10314 break;
10315 }
10316}
10317
10318/* Allow exactly one hash algorithm for each signature. */
10319void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10320 mbedtls_md_type_t md_alg )
10321{
10322 set->rsa = md_alg;
10323 set->ecdsa = md_alg;
10324}
10325
10326#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10327 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10328
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010329/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010330 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010333{
10334 switch( hash )
10335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336#if defined(MBEDTLS_MD5_C)
10337 case MBEDTLS_SSL_HASH_MD5:
10338 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340#if defined(MBEDTLS_SHA1_C)
10341 case MBEDTLS_SSL_HASH_SHA1:
10342 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010343#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344#if defined(MBEDTLS_SHA256_C)
10345 case MBEDTLS_SSL_HASH_SHA224:
10346 return( MBEDTLS_MD_SHA224 );
10347 case MBEDTLS_SSL_HASH_SHA256:
10348 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350#if defined(MBEDTLS_SHA512_C)
10351 case MBEDTLS_SSL_HASH_SHA384:
10352 return( MBEDTLS_MD_SHA384 );
10353 case MBEDTLS_SSL_HASH_SHA512:
10354 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010355#endif
10356 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010358 }
10359}
10360
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010361/*
10362 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10363 */
10364unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10365{
10366 switch( md )
10367 {
10368#if defined(MBEDTLS_MD5_C)
10369 case MBEDTLS_MD_MD5:
10370 return( MBEDTLS_SSL_HASH_MD5 );
10371#endif
10372#if defined(MBEDTLS_SHA1_C)
10373 case MBEDTLS_MD_SHA1:
10374 return( MBEDTLS_SSL_HASH_SHA1 );
10375#endif
10376#if defined(MBEDTLS_SHA256_C)
10377 case MBEDTLS_MD_SHA224:
10378 return( MBEDTLS_SSL_HASH_SHA224 );
10379 case MBEDTLS_MD_SHA256:
10380 return( MBEDTLS_SSL_HASH_SHA256 );
10381#endif
10382#if defined(MBEDTLS_SHA512_C)
10383 case MBEDTLS_MD_SHA384:
10384 return( MBEDTLS_SSL_HASH_SHA384 );
10385 case MBEDTLS_MD_SHA512:
10386 return( MBEDTLS_SSL_HASH_SHA512 );
10387#endif
10388 default:
10389 return( MBEDTLS_SSL_HASH_NONE );
10390 }
10391}
10392
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010393#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010394/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010395 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010396 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010397 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010398int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010401
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010402 if( ssl->conf->curve_list == NULL )
10403 return( -1 );
10404
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010405 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010406 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010407 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010408
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010409 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010410}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010411#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010412
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010413#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010414/*
10415 * Check if a hash proposed by the peer is in our list.
10416 * Return 0 if we're willing to use it, -1 otherwise.
10417 */
10418int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10419 mbedtls_md_type_t md )
10420{
10421 const int *cur;
10422
10423 if( ssl->conf->sig_hashes == NULL )
10424 return( -1 );
10425
10426 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10427 if( *cur == (int) md )
10428 return( 0 );
10429
10430 return( -1 );
10431}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010432#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_X509_CRT_PARSE_C)
10435int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10436 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010437 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010438 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010439{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010440 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010442 int usage = 0;
10443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010445 const char *ext_oid;
10446 size_t ext_len;
10447#endif
10448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10450 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010451 ((void) cert);
10452 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010453 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010454#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10457 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010458 {
10459 /* Server part of the key exchange */
10460 switch( ciphersuite->key_exchange )
10461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 case MBEDTLS_KEY_EXCHANGE_RSA:
10463 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010464 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010465 break;
10466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10468 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10469 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10470 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010471 break;
10472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10474 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010475 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010476 break;
10477
10478 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 case MBEDTLS_KEY_EXCHANGE_NONE:
10480 case MBEDTLS_KEY_EXCHANGE_PSK:
10481 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10482 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010483 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010484 usage = 0;
10485 }
10486 }
10487 else
10488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10490 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010491 }
10492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010494 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010495 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010496 ret = -1;
10497 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010498#else
10499 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10503 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10506 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010507 }
10508 else
10509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10511 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010512 }
10513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010515 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010516 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010517 ret = -1;
10518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010520
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010521 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010524
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010525/*
10526 * Convert version numbers to/from wire format
10527 * and, for DTLS, to/from TLS equivalent.
10528 *
10529 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010530 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010531 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10532 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010535 unsigned char ver[2] )
10536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#if defined(MBEDTLS_SSL_PROTO_DTLS)
10538 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010541 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10542
10543 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10544 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10545 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010546 else
10547#else
10548 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010549#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010550 {
10551 ver[0] = (unsigned char) major;
10552 ver[1] = (unsigned char) minor;
10553 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010554}
10555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010557 const unsigned char ver[2] )
10558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559#if defined(MBEDTLS_SSL_PROTO_DTLS)
10560 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010561 {
10562 *major = 255 - ver[0] + 2;
10563 *minor = 255 - ver[1] + 1;
10564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010566 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10567 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010568 else
10569#else
10570 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010571#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010572 {
10573 *major = ver[0];
10574 *minor = ver[1];
10575 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010576}
10577
Simon Butcher99000142016-10-13 17:21:01 +010010578int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10579{
10580#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10581 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10582 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10583
10584 switch( md )
10585 {
10586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10587#if defined(MBEDTLS_MD5_C)
10588 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010589 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010590#endif
10591#if defined(MBEDTLS_SHA1_C)
10592 case MBEDTLS_SSL_HASH_SHA1:
10593 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10594 break;
10595#endif
10596#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10597#if defined(MBEDTLS_SHA512_C)
10598 case MBEDTLS_SSL_HASH_SHA384:
10599 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10600 break;
10601#endif
10602#if defined(MBEDTLS_SHA256_C)
10603 case MBEDTLS_SSL_HASH_SHA256:
10604 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10605 break;
10606#endif
10607 default:
10608 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10609 }
10610
10611 return 0;
10612#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10613 (void) ssl;
10614 (void) md;
10615
10616 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10617#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10618}
10619
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010620#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10621 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10622int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10623 unsigned char *output,
10624 unsigned char *data, size_t data_len )
10625{
10626 int ret = 0;
10627 mbedtls_md5_context mbedtls_md5;
10628 mbedtls_sha1_context mbedtls_sha1;
10629
10630 mbedtls_md5_init( &mbedtls_md5 );
10631 mbedtls_sha1_init( &mbedtls_sha1 );
10632
10633 /*
10634 * digitally-signed struct {
10635 * opaque md5_hash[16];
10636 * opaque sha_hash[20];
10637 * };
10638 *
10639 * md5_hash
10640 * MD5(ClientHello.random + ServerHello.random
10641 * + ServerParams);
10642 * sha_hash
10643 * SHA(ClientHello.random + ServerHello.random
10644 * + ServerParams);
10645 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010646 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010647 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010649 goto exit;
10650 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010651 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010652 ssl->handshake->randbytes, 64 ) ) != 0 )
10653 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010655 goto exit;
10656 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010657 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010658 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010660 goto exit;
10661 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010662 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010663 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010665 goto exit;
10666 }
10667
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010668 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010669 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010671 goto exit;
10672 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010673 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010674 ssl->handshake->randbytes, 64 ) ) != 0 )
10675 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010677 goto exit;
10678 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010679 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010680 data_len ) ) != 0 )
10681 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010683 goto exit;
10684 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010685 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010686 output + 16 ) ) != 0 )
10687 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010689 goto exit;
10690 }
10691
10692exit:
10693 mbedtls_md5_free( &mbedtls_md5 );
10694 mbedtls_sha1_free( &mbedtls_sha1 );
10695
10696 if( ret != 0 )
10697 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10698 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10699
10700 return( ret );
10701
10702}
10703#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10704 MBEDTLS_SSL_PROTO_TLS1_1 */
10705
10706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010708
10709#if defined(MBEDTLS_USE_PSA_CRYPTO)
10710int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10711 unsigned char *hash, size_t *hashlen,
10712 unsigned char *data, size_t data_len,
10713 mbedtls_md_type_t md_alg )
10714{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010715 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010716 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010717 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10718
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010720
10721 if( ( status = psa_hash_setup( &hash_operation,
10722 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010723 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010724 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010725 goto exit;
10726 }
10727
Andrzej Kurek814feff2019-01-14 04:35:19 -050010728 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10729 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010730 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010731 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010732 goto exit;
10733 }
10734
Andrzej Kurek814feff2019-01-14 04:35:19 -050010735 if( ( status = psa_hash_update( &hash_operation,
10736 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010737 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010738 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010739 goto exit;
10740 }
10741
Andrzej Kurek814feff2019-01-14 04:35:19 -050010742 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10743 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010744 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010745 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010746 goto exit;
10747 }
10748
10749exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010750 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010751 {
10752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10753 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010754 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010755 {
10756 case PSA_ERROR_NOT_SUPPORTED:
10757 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010758 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010759 case PSA_ERROR_BUFFER_TOO_SMALL:
10760 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10761 case PSA_ERROR_INSUFFICIENT_MEMORY:
10762 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10763 default:
10764 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10765 }
10766 }
10767 return( 0 );
10768}
10769
10770#else
10771
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010772int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010773 unsigned char *hash, size_t *hashlen,
10774 unsigned char *data, size_t data_len,
10775 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010776{
10777 int ret = 0;
10778 mbedtls_md_context_t ctx;
10779 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010780 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010781
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010783
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010784 mbedtls_md_init( &ctx );
10785
10786 /*
10787 * digitally-signed struct {
10788 * opaque client_random[32];
10789 * opaque server_random[32];
10790 * ServerDHParams params;
10791 * };
10792 */
10793 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10794 {
10795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10796 goto exit;
10797 }
10798 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10799 {
10800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10801 goto exit;
10802 }
10803 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10804 {
10805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10806 goto exit;
10807 }
10808 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10809 {
10810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10811 goto exit;
10812 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010813 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010814 {
10815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10816 goto exit;
10817 }
10818
10819exit:
10820 mbedtls_md_free( &ctx );
10821
10822 if( ret != 0 )
10823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10824 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10825
10826 return( ret );
10827}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010828#endif /* MBEDTLS_USE_PSA_CRYPTO */
10829
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010830#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10831 MBEDTLS_SSL_PROTO_TLS1_2 */
10832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833#endif /* MBEDTLS_SSL_TLS_C */