blob: c2903605cdd657a42b668916174ca9433e6dbcda [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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519static int tls_prf_generic( mbedtls_md_type_t md_type,
520 const unsigned char *secret, size_t slen,
521 const char *label,
522 const unsigned char *random, size_t rlen,
523 unsigned char *dstbuf, size_t dlen )
524{
525 psa_status_t status;
526 psa_algorithm_t alg;
527 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500528 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500529 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
530
Andrzej Kurek2f760752019-01-28 08:08:15 -0500531 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500533
Andrzej Kurekc929a822019-01-14 03:51:11 -0500534 if( md_type == MBEDTLS_MD_SHA384 )
535 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
536 else
537 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
538
Andrzej Kurek2f760752019-01-28 08:08:15 -0500539 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500540 psa_key_policy_set_usage( &policy,
541 PSA_KEY_USAGE_DERIVE,
542 alg );
543 status = psa_set_key_policy( master_slot, &policy );
544 if( status != PSA_SUCCESS )
545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
546
547 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
548 if( status != PSA_SUCCESS )
549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
550
551 status = psa_key_derivation( &generator,
552 master_slot, alg,
553 random, rlen,
554 (unsigned char const *) label,
555 (size_t) strlen( label ),
556 dlen );
557 if( status != PSA_SUCCESS )
558 {
559 psa_generator_abort( &generator );
560 psa_destroy_key( master_slot );
561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
562 }
563
564 status = psa_generator_read( &generator, dstbuf, dlen );
565 if( status != PSA_SUCCESS )
566 {
567 psa_generator_abort( &generator );
568 psa_destroy_key( master_slot );
569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
570 }
571
572 status = psa_generator_abort( &generator );
573 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500574 {
575 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500576 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500577 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500578
579 status = psa_destroy_key( master_slot );
580 if( status != PSA_SUCCESS )
581 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
582
Andrzej Kurek33171262019-01-15 03:25:18 -0500583 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500584}
585
586#else /* MBEDTLS_USE_PSA_CRYPTO */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 const unsigned char *secret, size_t slen,
590 const char *label,
591 const unsigned char *random, size_t rlen,
592 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593{
594 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
598 const mbedtls_md_info_t *md_info;
599 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 int ret;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
609 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611
612 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 memcpy( tmp + md_len, label, nb );
614 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 nb += rlen;
616
617 /*
618 * Compute P_<hash>(secret, label + random)[0..dlen]
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
624 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
625 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100626
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
635 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
646 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647
648 return( 0 );
649}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652static int tls_prf_sha256( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663static int tls_prf_sha384( const unsigned char *secret, size_t slen,
664 const char *label,
665 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 unsigned char *dstbuf, size_t dlen )
667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_SHA512_C */
672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
677 defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker380da532012-04-18 16:10:25 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_SSL3)
682static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
683static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
687static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
688static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200689#endif
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
692#if defined(MBEDTLS_SHA256_C)
693static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
694static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
695static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
699static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
700static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
701static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Hanno Becker7d0a5692018-10-23 15:26:22 +0100705#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
706 defined(MBEDTLS_USE_PSA_CRYPTO)
707static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
708{
709 if( ssl->conf->f_psk != NULL )
710 {
711 /* If we've used a callback to select the PSK,
712 * the static configuration is irrelevant. */
713 if( ssl->handshake->psk_opaque != 0 )
714 return( 1 );
715
716 return( 0 );
717 }
718
719 if( ssl->conf->psk_opaque != 0 )
720 return( 1 );
721
722 return( 0 );
723}
724#endif /* MBEDTLS_USE_PSA_CRYPTO &&
725 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000728{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200729 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000730#if defined(MBEDTLS_USE_PSA_CRYPTO)
731 int psa_fallthrough;
732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 unsigned char keyblk[256];
735 unsigned char *key1;
736 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100737 unsigned char *mac_enc;
738 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000739 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000741 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 const mbedtls_cipher_info_t *cipher_info;
744 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100745
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000746 /* cf. RFC 5246, Section 8.1:
747 * "The master secret is always exactly 48 bytes in length." */
748 size_t const master_secret_len = 48;
749
Hanno Becker35b23c72018-10-23 12:10:41 +0100750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
751 unsigned char session_hash[48];
752#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_ssl_session *session = ssl->session_negotiate;
755 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000760#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
761 transform->encrypt_then_mac = session->encrypt_then_mac;
762#endif
763 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000764
765 ciphersuite_info = handshake->ciphersuite_info;
766 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( cipher_info == NULL )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000770 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100772 }
773
Hanno Beckere694c3e2017-12-27 21:34:08 +0000774 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100775 if( md_info == NULL )
776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000778 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100780 }
781
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_SSL3)
786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787 {
Paul Bakker48916f92012-09-16 19:57:18 +0000788 handshake->tls_prf = ssl3_prf;
789 handshake->calc_verify = ssl_calc_verify_ssl;
790 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000791 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792 else
793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
795 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 {
Paul Bakker48916f92012-09-16 19:57:18 +0000797 handshake->tls_prf = tls1_prf;
798 handshake->calc_verify = ssl_calc_verify_tls;
799 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200801 else
802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
804#if defined(MBEDTLS_SHA512_C)
805 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000806 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807 {
Paul Bakker48916f92012-09-16 19:57:18 +0000808 handshake->tls_prf = tls_prf_sha384;
809 handshake->calc_verify = ssl_calc_verify_tls_sha384;
810 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000811 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SHA256_C)
815 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 {
Paul Bakker48916f92012-09-16 19:57:18 +0000817 handshake->tls_prf = tls_prf_sha256;
818 handshake->calc_verify = ssl_calc_verify_tls_sha256;
819 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821 else
822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200827 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 * SSLv3:
831 * master =
832 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
833 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
834 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200835 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200836 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * master = PRF( premaster, "master secret", randbytes )[0..47]
838 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100839 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
842 }
843 else
844 {
845 /* The label for the KDF used for key expansion.
846 * This is either "master secret" or "extended master secret"
847 * depending on whether the Extended Master Secret extension
848 * is used. */
849 char const *lbl = "master secret";
850
851 /* The salt for the KDF used for key expansion.
852 * - If the Extended Master Secret extension is not used,
853 * this is ClientHello.Random + ServerHello.Random
854 * (see Sect. 8.1 in RFC 5246).
855 * - If the Extended Master Secret extension is used,
856 * this is the transcript of the handshake so far.
857 * (see Sect. 4 in RFC 7627). */
858 unsigned char const *salt = handshake->randbytes;
859 size_t salt_len = 64;
860
861#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200865
Hanno Becker35b23c72018-10-23 12:10:41 +0100866 lbl = "extended master secret";
867 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200868 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
870 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000873 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
874 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100875 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000876 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200877 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100878#endif /* MBEDTLS_SHA512_C */
879 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880 }
881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100883 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200884
Hanno Becker35b23c72018-10-23 12:10:41 +0100885 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200886 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100887#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
888
Hanno Becker7d0a5692018-10-23 15:26:22 +0100889#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
890 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
891 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
892 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
893 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100894 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100895 /* Perform PSK-to-MS expansion in a single step. */
896 psa_status_t status;
897 psa_algorithm_t alg;
898 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500899 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100900
901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
902
903 psk = ssl->conf->psk_opaque;
904 if( ssl->handshake->psk_opaque != 0 )
905 psk = ssl->handshake->psk_opaque;
906
Hanno Becker22bf1452019-04-05 11:21:08 +0100907 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +0100908 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
909 else
910 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
911
912 status = psa_key_derivation( &generator, psk, alg,
913 salt, salt_len,
914 (unsigned char const *) lbl,
915 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000916 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100917 if( status != PSA_SUCCESS )
918 {
919 psa_generator_abort( &generator );
920 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
921 }
922
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000923 status = psa_generator_read( &generator, session->master,
924 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100925 if( status != PSA_SUCCESS )
926 {
927 psa_generator_abort( &generator );
928 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
929 }
930
931 status = psa_generator_abort( &generator );
932 if( status != PSA_SUCCESS )
933 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100934 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 else
936#endif
937 {
938 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
939 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000940 session->master,
941 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100942 if( ret != 0 )
943 {
944 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
945 return( ret );
946 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
949 handshake->premaster,
950 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100951
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952 mbedtls_platform_zeroize( handshake->premaster,
953 sizeof(handshake->premaster) );
954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
957 /*
958 * Swap the client and server random values.
959 */
Paul Bakker48916f92012-09-16 19:57:18 +0000960 memcpy( tmp, handshake->randbytes, 64 );
961 memcpy( handshake->randbytes, tmp + 32, 32 );
962 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500963 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965 /*
966 * SSLv3:
967 * key block =
968 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
969 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
970 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
971 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
972 * ...
973 *
974 * TLSv1:
975 * key block = PRF( master, "key expansion", randbytes )
976 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100977 ret = handshake->tls_prf( session->master, 48, "key expansion",
978 handshake->randbytes, 64, keyblk, 256 );
979 if( ret != 0 )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100982 return( ret );
983 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
986 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
987 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
988 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
989 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500991 mbedtls_platform_zeroize( handshake->randbytes,
992 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 /*
995 * Determine the appropriate key, IV and MAC length.
996 */
Paul Bakker68884e32013-01-07 18:20:04 +0100997
Hanno Becker88aaf652017-12-27 08:17:40 +0000998 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200999
Hanno Becker8031d062018-01-03 15:32:31 +00001000#if defined(MBEDTLS_GCM_C) || \
1001 defined(MBEDTLS_CCM_C) || \
1002 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001004 cipher_info->mode == MBEDTLS_MODE_CCM ||
1005 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001007 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001008
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001009 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001010 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001011 transform->taglen =
1012 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001013
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001014 /* All modes haves 96-bit IVs;
1015 * GCM and CCM has 4 implicit and 8 explicit bytes
1016 * ChachaPoly has all 12 bytes implicit
1017 */
Paul Bakker68884e32013-01-07 18:20:04 +01001018 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001019 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1020 transform->fixed_ivlen = 12;
1021 else
1022 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001024 /* Minimum length of encrypted record */
1025 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001027 }
1028 else
Hanno Becker8031d062018-01-03 15:32:31 +00001029#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1030#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1031 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1032 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001033 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1036 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001039 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001042 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001043 mac_key_len = mbedtls_md_get_size( md_info );
1044 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001047 /*
1048 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1049 * (rfc 6066 page 13 or rfc 2104 section 4),
1050 * so we only need to adjust the length here.
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001055
1056#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1057 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001058 * HMAC implementation which also truncates the key
1059 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001060 mac_key_len = transform->maclen;
1061#endif
1062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001064
1065 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001066 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001068 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 transform->minlen = transform->maclen;
1071 else
Paul Bakker68884e32013-01-07 18:20:04 +01001072 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001073 /*
1074 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001075 * 1. if EtM is in use: one block plus MAC
1076 * otherwise: * first multiple of blocklen greater than maclen
1077 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1080 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001081 {
1082 transform->minlen = transform->maclen
1083 + cipher_info->block_size;
1084 }
1085 else
1086#endif
1087 {
1088 transform->minlen = transform->maclen
1089 + cipher_info->block_size
1090 - transform->maclen % cipher_info->block_size;
1091 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1094 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1095 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001096 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001097 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1100 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1101 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 {
1103 transform->minlen += transform->ivlen;
1104 }
1105 else
1106#endif
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001109 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1110 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001111 }
Paul Bakker68884e32013-01-07 18:20:04 +01001112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 }
Hanno Becker8031d062018-01-03 15:32:31 +00001114 else
1115#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1116 {
1117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1119 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Hanno Becker88aaf652017-12-27 08:17:40 +00001121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1122 (unsigned) keylen,
1123 (unsigned) transform->minlen,
1124 (unsigned) transform->ivlen,
1125 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
1127 /*
1128 * Finally setup the cipher contexts, IVs and MAC secrets.
1129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001131 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001133 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001134 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001137 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001139 /*
1140 * This is not used in TLS v1.1.
1141 */
Paul Bakker48916f92012-09-16 19:57:18 +00001142 iv_copy_len = ( transform->fixed_ivlen ) ?
1143 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001144 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1145 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001146 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 }
1148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#endif /* MBEDTLS_SSL_CLI_C */
1150#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001151 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001153 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001154 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Hanno Becker81c7b182017-11-09 18:39:33 +00001156 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001157 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001159 /*
1160 * This is not used in TLS v1.1.
1161 */
Paul Bakker48916f92012-09-16 19:57:18 +00001162 iv_copy_len = ( transform->fixed_ivlen ) ?
1163 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001164 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1165 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001166 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001168 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001172 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1173 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Hanno Beckerd56ed242018-01-03 15:32:51 +00001176#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_SSL_PROTO_SSL3)
1178 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001179 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001180 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001183 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1184 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001185 }
1186
Hanno Becker81c7b182017-11-09 18:39:33 +00001187 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1188 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001189 }
1190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1192#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1193 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001195 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001196 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1197 For AEAD-based ciphersuites, there is nothing to do here. */
1198 if( mac_key_len != 0 )
1199 {
1200 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1201 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1202 }
Paul Bakker68884e32013-01-07 18:20:04 +01001203 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001204 else
1205#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001208 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1209 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001210 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001211#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1214 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001215 {
1216 int ret = 0;
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001219
Hanno Becker88aaf652017-12-27 08:17:40 +00001220 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001221 transform->iv_enc, transform->iv_dec,
1222 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001223 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001224 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001227 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1228 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001229 }
1230 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001231#else
1232 ((void) mac_dec);
1233 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001235
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001236#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1237 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001238 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001239 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1240 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001241 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001242 iv_copy_len );
1243 }
1244#endif
1245
Hanno Beckerf704bef2018-11-16 15:21:18 +00001246#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001247
1248 /* Only use PSA-based ciphers for TLS-1.2.
1249 * That's relevant at least for TLS-1.0, where
1250 * we assume that mbedtls_cipher_crypt() updates
1251 * the structure field for the IV, which the PSA-based
1252 * implementation currently doesn't. */
1253#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1254 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001255 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001256 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001257 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001258 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1259 {
1260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001261 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001262 }
1263
1264 if( ret == 0 )
1265 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001267 psa_fallthrough = 0;
1268 }
1269 else
1270 {
1271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1272 psa_fallthrough = 1;
1273 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001274 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001275 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001276 psa_fallthrough = 1;
1277#else
1278 psa_fallthrough = 1;
1279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001280
Hanno Beckercb1cc802018-11-17 22:27:38 +00001281 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001282#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001283 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001284 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001287 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001288 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001289
Hanno Beckerf704bef2018-11-16 15:21:18 +00001290#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001291 /* Only use PSA-based ciphers for TLS-1.2.
1292 * That's relevant at least for TLS-1.0, where
1293 * we assume that mbedtls_cipher_crypt() updates
1294 * the structure field for the IV, which the PSA-based
1295 * implementation currently doesn't. */
1296#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1297 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001298 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001299 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001300 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001301 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1302 {
1303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001304 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001305 }
1306
1307 if( ret == 0 )
1308 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001310 psa_fallthrough = 0;
1311 }
1312 else
1313 {
1314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1315 psa_fallthrough = 1;
1316 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001317 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001318 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001319 psa_fallthrough = 1;
1320#else
1321 psa_fallthrough = 1;
1322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001323
Hanno Beckercb1cc802018-11-17 22:27:38 +00001324 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001325#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001326 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001327 cipher_info ) ) != 0 )
1328 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001330 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001331 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001334 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001338 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001339 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001342 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001346 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001347 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_CIPHER_MODE_CBC)
1350 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1353 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001356 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001357 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1360 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001363 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001368 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001371 // Initialize compression
1372 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001374 {
Paul Bakker16770332013-10-11 09:59:44 +02001375 if( ssl->compress_buf == NULL )
1376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001378 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001379 if( ssl->compress_buf == NULL )
1380 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001382 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001383 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1384 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001385 }
1386 }
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001389
Paul Bakker48916f92012-09-16 19:57:18 +00001390 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1391 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001392
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001393 if( deflateInit( &transform->ctx_deflate,
1394 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001395 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1399 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001400 }
1401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001405end:
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Ron Eldore6992702019-05-07 18:27:13 +03001407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408}
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_SSL_PROTO_SSL3)
1411void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001412{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001413 mbedtls_md5_context md5;
1414 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 unsigned char pad_1[48];
1416 unsigned char pad_2[48];
1417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001420 mbedtls_md5_init( &md5 );
1421 mbedtls_sha1_init( &sha1 );
1422
1423 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1424 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Paul Bakker380da532012-04-18 16:10:25 +00001426 memset( pad_1, 0x36, 48 );
1427 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001429 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1430 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1431 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001433 mbedtls_md5_starts_ret( &md5 );
1434 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1435 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1436 mbedtls_md5_update_ret( &md5, hash, 16 );
1437 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001439 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1440 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1441 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001443 mbedtls_sha1_starts_ret( &sha1 );
1444 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1445 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1446 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1447 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001452 mbedtls_md5_free( &md5 );
1453 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001454
Paul Bakker380da532012-04-18 16:10:25 +00001455 return;
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1460void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001461{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001462 mbedtls_md5_context md5;
1463 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_md5_init( &md5 );
1468 mbedtls_sha1_init( &sha1 );
1469
1470 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1471 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Andrzej Kurekeb342242019-01-29 09:14:33 -05001473 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001474 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_md5_free( &md5 );
1480 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001481
Paul Bakker380da532012-04-18 16:10:25 +00001482 return;
1483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1487#if defined(MBEDTLS_SHA256_C)
1488void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001489{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001490#if defined(MBEDTLS_USE_PSA_CRYPTO)
1491 size_t hash_size;
1492 psa_status_t status;
1493 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1494
1495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1496 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1497 if( status != PSA_SUCCESS )
1498 {
1499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1500 return;
1501 }
1502
1503 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1504 if( status != PSA_SUCCESS )
1505 {
1506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1507 return;
1508 }
1509 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1511#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001512 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001513
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001514 mbedtls_sha256_init( &sha256 );
1515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001517
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001518 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001519 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001525#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_SHA512_C)
1531void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001532{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001533#if defined(MBEDTLS_USE_PSA_CRYPTO)
1534 size_t hash_size;
1535 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001536 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001537
1538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001539 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001540 if( status != PSA_SUCCESS )
1541 {
1542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1543 return;
1544 }
1545
Andrzej Kurek972fba52019-01-30 03:29:12 -05001546 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001547 if( status != PSA_SUCCESS )
1548 {
1549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1550 return;
1551 }
1552 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1554#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001555 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001556
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001557 mbedtls_sha512_init( &sha512 );
1558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001560
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001561 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001562 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001567 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001568#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 return;
1570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_SHA512_C */
1572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1575int 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 +02001576{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001577 unsigned char *p = ssl->handshake->premaster;
1578 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001579 const unsigned char *psk = ssl->conf->psk;
1580 size_t psk_len = ssl->conf->psk_len;
1581
1582 /* If the psk callback was called, use its result */
1583 if( ssl->handshake->psk != NULL )
1584 {
1585 psk = ssl->handshake->psk;
1586 psk_len = ssl->handshake->psk_len;
1587 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588
1589 /*
1590 * PMS = struct {
1591 * opaque other_secret<0..2^16-1>;
1592 * opaque psk<0..2^16-1>;
1593 * };
1594 * with "other_secret" depending on the particular key exchange
1595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1597 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001599 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001602 *(p++) = (unsigned char)( psk_len >> 8 );
1603 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001604
1605 if( end < p || (size_t)( end - p ) < psk_len )
1606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1607
1608 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001609 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 }
1611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1613#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1614 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001615 {
1616 /*
1617 * other_secret already set by the ClientKeyExchange message,
1618 * and is 48 bytes long
1619 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001620 if( end - p < 2 )
1621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1622
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001623 *p++ = 0;
1624 *p++ = 48;
1625 p += 48;
1626 }
1627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1629#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1630 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001632 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001633 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001635 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001637 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001638 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 return( ret );
1642 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001643 *(p++) = (unsigned char)( len >> 8 );
1644 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 p += len;
1646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 }
1649 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1651#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1652 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001653 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001654 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655 size_t zlen;
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001658 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001659 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001662 return( ret );
1663 }
1664
1665 *(p++) = (unsigned char)( zlen >> 8 );
1666 *(p++) = (unsigned char)( zlen );
1667 p += zlen;
1668
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001669 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1670 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001671 }
1672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001677 }
1678
1679 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001680 if( end - p < 2 )
1681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001682
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001683 *(p++) = (unsigned char)( psk_len >> 8 );
1684 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001685
1686 if( end < p || (size_t)( end - p ) < psk_len )
1687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1688
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001689 memcpy( p, psk, psk_len );
1690 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001691
1692 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1693
1694 return( 0 );
1695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001699/*
1700 * SSLv3.0 MAC functions
1701 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001702#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001703static void ssl_mac( mbedtls_md_context_t *md_ctx,
1704 const unsigned char *secret,
1705 const unsigned char *buf, size_t len,
1706 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001707 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708{
1709 unsigned char header[11];
1710 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001711 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1713 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001714
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001715 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001717 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001718 else
Paul Bakker68884e32013-01-07 18:20:04 +01001719 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
1721 memcpy( header, ctr, 8 );
1722 header[ 8] = (unsigned char) type;
1723 header[ 9] = (unsigned char)( len >> 8 );
1724 header[10] = (unsigned char)( len );
1725
Paul Bakker68884e32013-01-07 18:20:04 +01001726 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_md_starts( md_ctx );
1728 mbedtls_md_update( md_ctx, secret, md_size );
1729 mbedtls_md_update( md_ctx, padding, padlen );
1730 mbedtls_md_update( md_ctx, header, 11 );
1731 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001732 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Paul Bakker68884e32013-01-07 18:20:04 +01001734 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 mbedtls_md_starts( md_ctx );
1736 mbedtls_md_update( md_ctx, secret, md_size );
1737 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001738 mbedtls_md_update( md_ctx, out, md_size );
1739 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001742
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001743/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001744 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001745#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001746 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1747 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1748 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1749/* This function makes sure every byte in the memory region is accessed
1750 * (in ascending addresses order) */
1751static void ssl_read_memory( unsigned char *p, size_t len )
1752{
1753 unsigned char acc = 0;
1754 volatile unsigned char force;
1755
1756 for( ; len != 0; p++, len-- )
1757 acc ^= *p;
1758
1759 force = acc;
1760 (void) force;
1761}
1762#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1763
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001764/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001766 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001767
1768static void ssl_extract_add_data_from_record( unsigned char* add_data,
1769 mbedtls_record *rec )
1770{
1771 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1772 add_data[8] = rec->type;
1773 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1774 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1775 add_data[12] = rec->data_len & 0xFF;
1776}
1777
Hanno Beckera18d1322018-01-03 14:27:32 +00001778int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1779 mbedtls_ssl_transform *transform,
1780 mbedtls_record *rec,
1781 int (*f_rng)(void *, unsigned char *, size_t),
1782 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001786 unsigned char * data;
1787 unsigned char add_data[13];
1788 size_t post_avail;
1789
1790 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001791#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001792 ((void) ssl);
1793#endif
1794
1795 /* The PRNG is used for dynamic IV generation that's used
1796 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1797#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1798 ( defined(MBEDTLS_AES_C) || \
1799 defined(MBEDTLS_ARIA_C) || \
1800 defined(MBEDTLS_CAMELLIA_C) ) && \
1801 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1802 ((void) f_rng);
1803 ((void) p_rng);
1804#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001808 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001809 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1812 }
1813 if( rec == NULL ||
1814 rec->buf == NULL ||
1815 rec->buf_len < rec->data_offset ||
1816 rec->buf_len - rec->data_offset < rec->data_len )
1817 {
1818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001820 }
1821
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001822 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1823 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001825 data, rec->data_len );
1826
1827 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1828
1829 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1830 {
1831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1832 (unsigned) rec->data_len,
1833 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1835 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001836
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001838 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 */
Hanno Becker52344c22018-01-03 15:24:20 +00001840#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 if( mode == MBEDTLS_MODE_STREAM ||
1842 ( mode == MBEDTLS_MODE_CBC
1843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001844 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001845#endif
1846 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001848 if( post_avail < transform->maclen )
1849 {
1850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1851 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1852 }
1853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001855 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001856 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001857 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001858 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1859 data, rec->data_len, rec->ctr, rec->type, mac );
1860 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001861 }
1862 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1865 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001866 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001867 {
Hanno Becker992b6872017-11-09 18:57:39 +00001868 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1869
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001870 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001871
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001872 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1873 sizeof( add_data ) );
1874 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1875 data, rec->data_len );
1876 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1877 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1878
1879 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001880 }
1881 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001882#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001886 }
1887
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001888 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1889 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001890
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001891 rec->data_len += transform->maclen;
1892 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001893 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001894 }
Hanno Becker52344c22018-01-03 15:24:20 +00001895#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001897 /*
1898 * Encrypt
1899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1901 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001903 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001904 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001906 "including %d bytes of padding",
1907 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001909 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1910 transform->iv_enc, transform->ivlen,
1911 data, rec->data_len,
1912 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001915 return( ret );
1916 }
1917
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001918 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 }
Paul Bakker68884e32013-01-07 18:20:04 +01001924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001926
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927#if defined(MBEDTLS_GCM_C) || \
1928 defined(MBEDTLS_CCM_C) || \
1929 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001931 mode == MBEDTLS_MODE_CCM ||
1932 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001933 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001934 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001935 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001936 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001937
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001938 /* Check that there's space for both the authentication tag
1939 * and the explicit IV before and after the record content. */
1940 if( post_avail < transform->taglen ||
1941 rec->data_offset < explicit_iv_len )
1942 {
1943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1944 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1945 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001946
Paul Bakker68884e32013-01-07 18:20:04 +01001947 /*
1948 * Generate IV
1949 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001950 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1951 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001952 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001953 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001954 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1955 explicit_iv_len );
1956 /* Prefix record content with explicit IV. */
1957 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001958 }
1959 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1960 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001961 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001962 unsigned char i;
1963
1964 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1965
1966 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001967 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001968 }
1969 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001970 {
1971 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001974 }
1975
Hanno Becker1f10d762019-04-26 13:34:37 +01001976 ssl_extract_add_data_from_record( add_data, rec );
1977
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001978 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1979 iv, transform->ivlen );
1980 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001981 data - explicit_iv_len, explicit_iv_len );
1982 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1983 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001985 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001986 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001987
Paul Bakker68884e32013-01-07 18:20:04 +01001988 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001989 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001990 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001991
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001992 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001993 iv, transform->ivlen,
1994 add_data, 13, /* add data */
1995 data, rec->data_len, /* source */
1996 data, &rec->data_len, /* destination */
1997 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002000 return( ret );
2001 }
2002
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002003 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2004 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002005
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002006 rec->data_len += transform->taglen + explicit_iv_len;
2007 rec->data_offset -= explicit_iv_len;
2008 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002009 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2013#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002014 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002017 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002018 size_t padlen, i;
2019 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002020
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002021 /* Currently we're always using minimal padding
2022 * (up to 255 bytes would be allowed). */
2023 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2024 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 padlen = 0;
2026
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002027 /* Check there's enough space in the buffer for the padding. */
2028 if( post_avail < padlen + 1 )
2029 {
2030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2031 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2032 }
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002035 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002036
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002037 rec->data_len += padlen + 1;
2038 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002041 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002042 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2043 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002044 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002045 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002046 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002047 if( f_rng == NULL )
2048 {
2049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2051 }
2052
2053 if( rec->data_offset < transform->ivlen )
2054 {
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2056 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2057 }
2058
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002059 /*
2060 * Generate IV
2061 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002062 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002063 if( ret != 0 )
2064 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002065
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002066 memcpy( data - transform->ivlen, transform->iv_enc,
2067 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002068
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002073 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002074 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002075 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002077 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2078 transform->iv_enc,
2079 transform->ivlen,
2080 data, rec->data_len,
2081 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002084 return( ret );
2085 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002086
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002091 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002094 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002095 {
2096 /*
2097 * Save IV in SSL3 and TLS1
2098 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002099 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2100 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002103#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 {
2105 data -= transform->ivlen;
2106 rec->data_offset -= transform->ivlen;
2107 rec->data_len += transform->ivlen;
2108 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002111 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002112 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002113 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2114
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002115 /*
2116 * MAC(MAC_write_key, seq_num +
2117 * TLSCipherText.type +
2118 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002119 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002120 * IV + // except for TLS 1.0
2121 * ENC(content + padding + padding_length));
2122 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002123
2124 if( post_avail < transform->maclen)
2125 {
2126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2127 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2128 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002129
Hanno Becker1f10d762019-04-26 13:34:37 +01002130 ssl_extract_add_data_from_record( add_data, rec );
2131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002133 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2134 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2137 sizeof( add_data ) );
2138 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2139 data, rec->data_len );
2140 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2141 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002142
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002144
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002145 rec->data_len += transform->maclen;
2146 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002147 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002153 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002159 /* Make extra sure authentication was performed, exactly once */
2160 if( auth_done != 1 )
2161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002164 }
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
2168 return( 0 );
2169}
2170
Hanno Beckera18d1322018-01-03 14:27:32 +00002171int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2172 mbedtls_ssl_transform *transform,
2173 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002175 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002177 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002178#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002179 size_t padlen = 0, correct = 1;
2180#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002181 unsigned char* data;
2182 unsigned char add_data[13];
2183
Hanno Beckera18d1322018-01-03 14:27:32 +00002184#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002185 ((void) ssl);
2186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002189 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002190 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2193 }
2194 if( rec == NULL ||
2195 rec->buf == NULL ||
2196 rec->buf_len < rec->data_offset ||
2197 rec->buf_len - rec->data_offset < rec->data_len )
2198 {
2199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002201 }
2202
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002203 data = rec->buf + rec->data_offset;
2204 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2207 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002208 {
2209 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002210 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2211 transform->iv_dec,
2212 transform->ivlen,
2213 data, rec->data_len,
2214 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002217 return( ret );
2218 }
2219
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002220 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
Paul Bakker68884e32013-01-07 18:20:04 +01002226 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002228#if defined(MBEDTLS_GCM_C) || \
2229 defined(MBEDTLS_CCM_C) || \
2230 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002232 mode == MBEDTLS_MODE_CCM ||
2233 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002234 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002235 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002236 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002237
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002238 /*
2239 * Compute and update sizes
2240 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002241 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002244 "+ taglen (%d)", rec->data_len,
2245 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002247 }
Paul Bakker68884e32013-01-07 18:20:04 +01002248
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002249 /*
2250 * Prepare IV
2251 */
2252 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2253 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002254 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002255 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002256 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002257
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002258 }
2259 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2260 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002261 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002262 unsigned char i;
2263
2264 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2265
2266 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002267 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002268 }
2269 else
2270 {
2271 /* Reminder if we ever add an AEAD mode with a different size */
2272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2274 }
2275
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002276 data += explicit_iv_len;
2277 rec->data_offset += explicit_iv_len;
2278 rec->data_len -= explicit_iv_len + transform->taglen;
2279
2280 ssl_extract_add_data_from_record( add_data, rec );
2281 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2282 add_data, 13 );
2283
2284 memcpy( transform->iv_dec + transform->fixed_ivlen,
2285 data - explicit_iv_len, explicit_iv_len );
2286
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002287 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002288 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002289 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002290
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002291
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002292 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002293 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002294 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002295 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2296 iv, transform->ivlen,
2297 add_data, 13,
2298 data, rec->data_len,
2299 data, &olen,
2300 data + rec->data_len,
2301 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2306 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002307
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002308 return( ret );
2309 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002310 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002311
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002312 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2315 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002316 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2320#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002321 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002324 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002325
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 /*
Paul Bakker45829992013-01-03 14:52:21 +01002327 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002330 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2331 {
2332 /* The ciphertext is prefixed with the CBC IV. */
2333 minlen += transform->ivlen;
2334 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002335#endif
Paul Bakker45829992013-01-03 14:52:21 +01002336
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002337 /* Size considerations:
2338 *
2339 * - The CBC cipher text must not be empty and hence
2340 * at least of size transform->ivlen.
2341 *
2342 * Together with the potential IV-prefix, this explains
2343 * the first of the two checks below.
2344 *
2345 * - The record must contain a MAC, either in plain or
2346 * encrypted, depending on whether Encrypt-then-MAC
2347 * is used or not.
2348 * - If it is, the message contains the IV-prefix,
2349 * the CBC ciphertext, and the MAC.
2350 * - If it is not, the padded plaintext, and hence
2351 * the CBC ciphertext, has at least length maclen + 1
2352 * because there is at least the padding length byte.
2353 *
2354 * As the CBC ciphertext is not empty, both cases give the
2355 * lower bound minlen + maclen + 1 on the record size, which
2356 * we test for in the second check below.
2357 */
2358 if( rec->data_len < minlen + transform->ivlen ||
2359 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002362 "+ 1 ) ( + expl IV )", rec->data_len,
2363 transform->ivlen,
2364 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002366 }
2367
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002368 /*
2369 * Authenticate before decrypt if enabled
2370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002372 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002373 {
Hanno Becker992b6872017-11-09 18:57:39 +00002374 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002377
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002378 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2379 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002380
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002382
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002383 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2384 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2385 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2386 data, rec->data_len );
2387 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2388 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002389
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002390 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2391 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002392 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002393 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002394
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002395 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2396 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002400 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002401 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002402 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002404
2405 /*
2406 * Check length sanity
2407 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002408 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002411 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002413 }
2414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002417 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002421 /* This is safe because data_len >= minlen + maclen + 1 initially,
2422 * and at this point we have at most subtracted maclen (note that
2423 * minlen == transform->ivlen here). */
2424 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002426 data += transform->ivlen;
2427 rec->data_offset += transform->ivlen;
2428 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002432 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2433 transform->iv_dec, transform->ivlen,
2434 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002437 return( ret );
2438 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002439
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002440 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002444 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002447 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 {
2449 /*
2450 * Save IV in SSL3 and TLS1
2451 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002452 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2453 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002455#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002457 /* Safe since data_len >= minlen + maclen + 1, so after having
2458 * subtracted at most minlen and maclen up to this point,
2459 * data_len > 0. */
2460 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002461
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002462 if( auth_done == 1 )
2463 {
2464 correct *= ( rec->data_len >= padlen + 1 );
2465 padlen *= ( rec->data_len >= padlen + 1 );
2466 }
2467 else
Paul Bakker45829992013-01-03 14:52:21 +01002468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002470 if( rec->data_len < transform->maclen + padlen + 1 )
2471 {
2472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2473 rec->data_len,
2474 transform->maclen,
2475 padlen + 1 ) );
2476 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002477#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002478
2479 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2480 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002483 padlen++;
2484
2485 /* Regardless of the validity of the padding,
2486 * we have data_len >= padlen here. */
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002489 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002491 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_SSL_DEBUG_ALL)
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002495 "should be no more than %d",
2496 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002497#endif
Paul Bakker45829992013-01-03 14:52:21 +01002498 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
2500 }
2501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2503#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2504 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002505 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002507 /* The padding check involves a series of up to 256
2508 * consecutive memory reads at the end of the record
2509 * plaintext buffer. In order to hide the length and
2510 * validity of the padding, always perform exactly
2511 * `min(256,plaintext_len)` reads (but take into account
2512 * only the last `padlen` bytes for the padding check). */
2513 size_t pad_count = 0;
2514 size_t real_count = 0;
2515 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002516
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002517 /* Index of first padding byte; it has been ensured above
2518 * that the subtraction is safe. */
2519 size_t const padding_idx = rec->data_len - padlen;
2520 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2521 size_t const start_idx = rec->data_len - num_checks;
2522 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002523
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002524 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002525 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002526 real_count |= ( idx >= padding_idx );
2527 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002528 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002529 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002532 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002534#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002535 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2539 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002543 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002544
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002545 /* If the padding was found to be invalid, padlen == 0
2546 * and the subtraction is safe. If the padding was found valid,
2547 * padlen hasn't been changed and the previous assertion
2548 * data_len >= padlen still holds. */
2549 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002551 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002553 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002559#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
2564 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002565 * Authenticate if not done yet.
2566 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 */
Hanno Becker52344c22018-01-03 15:24:20 +00002568#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002569 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002570 {
Hanno Becker992b6872017-11-09 18:57:39 +00002571 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002572
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002573 /* If the initial value of padlen was such that
2574 * data_len < maclen + padlen + 1, then padlen
2575 * got reset to 1, and the initial check
2576 * data_len >= minlen + maclen + 1
2577 * guarantees that at this point we still
2578 * have at least data_len >= maclen.
2579 *
2580 * If the initial value of padlen was such that
2581 * data_len >= maclen + padlen + 1, then we have
2582 * subtracted either padlen + 1 (if the padding was correct)
2583 * or 0 (if the padding was incorrect) since then,
2584 * hence data_len >= maclen in any case.
2585 */
2586 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002588 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002592 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 ssl_mac( &transform->md_ctx_dec,
2594 transform->mac_dec,
2595 data, rec->data_len,
2596 rec->ctr, rec->type,
2597 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 }
2599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2601#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2602 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002604 {
2605 /*
2606 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002607 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 *
2609 * Known timing attacks:
2610 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2611 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002612 * To compensate for different timings for the MAC calculation
2613 * depending on how much padding was removed (which is determined
2614 * by padlen), process extra_run more blocks through the hash
2615 * function.
2616 *
2617 * The formula in the paper is
2618 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2619 * where L1 is the size of the header plus the decrypted message
2620 * plus CBC padding and L2 is the size of the header plus the
2621 * decrypted message. This is for an underlying hash function
2622 * with 64-byte blocks.
2623 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2624 * correctly. We round down instead of up, so -56 is the correct
2625 * value for our calculations instead of -55.
2626 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002627 * Repeat the formula rather than defining a block_size variable.
2628 * This avoids requiring division by a variable at runtime
2629 * (which would be marginally less efficient and would require
2630 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002631 */
2632 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002634
2635 /*
2636 * The next two sizes are the minimum and maximum values of
2637 * in_msglen over all padlen values.
2638 *
2639 * They're independent of padlen, since we previously did
2640 * in_msglen -= padlen.
2641 *
2642 * Note that max_len + maclen is never more than the buffer
2643 * length, as we previously did in_msglen -= maclen too.
2644 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002645 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002646 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2647
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002648 memset( tmp, 0, sizeof( tmp ) );
2649
2650 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002651 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002652#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2653 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002654 case MBEDTLS_MD_MD5:
2655 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002656 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002657 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2659 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002660 break;
2661#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002662#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002663 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002664 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2666 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002667 break;
2668#endif
2669 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2672 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002673
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002674 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002675
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2677 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2678 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002679 /* Make sure we access everything even when padlen > 0. This
2680 * makes the synchronisation requirements for just-in-time
2681 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 ssl_read_memory( data + rec->data_len, padlen );
2683 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002684
2685 /* Call mbedtls_md_process at least once due to cache attacks
2686 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002687 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002689
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002690 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002691
2692 /* Make sure we access all the memory that could contain the MAC,
2693 * before we check it in the next code block. This makes the
2694 * synchronisation requirements for just-in-time Prime+Probe
2695 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002696 ssl_read_memory( data + min_len,
2697 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002698 }
2699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2701 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002705 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002707#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2709 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002710#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002712 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2713 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_SSL_DEBUG_ALL)
2716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002717#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 correct = 0;
2719 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002720 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002721 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002722
2723 /*
2724 * Finally check the correct flag
2725 */
2726 if( correct == 0 )
2727 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002728#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002729
2730 /* Make extra sure authentication was performed, exactly once */
2731 if( auth_done != 1 )
2732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
2739 return( 0 );
2740}
2741
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002742#undef MAC_NONE
2743#undef MAC_PLAINTEXT
2744#undef MAC_CIPHERTEXT
2745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002747/*
2748 * Compression/decompression functions
2749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002751{
2752 int ret;
2753 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002754 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002755 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002756 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002760 if( len_pre == 0 )
2761 return( 0 );
2762
Paul Bakker2770fbd2012-07-03 13:30:23 +00002763 memcpy( msg_pre, ssl->out_msg, len_pre );
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766 ssl->out_msglen ) );
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769 ssl->out_msg, ssl->out_msglen );
2770
Paul Bakker48916f92012-09-16 19:57:18 +00002771 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2772 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2773 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002774 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775
Paul Bakker48916f92012-09-16 19:57:18 +00002776 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777 if( ret != Z_OK )
2778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2780 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 }
2782
Angus Grattond8213d02016-05-25 20:56:48 +10002783 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002784 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002787 ssl->out_msglen ) );
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002790 ssl->out_msg, ssl->out_msglen );
2791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002793
2794 return( 0 );
2795}
2796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002798{
2799 int ret;
2800 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002801 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002803 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002807 if( len_pre == 0 )
2808 return( 0 );
2809
Paul Bakker2770fbd2012-07-03 13:30:23 +00002810 memcpy( msg_pre, ssl->in_msg, len_pre );
2811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002813 ssl->in_msglen ) );
2814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002816 ssl->in_msg, ssl->in_msglen );
2817
Paul Bakker48916f92012-09-16 19:57:18 +00002818 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2819 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2820 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002821 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002822 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002823
Paul Bakker48916f92012-09-16 19:57:18 +00002824 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002825 if( ret != Z_OK )
2826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2828 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002829 }
2830
Angus Grattond8213d02016-05-25 20:56:48 +10002831 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002832 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002835 ssl->in_msglen ) );
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002838 ssl->in_msg, ssl->in_msglen );
2839
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
2842 return( 0 );
2843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2847static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#if defined(MBEDTLS_SSL_PROTO_DTLS)
2850static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002851{
2852 /* If renegotiation is not enforced, retransmit until we would reach max
2853 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002854 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002855 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002856 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002857 unsigned char doublings = 1;
2858
2859 while( ratio != 0 )
2860 {
2861 ++doublings;
2862 ratio >>= 1;
2863 }
2864
2865 if( ++ssl->renego_records_seen > doublings )
2866 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002868 return( 0 );
2869 }
2870 }
2871
2872 return( ssl_write_hello_request( ssl ) );
2873}
2874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002876
Paul Bakker5121ce52009-01-03 21:22:43 +00002877/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002878 * Fill the input message buffer by appending data to it.
2879 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002880 *
2881 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2882 * available (from this read and/or a previous one). Otherwise, an error code
2883 * is returned (possibly EOF or WANT_READ).
2884 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002885 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2886 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2887 * since we always read a whole datagram at once.
2888 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002889 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002890 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002893{
Paul Bakker23986e52011-04-24 08:57:21 +00002894 int ret;
2895 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002899 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002902 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002904 }
2905
Angus Grattond8213d02016-05-25 20:56:48 +10002906 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002910 }
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002915 uint32_t timeout;
2916
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002917 /* Just to be sure */
2918 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2919 {
2920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2921 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2923 }
2924
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002925 /*
2926 * The point is, we need to always read a full datagram at once, so we
2927 * sometimes read more then requested, and handle the additional data.
2928 * It could be the rest of the current record (while fetching the
2929 * header) and/or some other records in the same datagram.
2930 */
2931
2932 /*
2933 * Move to the next record in the already read datagram if applicable
2934 */
2935 if( ssl->next_record_offset != 0 )
2936 {
2937 if( ssl->in_left < ssl->next_record_offset )
2938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002941 }
2942
2943 ssl->in_left -= ssl->next_record_offset;
2944
2945 if( ssl->in_left != 0 )
2946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002948 ssl->next_record_offset ) );
2949 memmove( ssl->in_hdr,
2950 ssl->in_hdr + ssl->next_record_offset,
2951 ssl->in_left );
2952 }
2953
2954 ssl->next_record_offset = 0;
2955 }
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002959
2960 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002961 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002962 */
2963 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002966 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002967 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002968
2969 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002970 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002971 * are not at the beginning of a new record, the caller did something
2972 * wrong.
2973 */
2974 if( ssl->in_left != 0 )
2975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002978 }
2979
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002980 /*
2981 * Don't even try to read if time's out already.
2982 * This avoids by-passing the timer when repeatedly receiving messages
2983 * that will end up being dropped.
2984 */
2985 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002986 {
2987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002988 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002989 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002990 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002991 {
Angus Grattond8213d02016-05-25 20:56:48 +10002992 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002995 timeout = ssl->handshake->retransmit_timeout;
2996 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002997 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003000
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003001 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003002 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3003 timeout );
3004 else
3005 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003008
3009 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003011 }
3012
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003013 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003016 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003019 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003020 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003023 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003024 }
3025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003029 return( ret );
3030 }
3031
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003032 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003035 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003037 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003038 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003041 return( ret );
3042 }
3043
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003044 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003045 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003047 }
3048
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 if( ret < 0 )
3050 return( ret );
3051
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003052 ssl->in_left = ret;
3053 }
3054 else
3055#endif
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 ssl->in_left, nb_want ) );
3059
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003060 while( ssl->in_left < nb_want )
3061 {
3062 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003063
3064 if( ssl_check_timer( ssl ) != 0 )
3065 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3066 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003067 {
3068 if( ssl->f_recv_timeout != NULL )
3069 {
3070 ret = ssl->f_recv_timeout( ssl->p_bio,
3071 ssl->in_hdr + ssl->in_left, len,
3072 ssl->conf->read_timeout );
3073 }
3074 else
3075 {
3076 ret = ssl->f_recv( ssl->p_bio,
3077 ssl->in_hdr + ssl->in_left, len );
3078 }
3079 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003082 ssl->in_left, nb_want ) );
3083 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003084
3085 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003087
3088 if( ret < 0 )
3089 return( ret );
3090
mohammad160352aecb92018-03-28 23:41:40 -07003091 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003092 {
Darryl Green11999bb2018-03-13 15:22:58 +00003093 MBEDTLS_SSL_DEBUG_MSG( 1,
3094 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003095 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3097 }
3098
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099 ssl->in_left += ret;
3100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 }
3102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
3105 return( 0 );
3106}
3107
3108/*
3109 * Flush any data not yet written
3110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003112{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003113 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003114 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003118 if( ssl->f_send == NULL )
3119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003121 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003123 }
3124
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003125 /* Avoid incrementing counter if data is flushed */
3126 if( ssl->out_left == 0 )
3127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003129 return( 0 );
3130 }
3131
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 while( ssl->out_left > 0 )
3133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3135 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Hanno Becker2b1e3542018-08-06 11:19:13 +01003137 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003138 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
3142 if( ret <= 0 )
3143 return( ret );
3144
mohammad160352aecb92018-03-28 23:41:40 -07003145 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003146 {
Darryl Green11999bb2018-03-13 15:22:58 +00003147 MBEDTLS_SSL_DEBUG_MSG( 1,
3148 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003149 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3151 }
3152
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 ssl->out_left -= ret;
3154 }
3155
Hanno Becker2b1e3542018-08-06 11:19:13 +01003156#if defined(MBEDTLS_SSL_PROTO_DTLS)
3157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003158 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003159 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003160 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003161 else
3162#endif
3163 {
3164 ssl->out_hdr = ssl->out_buf + 8;
3165 }
3166 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
3170 return( 0 );
3171}
3172
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003173/*
3174 * Functions to handle the DTLS retransmission state machine
3175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177/*
3178 * Append current handshake message to current outgoing flight
3179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003181{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3184 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3185 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186
3187 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003188 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003192 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003193 }
3194
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003195 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003196 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003199 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200 }
3201
3202 /* Copy current handshake message with headers */
3203 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3204 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003205 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206 msg->next = NULL;
3207
3208 /* Append to the current flight */
3209 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003210 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003211 else
3212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003214 while( cur->next != NULL )
3215 cur = cur->next;
3216 cur->next = msg;
3217 }
3218
Hanno Becker3b235902018-08-06 09:54:53 +01003219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003220 return( 0 );
3221}
3222
3223/*
3224 * Free the current flight of handshake messages
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 mbedtls_ssl_flight_item *cur = flight;
3229 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003230
3231 while( cur != NULL )
3232 {
3233 next = cur->next;
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 mbedtls_free( cur->p );
3236 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003237
3238 cur = next;
3239 }
3240}
3241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3243static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003244#endif
3245
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003246/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003247 * Swap transform_out and out_ctr with the alternative ones
3248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252 unsigned char tmp_out_ctr[8];
3253
3254 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003257 return;
3258 }
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003262 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003263 tmp_transform = ssl->transform_out;
3264 ssl->transform_out = ssl->handshake->alt_transform_out;
3265 ssl->handshake->alt_transform_out = tmp_transform;
3266
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003267 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003268 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3269 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003270 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003271
3272 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003273 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3276 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3281 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003282 }
3283 }
3284#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003285}
3286
3287/*
3288 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003289 */
3290int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3291{
3292 int ret = 0;
3293
3294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3295
3296 ret = mbedtls_ssl_flight_transmit( ssl );
3297
3298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3299
3300 return( ret );
3301}
3302
3303/*
3304 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305 *
3306 * Need to remember the current message in case flush_output returns
3307 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003308 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003310int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003312 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003316 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003318
3319 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003320 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003321 ssl_swap_epochs( ssl );
3322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003324 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325
3326 while( ssl->handshake->cur_msg != NULL )
3327 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003328 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003329 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003330
Hanno Beckere1dcb032018-08-17 16:47:58 +01003331 int const is_finished =
3332 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3333 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3334
Hanno Becker04da1892018-08-14 13:22:10 +01003335 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3336 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3337
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003338 /* Swap epochs before sending Finished: we can't do it after
3339 * sending ChangeCipherSpec, in case write returns WANT_READ.
3340 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003341 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003342 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003344 ssl_swap_epochs( ssl );
3345 }
3346
Hanno Becker67bc7c32018-08-06 11:33:50 +01003347 ret = ssl_get_remaining_payload_in_datagram( ssl );
3348 if( ret < 0 )
3349 return( ret );
3350 max_frag_len = (size_t) ret;
3351
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003352 /* CCS is copied as is, while HS messages may need fragmentation */
3353 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3354 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003355 if( max_frag_len == 0 )
3356 {
3357 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3358 return( ret );
3359
3360 continue;
3361 }
3362
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003363 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003364 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003365 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003366
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003367 /* Update position inside current message */
3368 ssl->handshake->cur_msg_p += cur->len;
3369 }
3370 else
3371 {
3372 const unsigned char * const p = ssl->handshake->cur_msg_p;
3373 const size_t hs_len = cur->len - 12;
3374 const size_t frag_off = p - ( cur->p + 12 );
3375 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003376 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003377
Hanno Beckere1dcb032018-08-17 16:47:58 +01003378 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003379 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003380 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003381 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003382
Hanno Becker67bc7c32018-08-06 11:33:50 +01003383 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3384 return( ret );
3385
3386 continue;
3387 }
3388 max_hs_frag_len = max_frag_len - 12;
3389
3390 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3391 max_hs_frag_len : rem_len;
3392
3393 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003394 {
3395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003396 (unsigned) cur_hs_frag_len,
3397 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003398 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003399
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003400 /* Messages are stored with handshake headers as if not fragmented,
3401 * copy beginning of headers then fill fragmentation fields.
3402 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3403 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003404
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003405 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3406 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3407 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3408
Hanno Becker67bc7c32018-08-06 11:33:50 +01003409 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3410 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3411 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003412
3413 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3414
Hanno Becker3f7b9732018-08-28 09:53:25 +01003415 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3417 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003418 ssl->out_msgtype = cur->type;
3419
3420 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003421 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003422 }
3423
3424 /* If done with the current message move to the next one if any */
3425 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3426 {
3427 if( cur->next != NULL )
3428 {
3429 ssl->handshake->cur_msg = cur->next;
3430 ssl->handshake->cur_msg_p = cur->next->p + 12;
3431 }
3432 else
3433 {
3434 ssl->handshake->cur_msg = NULL;
3435 ssl->handshake->cur_msg_p = NULL;
3436 }
3437 }
3438
3439 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003440 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443 return( ret );
3444 }
3445 }
3446
Hanno Becker67bc7c32018-08-06 11:33:50 +01003447 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3448 return( ret );
3449
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003450 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003453 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003456 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3457 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003458
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460
3461 return( 0 );
3462}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003463
3464/*
3465 * To be called when the last message of an incoming flight is received.
3466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003468{
3469 /* We won't need to resend that one any more */
3470 ssl_flight_free( ssl->handshake->flight );
3471 ssl->handshake->flight = NULL;
3472 ssl->handshake->cur_msg = NULL;
3473
3474 /* The next incoming flight will start with this msg_seq */
3475 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3476
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003477 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003478 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003479
Hanno Becker0271f962018-08-16 13:23:47 +01003480 /* Clear future message buffering structure. */
3481 ssl_buffering_free( ssl );
3482
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003483 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003484 ssl_set_timer( ssl, 0 );
3485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3487 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003490 }
3491 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003493}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003494
3495/*
3496 * To be called when the last message of an outgoing flight is send.
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003499{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003500 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003501 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3504 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003507 }
3508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
Paul Bakker5121ce52009-01-03 21:22:43 +00003513/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003514 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003516
3517/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003518 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003519 *
3520 * - fill in handshake headers
3521 * - update handshake checksum
3522 * - DTLS: save message for resending
3523 * - then pass to the record layer
3524 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003525 * DTLS: except for HelloRequest, messages are only queued, and will only be
3526 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003527 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003528 * Inputs:
3529 * - ssl->out_msglen: 4 + actual handshake message len
3530 * (4 is the size of handshake headers for TLS)
3531 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3532 * - ssl->out_msg + 4: the handshake message body
3533 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003534 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003535 * - ssl->out_msglen: the length of the record contents
3536 * (including handshake headers but excluding record headers)
3537 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003538 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003539int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003540{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003541 int ret;
3542 const size_t hs_len = ssl->out_msglen - 4;
3543 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3546
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003547 /*
3548 * Sanity checks
3549 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003550 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003551 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3552 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003553 /* In SSLv3, the client might send a NoCertificate alert. */
3554#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3555 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3556 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3557 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3558#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3559 {
3560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3562 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003565 /* Whenever we send anything different from a
3566 * HelloRequest we should be in a handshake - double check. */
3567 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3568 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003569 ssl->handshake == NULL )
3570 {
3571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003576 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003583#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003584
Hanno Beckerb50a2532018-08-06 11:52:54 +01003585 /* Double-check that we did not exceed the bounds
3586 * of the outgoing record buffer.
3587 * This should never fail as the various message
3588 * writing functions must obey the bounds of the
3589 * outgoing record buffer, but better be safe.
3590 *
3591 * Note: We deliberately do not check for the MTU or MFL here.
3592 */
3593 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3594 {
3595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3596 "size %u, maximum %u",
3597 (unsigned) ssl->out_msglen,
3598 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3600 }
3601
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003602 /*
3603 * Fill handshake headers
3604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003607 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3608 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3609 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003611 /*
3612 * DTLS has additional fields in the Handshake layer,
3613 * between the length field and the actual payload:
3614 * uint16 message_seq;
3615 * uint24 fragment_offset;
3616 * uint24 fragment_length;
3617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003620 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003621 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003622 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003623 {
3624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3625 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003626 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003627 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3629 }
3630
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003631 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003632 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003633
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003634 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003635 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003636 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003637 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3638 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3639 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003640 }
3641 else
3642 {
3643 ssl->out_msg[4] = 0;
3644 ssl->out_msg[5] = 0;
3645 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003646
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003647 /* Handshake hashes are computed without fragmentation,
3648 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003649 memset( ssl->out_msg + 6, 0x00, 3 );
3650 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003653
Hanno Becker0207e532018-08-28 10:28:28 +01003654 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003655 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3656 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 }
3658
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003659 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003662 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3663 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003664 {
3665 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003668 return( ret );
3669 }
3670 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003671 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003672#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003673 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003674 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003675 {
3676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3677 return( ret );
3678 }
3679 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003680
3681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3682
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003683 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003684}
3685
3686/*
3687 * Record layer functions
3688 */
3689
3690/*
3691 * Write current record.
3692 *
3693 * Uses:
3694 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3695 * - ssl->out_msglen: length of the record content (excl headers)
3696 * - ssl->out_msg: record content
3697 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003698int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003699{
3700 int ret, done = 0;
3701 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003702 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003703
3704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003707 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003709 {
3710 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003713 return( ret );
3714 }
3715
3716 len = ssl->out_msglen;
3717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3721 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 ret = mbedtls_ssl_hw_record_write( ssl );
3726 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3729 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003730 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003731
3732 if( ret == 0 )
3733 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003736 if( !done )
3737 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003738 unsigned i;
3739 size_t protected_record_size;
3740
Paul Bakker05ef8352012-05-08 09:17:57 +00003741 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003743 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003744
Hanno Becker19859472018-08-06 09:40:20 +01003745 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003746 ssl->out_len[0] = (unsigned char)( len >> 8 );
3747 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003748
Paul Bakker48916f92012-09-16 19:57:18 +00003749 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003750 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003751 mbedtls_record rec;
3752
3753 rec.buf = ssl->out_iv;
3754 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3755 ( ssl->out_iv - ssl->out_buf );
3756 rec.data_len = ssl->out_msglen;
3757 rec.data_offset = ssl->out_msg - rec.buf;
3758
3759 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3760 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3761 ssl->conf->transport, rec.ver );
3762 rec.type = ssl->out_msgtype;
3763
Hanno Beckera18d1322018-01-03 14:27:32 +00003764 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003765 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003768 return( ret );
3769 }
3770
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003771 if( rec.data_offset != 0 )
3772 {
3773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3775 }
3776
Hanno Becker78f839d2019-03-14 12:56:23 +00003777 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003778 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3779 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003780 }
3781
Hanno Becker2b1e3542018-08-06 11:19:13 +01003782 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3783
3784#if defined(MBEDTLS_SSL_PROTO_DTLS)
3785 /* In case of DTLS, double-check that we don't exceed
3786 * the remaining space in the datagram. */
3787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3788 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003789 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003790 if( ret < 0 )
3791 return( ret );
3792
3793 if( protected_record_size > (size_t) ret )
3794 {
3795 /* Should never happen */
3796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3797 }
3798 }
3799#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003802 "version = [%d:%d], msglen = %d",
3803 ssl->out_hdr[0], ssl->out_hdr[1],
3804 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003807 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003808
3809 ssl->out_left += protected_record_size;
3810 ssl->out_hdr += protected_record_size;
3811 ssl_update_out_pointers( ssl, ssl->transform_out );
3812
Hanno Becker04484622018-08-06 09:49:38 +01003813 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3814 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3815 break;
3816
3817 /* The loop goes to its end iff the counter is wrapping */
3818 if( i == ssl_ep_len( ssl ) )
3819 {
3820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3821 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3822 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 }
3824
Hanno Becker67bc7c32018-08-06 11:33:50 +01003825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3827 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003828 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003829 size_t remaining;
3830 ret = ssl_get_remaining_payload_in_datagram( ssl );
3831 if( ret < 0 )
3832 {
3833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3834 ret );
3835 return( ret );
3836 }
3837
3838 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003840 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003841 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003842 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003843 else
3844 {
Hanno Becker513815a2018-08-20 11:56:09 +01003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003846 }
3847 }
3848#endif /* MBEDTLS_SSL_PROTO_DTLS */
3849
3850 if( ( flush == SSL_FORCE_FLUSH ) &&
3851 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 return( ret );
3855 }
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
3859 return( 0 );
3860}
3861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003863
3864static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3865{
3866 if( ssl->in_msglen < ssl->in_hslen ||
3867 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3868 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3869 {
3870 return( 1 );
3871 }
3872 return( 0 );
3873}
Hanno Becker44650b72018-08-16 12:51:11 +01003874
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003875static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003876{
3877 return( ( ssl->in_msg[9] << 16 ) |
3878 ( ssl->in_msg[10] << 8 ) |
3879 ssl->in_msg[11] );
3880}
3881
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003882static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003883{
3884 return( ( ssl->in_msg[6] << 16 ) |
3885 ( ssl->in_msg[7] << 8 ) |
3886 ssl->in_msg[8] );
3887}
3888
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003889static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003890{
3891 uint32_t msg_len, frag_off, frag_len;
3892
3893 msg_len = ssl_get_hs_total_len( ssl );
3894 frag_off = ssl_get_hs_frag_off( ssl );
3895 frag_len = ssl_get_hs_frag_len( ssl );
3896
3897 if( frag_off > msg_len )
3898 return( -1 );
3899
3900 if( frag_len > msg_len - frag_off )
3901 return( -1 );
3902
3903 if( frag_len + 12 > ssl->in_msglen )
3904 return( -1 );
3905
3906 return( 0 );
3907}
3908
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003909/*
3910 * Mark bits in bitmask (used for DTLS HS reassembly)
3911 */
3912static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3913{
3914 unsigned int start_bits, end_bits;
3915
3916 start_bits = 8 - ( offset % 8 );
3917 if( start_bits != 8 )
3918 {
3919 size_t first_byte_idx = offset / 8;
3920
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003921 /* Special case */
3922 if( len <= start_bits )
3923 {
3924 for( ; len != 0; len-- )
3925 mask[first_byte_idx] |= 1 << ( start_bits - len );
3926
3927 /* Avoid potential issues with offset or len becoming invalid */
3928 return;
3929 }
3930
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003931 offset += start_bits; /* Now offset % 8 == 0 */
3932 len -= start_bits;
3933
3934 for( ; start_bits != 0; start_bits-- )
3935 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3936 }
3937
3938 end_bits = len % 8;
3939 if( end_bits != 0 )
3940 {
3941 size_t last_byte_idx = ( offset + len ) / 8;
3942
3943 len -= end_bits; /* Now len % 8 == 0 */
3944
3945 for( ; end_bits != 0; end_bits-- )
3946 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3947 }
3948
3949 memset( mask + offset / 8, 0xFF, len / 8 );
3950}
3951
3952/*
3953 * Check that bitmask is full
3954 */
3955static int ssl_bitmask_check( unsigned char *mask, size_t len )
3956{
3957 size_t i;
3958
3959 for( i = 0; i < len / 8; i++ )
3960 if( mask[i] != 0xFF )
3961 return( -1 );
3962
3963 for( i = 0; i < len % 8; i++ )
3964 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3965 return( -1 );
3966
3967 return( 0 );
3968}
3969
Hanno Becker56e205e2018-08-16 09:06:12 +01003970/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003971static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003972 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003973{
Hanno Becker56e205e2018-08-16 09:06:12 +01003974 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003975
Hanno Becker56e205e2018-08-16 09:06:12 +01003976 alloc_len = 12; /* Handshake header */
3977 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003978
Hanno Beckerd07df862018-08-16 09:14:58 +01003979 if( add_bitmap )
3980 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003981
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003982 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003983}
Hanno Becker56e205e2018-08-16 09:06:12 +01003984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003986
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003987static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003988{
3989 return( ( ssl->in_msg[1] << 16 ) |
3990 ( ssl->in_msg[2] << 8 ) |
3991 ssl->in_msg[3] );
3992}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003993
Simon Butcher99000142016-10-13 17:21:01 +01003994int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003999 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004001 }
4002
Hanno Becker12555c62018-08-16 12:47:53 +01004003 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004006 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004007 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004011 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004012 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004013 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004014
Hanno Becker44650b72018-08-16 12:51:11 +01004015 if( ssl_check_hs_header( ssl ) != 0 )
4016 {
4017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4019 }
4020
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004021 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004022 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4023 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4024 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4025 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004026 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004027 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4028 {
4029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4030 recv_msg_seq,
4031 ssl->handshake->in_msg_seq ) );
4032 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4033 }
4034
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004035 /* Retransmit only on last message from previous flight, to avoid
4036 * too many retransmissions.
4037 * Besides, No sane server ever retransmits HelloVerifyRequest */
4038 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004042 "message_seq = %d, start_of_flight = %d",
4043 recv_msg_seq,
4044 ssl->handshake->in_flight_start_seq ) );
4045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004049 return( ret );
4050 }
4051 }
4052 else
4053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004055 "message_seq = %d, expected = %d",
4056 recv_msg_seq,
4057 ssl->handshake->in_msg_seq ) );
4058 }
4059
Hanno Becker90333da2017-10-10 11:27:13 +01004060 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004061 }
4062 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004063
Hanno Becker6d97ef52018-08-16 13:09:04 +01004064 /* Message reassembly is handled alongside buffering of future
4065 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004066 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004067 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004068 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004071 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004072 }
4073 }
4074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004076 /* With TLS we don't handle fragmentation (for now) */
4077 if( ssl->in_msglen < ssl->in_hslen )
4078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4080 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004081 }
4082
Simon Butcher99000142016-10-13 17:21:01 +01004083 return( 0 );
4084}
4085
4086void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4087{
Hanno Becker0271f962018-08-16 13:23:47 +01004088 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004089
Hanno Becker0271f962018-08-16 13:23:47 +01004090 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004091 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004092 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004093 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004094
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004095 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004098 ssl->handshake != NULL )
4099 {
Hanno Becker0271f962018-08-16 13:23:47 +01004100 unsigned offset;
4101 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004102
Hanno Becker0271f962018-08-16 13:23:47 +01004103 /* Increment handshake sequence number */
4104 hs->in_msg_seq++;
4105
4106 /*
4107 * Clear up handshake buffering and reassembly structure.
4108 */
4109
4110 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004111 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004112
4113 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004114 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4115 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004116 offset++, hs_buf++ )
4117 {
4118 *hs_buf = *(hs_buf + 1);
4119 }
4120
4121 /* Create a fresh last entry */
4122 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004123 }
4124#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004125}
4126
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004127/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004128 * DTLS anti-replay: RFC 6347 4.1.2.6
4129 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004130 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4131 * Bit n is set iff record number in_window_top - n has been seen.
4132 *
4133 * Usually, in_window_top is the last record number seen and the lsb of
4134 * in_window is set. The only exception is the initial state (record number 0
4135 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4138static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004139{
4140 ssl->in_window_top = 0;
4141 ssl->in_window = 0;
4142}
4143
4144static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4145{
4146 return( ( (uint64_t) buf[0] << 40 ) |
4147 ( (uint64_t) buf[1] << 32 ) |
4148 ( (uint64_t) buf[2] << 24 ) |
4149 ( (uint64_t) buf[3] << 16 ) |
4150 ( (uint64_t) buf[4] << 8 ) |
4151 ( (uint64_t) buf[5] ) );
4152}
4153
4154/*
4155 * Return 0 if sequence number is acceptable, -1 otherwise
4156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004158{
4159 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4160 uint64_t bit;
4161
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004162 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004163 return( 0 );
4164
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004165 if( rec_seqnum > ssl->in_window_top )
4166 return( 0 );
4167
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004168 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004169
4170 if( bit >= 64 )
4171 return( -1 );
4172
4173 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4174 return( -1 );
4175
4176 return( 0 );
4177}
4178
4179/*
4180 * Update replay window on new validated record
4181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004183{
4184 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4185
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004186 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004187 return;
4188
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004189 if( rec_seqnum > ssl->in_window_top )
4190 {
4191 /* Update window_top and the contents of the window */
4192 uint64_t shift = rec_seqnum - ssl->in_window_top;
4193
4194 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004195 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004196 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004197 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004198 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004199 ssl->in_window |= 1;
4200 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004201
4202 ssl->in_window_top = rec_seqnum;
4203 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004204 else
4205 {
4206 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004207 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004208
4209 if( bit < 64 ) /* Always true, but be extra sure */
4210 ssl->in_window |= (uint64_t) 1 << bit;
4211 }
4212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004214
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004215#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004216/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004217static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4218
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004219/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004220 * Without any SSL context, check if a datagram looks like a ClientHello with
4221 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004222 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004223 *
4224 * - if cookie is valid, return 0
4225 * - if ClientHello looks superficially valid but cookie is not,
4226 * fill obuf and set olen, then
4227 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4228 * - otherwise return a specific error code
4229 */
4230static int ssl_check_dtls_clihlo_cookie(
4231 mbedtls_ssl_cookie_write_t *f_cookie_write,
4232 mbedtls_ssl_cookie_check_t *f_cookie_check,
4233 void *p_cookie,
4234 const unsigned char *cli_id, size_t cli_id_len,
4235 const unsigned char *in, size_t in_len,
4236 unsigned char *obuf, size_t buf_len, size_t *olen )
4237{
4238 size_t sid_len, cookie_len;
4239 unsigned char *p;
4240
4241 if( f_cookie_write == NULL || f_cookie_check == NULL )
4242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4243
4244 /*
4245 * Structure of ClientHello with record and handshake headers,
4246 * and expected values. We don't need to check a lot, more checks will be
4247 * done when actually parsing the ClientHello - skipping those checks
4248 * avoids code duplication and does not make cookie forging any easier.
4249 *
4250 * 0-0 ContentType type; copied, must be handshake
4251 * 1-2 ProtocolVersion version; copied
4252 * 3-4 uint16 epoch; copied, must be 0
4253 * 5-10 uint48 sequence_number; copied
4254 * 11-12 uint16 length; (ignored)
4255 *
4256 * 13-13 HandshakeType msg_type; (ignored)
4257 * 14-16 uint24 length; (ignored)
4258 * 17-18 uint16 message_seq; copied
4259 * 19-21 uint24 fragment_offset; copied, must be 0
4260 * 22-24 uint24 fragment_length; (ignored)
4261 *
4262 * 25-26 ProtocolVersion client_version; (ignored)
4263 * 27-58 Random random; (ignored)
4264 * 59-xx SessionID session_id; 1 byte len + sid_len content
4265 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4266 * ...
4267 *
4268 * Minimum length is 61 bytes.
4269 */
4270 if( in_len < 61 ||
4271 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4272 in[3] != 0 || in[4] != 0 ||
4273 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4274 {
4275 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4276 }
4277
4278 sid_len = in[59];
4279 if( sid_len > in_len - 61 )
4280 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4281
4282 cookie_len = in[60 + sid_len];
4283 if( cookie_len > in_len - 60 )
4284 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4285
4286 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4287 cli_id, cli_id_len ) == 0 )
4288 {
4289 /* Valid cookie */
4290 return( 0 );
4291 }
4292
4293 /*
4294 * If we get here, we've got an invalid cookie, let's prepare HVR.
4295 *
4296 * 0-0 ContentType type; copied
4297 * 1-2 ProtocolVersion version; copied
4298 * 3-4 uint16 epoch; copied
4299 * 5-10 uint48 sequence_number; copied
4300 * 11-12 uint16 length; olen - 13
4301 *
4302 * 13-13 HandshakeType msg_type; hello_verify_request
4303 * 14-16 uint24 length; olen - 25
4304 * 17-18 uint16 message_seq; copied
4305 * 19-21 uint24 fragment_offset; copied
4306 * 22-24 uint24 fragment_length; olen - 25
4307 *
4308 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4309 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4310 *
4311 * Minimum length is 28.
4312 */
4313 if( buf_len < 28 )
4314 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4315
4316 /* Copy most fields and adapt others */
4317 memcpy( obuf, in, 25 );
4318 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4319 obuf[25] = 0xfe;
4320 obuf[26] = 0xff;
4321
4322 /* Generate and write actual cookie */
4323 p = obuf + 28;
4324 if( f_cookie_write( p_cookie,
4325 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4326 {
4327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4328 }
4329
4330 *olen = p - obuf;
4331
4332 /* Go back and fill length fields */
4333 obuf[27] = (unsigned char)( *olen - 28 );
4334
4335 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4336 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4337 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4338
4339 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4340 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4341
4342 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4343}
4344
4345/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004346 * Handle possible client reconnect with the same UDP quadruplet
4347 * (RFC 6347 Section 4.2.8).
4348 *
4349 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4350 * that looks like a ClientHello.
4351 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004352 * - if the input looks like a ClientHello without cookies,
4353 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004354 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004355 * - if the input looks like a ClientHello with a valid cookie,
4356 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004357 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004358 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004359 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004360 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004361 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4362 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004363 */
4364static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4365{
4366 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004367 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004368
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004369 ret = ssl_check_dtls_clihlo_cookie(
4370 ssl->conf->f_cookie_write,
4371 ssl->conf->f_cookie_check,
4372 ssl->conf->p_cookie,
4373 ssl->cli_id, ssl->cli_id_len,
4374 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004375 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4378
4379 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004380 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004381 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004382 * If the error is permanent we'll catch it later,
4383 * if it's not, then hopefully it'll work next time. */
4384 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4385
4386 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004387 }
4388
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004389 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004391 /* Got a valid cookie, partially reset context */
4392 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4393 {
4394 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4395 return( ret );
4396 }
4397
4398 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004399 }
4400
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004401 return( ret );
4402}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004403#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004404
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004405/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004406 * ContentType type;
4407 * ProtocolVersion version;
4408 * uint16 epoch; // DTLS only
4409 * uint48 sequence_number; // DTLS only
4410 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004411 *
4412 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004413 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004414 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4415 *
4416 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004417 * 1. proceed with the record if this function returns 0
4418 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4419 * 3. return CLIENT_RECONNECT if this function return that value
4420 * 4. drop the whole datagram if this function returns anything else.
4421 * Point 2 is needed when the peer is resending, and we have already received
4422 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004425{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004426 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 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 +02004429
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004431 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004432 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004436 ssl->in_msgtype,
4437 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004438
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004439 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4441 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4442 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4443 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004446
4447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004448 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4449 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004450 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4451#endif /* MBEDTLS_SSL_PROTO_DTLS */
4452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4453 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004456 }
4457
4458 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004459 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4462 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 }
4464
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004465 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4468 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 }
4470
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004471 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004472 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004473 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4476 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004477 }
4478
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004479 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004480 * DTLS-related tests.
4481 * Check epoch before checking length constraint because
4482 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4483 * message gets duplicated before the corresponding Finished message,
4484 * the second ChangeCipherSpec should be discarded because it belongs
4485 * to an old epoch, but not because its length is shorter than
4486 * the minimum record length for packets using the new record transform.
4487 * Note that these two kinds of failures are handled differently,
4488 * as an unexpected record is silently skipped but an invalid
4489 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004490 */
4491#if defined(MBEDTLS_SSL_PROTO_DTLS)
4492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4493 {
4494 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4495
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004496 /* Check epoch (and sequence number) with DTLS */
4497 if( rec_epoch != ssl->in_epoch )
4498 {
4499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4500 "expected %d, received %d",
4501 ssl->in_epoch, rec_epoch ) );
4502
4503#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4504 /*
4505 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4506 * access the first byte of record content (handshake type), as we
4507 * have an active transform (possibly iv_len != 0), so use the
4508 * fact that the record header len is 13 instead.
4509 */
4510 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4511 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4512 rec_epoch == 0 &&
4513 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4514 ssl->in_left > 13 &&
4515 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4516 {
4517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4518 "from the same port" ) );
4519 return( ssl_handle_possible_reconnect( ssl ) );
4520 }
4521 else
4522#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004523 {
4524 /* Consider buffering the record. */
4525 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4526 {
4527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4528 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4529 }
4530
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004531 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004532 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004533 }
4534
4535#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4536 /* Replay detection only works for the current epoch */
4537 if( rec_epoch == ssl->in_epoch &&
4538 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4539 {
4540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4542 }
4543#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004544
Hanno Becker52c6dc62017-05-26 16:07:36 +01004545 /* Drop unexpected ApplicationData records,
4546 * except at the beginning of renegotiations */
4547 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4548 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4549#if defined(MBEDTLS_SSL_RENEGOTIATION)
4550 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4551 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4552#endif
4553 )
4554 {
4555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4556 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4557 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004558 }
4559#endif /* MBEDTLS_SSL_PROTO_DTLS */
4560
Hanno Becker52c6dc62017-05-26 16:07:36 +01004561
4562 /* Check length against bounds of the current transform and version */
4563 if( ssl->transform_in == NULL )
4564 {
4565 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004566 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004567 {
4568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4570 }
4571 }
4572 else
4573 {
4574 if( ssl->in_msglen < ssl->transform_in->minlen )
4575 {
4576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4577 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4578 }
4579
4580#if defined(MBEDTLS_SSL_PROTO_SSL3)
4581 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004582 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004583 {
4584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4585 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4586 }
4587#endif
4588#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4589 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4590 /*
4591 * TLS encrypted messages can have up to 256 bytes of padding
4592 */
4593 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4594 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004595 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4598 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4599 }
4600#endif
4601 }
4602
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004603 return( 0 );
4604}
Paul Bakker5121ce52009-01-03 21:22:43 +00004605
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004606/*
4607 * If applicable, decrypt (and decompress) record content
4608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004610{
4611 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4614 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4617 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 ret = mbedtls_ssl_hw_record_read( ssl );
4622 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4625 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004626 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004627
4628 if( ret == 0 )
4629 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004632 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004634 mbedtls_record rec;
4635
4636 rec.buf = ssl->in_iv;
4637 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4638 - ( ssl->in_iv - ssl->in_buf );
4639 rec.data_len = ssl->in_msglen;
4640 rec.data_offset = 0;
4641
4642 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4643 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4644 ssl->conf->transport, rec.ver );
4645 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004646 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4647 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004650 return( ret );
4651 }
4652
Hanno Becker29800d22018-08-07 14:30:18 +01004653 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4654 {
4655 /* Should never happen */
4656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004659 ssl->in_msglen = rec.data_len;
4660 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4661 ssl->in_len[1] = (unsigned char)( rec.data_len );
4662
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004663 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4664 ssl->in_msg, ssl->in_msglen );
4665
Angus Grattond8213d02016-05-25 20:56:48 +10004666 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004671 else if( ssl->in_msglen == 0 )
4672 {
4673#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4674 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4675 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4676 {
4677 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4679 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4680 }
4681#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4682
4683 ssl->nb_zero++;
4684
4685 /*
4686 * Three or more empty messages may be a DoS attack
4687 * (excessive CPU consumption).
4688 */
4689 if( ssl->nb_zero > 3 )
4690 {
4691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4692 "messages, possible DoS attack" ) );
4693 /* Q: Is that the right error code? */
4694 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4695 }
4696 }
4697 else
4698 ssl->nb_zero = 0;
4699
4700#if defined(MBEDTLS_SSL_PROTO_DTLS)
4701 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4702 {
4703 ; /* in_ctr read from peer, not maintained internally */
4704 }
4705 else
4706#endif
4707 {
4708 unsigned i;
4709 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4710 if( ++ssl->in_ctr[i - 1] != 0 )
4711 break;
4712
4713 /* The loop goes to its end iff the counter is wrapping */
4714 if( i == ssl_ep_len( ssl ) )
4715 {
4716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4717 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4718 }
4719 }
4720
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 }
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004724 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004726 {
4727 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004730 return( ret );
4731 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004739 }
4740#endif
4741
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004742 return( 0 );
4743}
4744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004746
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004747/*
4748 * Read a record.
4749 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004750 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4751 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4752 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004753 */
Hanno Becker1097b342018-08-15 14:09:41 +01004754
4755/* Helper functions for mbedtls_ssl_read_record(). */
4756static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004757static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4758static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004759
Hanno Becker327c93b2018-08-15 13:56:18 +01004760int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004761 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004762{
4763 int ret;
4764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004766
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004767 if( ssl->keep_current_message == 0 )
4768 {
4769 do {
Simon Butcher99000142016-10-13 17:21:01 +01004770
Hanno Becker26994592018-08-15 14:14:59 +01004771 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004772 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004773 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004774
Hanno Beckere74d5562018-08-15 14:26:08 +01004775 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004776 {
Hanno Becker40f50842018-08-15 14:48:01 +01004777#if defined(MBEDTLS_SSL_PROTO_DTLS)
4778 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004779
Hanno Becker40f50842018-08-15 14:48:01 +01004780 /* We only check for buffered messages if the
4781 * current datagram is fully consumed. */
4782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004783 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004784 {
Hanno Becker40f50842018-08-15 14:48:01 +01004785 if( ssl_load_buffered_message( ssl ) == 0 )
4786 have_buffered = 1;
4787 }
4788
4789 if( have_buffered == 0 )
4790#endif /* MBEDTLS_SSL_PROTO_DTLS */
4791 {
4792 ret = ssl_get_next_record( ssl );
4793 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4794 continue;
4795
4796 if( ret != 0 )
4797 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004798 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004799 return( ret );
4800 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004801 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004802 }
4803
4804 ret = mbedtls_ssl_handle_message_type( ssl );
4805
Hanno Becker40f50842018-08-15 14:48:01 +01004806#if defined(MBEDTLS_SSL_PROTO_DTLS)
4807 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4808 {
4809 /* Buffer future message */
4810 ret = ssl_buffer_message( ssl );
4811 if( ret != 0 )
4812 return( ret );
4813
4814 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4815 }
4816#endif /* MBEDTLS_SSL_PROTO_DTLS */
4817
Hanno Becker90333da2017-10-10 11:27:13 +01004818 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4819 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004820
4821 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004822 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004823 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004824 return( ret );
4825 }
4826
Hanno Becker327c93b2018-08-15 13:56:18 +01004827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004828 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004829 {
4830 mbedtls_ssl_update_handshake_status( ssl );
4831 }
Simon Butcher99000142016-10-13 17:21:01 +01004832 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004833 else
Simon Butcher99000142016-10-13 17:21:01 +01004834 {
Hanno Becker02f59072018-08-15 14:00:24 +01004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004836 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004837 }
4838
4839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4840
4841 return( 0 );
4842}
4843
Hanno Becker40f50842018-08-15 14:48:01 +01004844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004845static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004846{
Hanno Becker40f50842018-08-15 14:48:01 +01004847 if( ssl->in_left > ssl->next_record_offset )
4848 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004849
Hanno Becker40f50842018-08-15 14:48:01 +01004850 return( 0 );
4851}
4852
4853static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4854{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004855 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004856 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004857 int ret = 0;
4858
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004859 if( hs == NULL )
4860 return( -1 );
4861
Hanno Beckere00ae372018-08-20 09:39:42 +01004862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4863
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004864 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4865 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4866 {
4867 /* Check if we have seen a ChangeCipherSpec before.
4868 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004869 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4872 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004873 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004874 }
4875
Hanno Becker39b8bc92018-08-28 17:17:13 +01004876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004877 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4878 ssl->in_msglen = 1;
4879 ssl->in_msg[0] = 1;
4880
4881 /* As long as they are equal, the exact value doesn't matter. */
4882 ssl->in_left = 0;
4883 ssl->next_record_offset = 0;
4884
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004885 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004886 goto exit;
4887 }
Hanno Becker37f95322018-08-16 13:55:32 +01004888
Hanno Beckerb8f50142018-08-28 10:01:34 +01004889#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004890 /* Debug only */
4891 {
4892 unsigned offset;
4893 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4894 {
4895 hs_buf = &hs->buffering.hs[offset];
4896 if( hs_buf->is_valid == 1 )
4897 {
4898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4899 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004900 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004901 }
4902 }
4903 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004904#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004905
4906 /* Check if we have buffered and/or fully reassembled the
4907 * next handshake message. */
4908 hs_buf = &hs->buffering.hs[0];
4909 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4910 {
4911 /* Synthesize a record containing the buffered HS message. */
4912 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4913 ( hs_buf->data[2] << 8 ) |
4914 hs_buf->data[3];
4915
4916 /* Double-check that we haven't accidentally buffered
4917 * a message that doesn't fit into the input buffer. */
4918 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4919 {
4920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4922 }
4923
4924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4925 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4926 hs_buf->data, msg_len + 12 );
4927
4928 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4929 ssl->in_hslen = msg_len + 12;
4930 ssl->in_msglen = msg_len + 12;
4931 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4932
4933 ret = 0;
4934 goto exit;
4935 }
4936 else
4937 {
4938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4939 hs->in_msg_seq ) );
4940 }
4941
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004942 ret = -1;
4943
4944exit:
4945
4946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4947 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004948}
4949
Hanno Beckera02b0b42018-08-21 17:20:27 +01004950static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4951 size_t desired )
4952{
4953 int offset;
4954 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4956 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004957
Hanno Becker01315ea2018-08-21 17:22:17 +01004958 /* Get rid of future records epoch first, if such exist. */
4959 ssl_free_buffered_record( ssl );
4960
4961 /* Check if we have enough space available now. */
4962 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4963 hs->buffering.total_bytes_buffered ) )
4964 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004966 return( 0 );
4967 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004968
Hanno Becker4f432ad2018-08-28 10:02:32 +01004969 /* We don't have enough space to buffer the next expected handshake
4970 * message. Remove buffers used for future messages to gain space,
4971 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004972 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4973 offset >= 0; offset-- )
4974 {
4975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4976 offset ) );
4977
Hanno Beckerb309b922018-08-23 13:18:05 +01004978 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004979
4980 /* Check if we have enough space available now. */
4981 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4982 hs->buffering.total_bytes_buffered ) )
4983 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004985 return( 0 );
4986 }
4987 }
4988
4989 return( -1 );
4990}
4991
Hanno Becker40f50842018-08-15 14:48:01 +01004992static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4993{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004994 int ret = 0;
4995 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4996
4997 if( hs == NULL )
4998 return( 0 );
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5001
5002 switch( ssl->in_msgtype )
5003 {
5004 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005006
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005007 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005008 break;
5009
5010 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005011 {
5012 unsigned recv_msg_seq_offset;
5013 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5014 mbedtls_ssl_hs_buffer *hs_buf;
5015 size_t msg_len = ssl->in_hslen - 12;
5016
5017 /* We should never receive an old handshake
5018 * message - double-check nonetheless. */
5019 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5020 {
5021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5023 }
5024
5025 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5026 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5027 {
5028 /* Silently ignore -- message too far in the future */
5029 MBEDTLS_SSL_DEBUG_MSG( 2,
5030 ( "Ignore future HS message with sequence number %u, "
5031 "buffering window %u - %u",
5032 recv_msg_seq, ssl->handshake->in_msg_seq,
5033 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5034
5035 goto exit;
5036 }
5037
5038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5039 recv_msg_seq, recv_msg_seq_offset ) );
5040
5041 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5042
5043 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005044 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005045 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005046 size_t reassembly_buf_sz;
5047
Hanno Becker37f95322018-08-16 13:55:32 +01005048 hs_buf->is_fragmented =
5049 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5050
5051 /* We copy the message back into the input buffer
5052 * after reassembly, so check that it's not too large.
5053 * This is an implementation-specific limitation
5054 * and not one from the standard, hence it is not
5055 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005056 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005057 {
5058 /* Ignore message */
5059 goto exit;
5060 }
5061
Hanno Beckere0b150f2018-08-21 15:51:03 +01005062 /* Check if we have enough space to buffer the message. */
5063 if( hs->buffering.total_bytes_buffered >
5064 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5065 {
5066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5068 }
5069
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005070 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5071 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005072
5073 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5074 hs->buffering.total_bytes_buffered ) )
5075 {
5076 if( recv_msg_seq_offset > 0 )
5077 {
5078 /* If we can't buffer a future message because
5079 * of space limitations -- ignore. */
5080 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",
5081 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5082 (unsigned) hs->buffering.total_bytes_buffered ) );
5083 goto exit;
5084 }
Hanno Beckere1801392018-08-21 16:51:05 +01005085 else
5086 {
5087 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",
5088 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5089 (unsigned) hs->buffering.total_bytes_buffered ) );
5090 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005091
Hanno Beckera02b0b42018-08-21 17:20:27 +01005092 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005093 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005094 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",
5095 (unsigned) msg_len,
5096 (unsigned) reassembly_buf_sz,
5097 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005098 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005099 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5100 goto exit;
5101 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005102 }
5103
5104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5105 msg_len ) );
5106
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005107 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5108 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005109 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005110 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005111 goto exit;
5112 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005113 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005114
5115 /* Prepare final header: copy msg_type, length and message_seq,
5116 * then add standardised fragment_offset and fragment_length */
5117 memcpy( hs_buf->data, ssl->in_msg, 6 );
5118 memset( hs_buf->data + 6, 0, 3 );
5119 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5120
5121 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005122
5123 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005124 }
5125 else
5126 {
5127 /* Make sure msg_type and length are consistent */
5128 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5129 {
5130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5131 /* Ignore */
5132 goto exit;
5133 }
5134 }
5135
Hanno Becker4422bbb2018-08-20 09:40:19 +01005136 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005137 {
5138 size_t frag_len, frag_off;
5139 unsigned char * const msg = hs_buf->data + 12;
5140
5141 /*
5142 * Check and copy current fragment
5143 */
5144
5145 /* Validation of header fields already done in
5146 * mbedtls_ssl_prepare_handshake_record(). */
5147 frag_off = ssl_get_hs_frag_off( ssl );
5148 frag_len = ssl_get_hs_frag_len( ssl );
5149
5150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5151 frag_off, frag_len ) );
5152 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5153
5154 if( hs_buf->is_fragmented )
5155 {
5156 unsigned char * const bitmask = msg + msg_len;
5157 ssl_bitmask_set( bitmask, frag_off, frag_len );
5158 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5159 msg_len ) == 0 );
5160 }
5161 else
5162 {
5163 hs_buf->is_complete = 1;
5164 }
5165
5166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5167 hs_buf->is_complete ? "" : "not yet " ) );
5168 }
5169
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005170 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005171 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005172
5173 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005174 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005175 break;
5176 }
5177
5178exit:
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5181 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005182}
5183#endif /* MBEDTLS_SSL_PROTO_DTLS */
5184
Hanno Becker1097b342018-08-15 14:09:41 +01005185static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005186{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005187 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005188 * Consume last content-layer message and potentially
5189 * update in_msglen which keeps track of the contents'
5190 * consumption state.
5191 *
5192 * (1) Handshake messages:
5193 * Remove last handshake message, move content
5194 * and adapt in_msglen.
5195 *
5196 * (2) Alert messages:
5197 * Consume whole record content, in_msglen = 0.
5198 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005199 * (3) Change cipher spec:
5200 * Consume whole record content, in_msglen = 0.
5201 *
5202 * (4) Application data:
5203 * Don't do anything - the record layer provides
5204 * the application data as a stream transport
5205 * and consumes through mbedtls_ssl_read only.
5206 *
5207 */
5208
5209 /* Case (1): Handshake messages */
5210 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005211 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005212 /* Hard assertion to be sure that no application data
5213 * is in flight, as corrupting ssl->in_msglen during
5214 * ssl->in_offt != NULL is fatal. */
5215 if( ssl->in_offt != NULL )
5216 {
5217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5219 }
5220
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005221 /*
5222 * Get next Handshake message in the current record
5223 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005224
Hanno Becker4a810fb2017-05-24 16:27:30 +01005225 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005226 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005227 * current handshake content: If DTLS handshake
5228 * fragmentation is used, that's the fragment
5229 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005230 * size here is faulty and should be changed at
5231 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005232 * (2) While it doesn't seem to cause problems, one
5233 * has to be very careful not to assume that in_hslen
5234 * is always <= in_msglen in a sensible communication.
5235 * Again, it's wrong for DTLS handshake fragmentation.
5236 * The following check is therefore mandatory, and
5237 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005238 * Additionally, ssl->in_hslen might be arbitrarily out of
5239 * bounds after handling a DTLS message with an unexpected
5240 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005241 */
5242 if( ssl->in_hslen < ssl->in_msglen )
5243 {
5244 ssl->in_msglen -= ssl->in_hslen;
5245 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5246 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005247
Hanno Becker4a810fb2017-05-24 16:27:30 +01005248 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5249 ssl->in_msg, ssl->in_msglen );
5250 }
5251 else
5252 {
5253 ssl->in_msglen = 0;
5254 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005255
Hanno Becker4a810fb2017-05-24 16:27:30 +01005256 ssl->in_hslen = 0;
5257 }
5258 /* Case (4): Application data */
5259 else if( ssl->in_offt != NULL )
5260 {
5261 return( 0 );
5262 }
5263 /* Everything else (CCS & Alerts) */
5264 else
5265 {
5266 ssl->in_msglen = 0;
5267 }
5268
Hanno Becker1097b342018-08-15 14:09:41 +01005269 return( 0 );
5270}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005271
Hanno Beckere74d5562018-08-15 14:26:08 +01005272static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5273{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005274 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005275 return( 1 );
5276
5277 return( 0 );
5278}
5279
Hanno Becker5f066e72018-08-16 14:56:31 +01005280#if defined(MBEDTLS_SSL_PROTO_DTLS)
5281
5282static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5283{
5284 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5285 if( hs == NULL )
5286 return;
5287
Hanno Becker01315ea2018-08-21 17:22:17 +01005288 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005289 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005290 hs->buffering.total_bytes_buffered -=
5291 hs->buffering.future_record.len;
5292
5293 mbedtls_free( hs->buffering.future_record.data );
5294 hs->buffering.future_record.data = NULL;
5295 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005296}
5297
5298static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5299{
5300 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5301 unsigned char * rec;
5302 size_t rec_len;
5303 unsigned rec_epoch;
5304
5305 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5306 return( 0 );
5307
5308 if( hs == NULL )
5309 return( 0 );
5310
Hanno Becker5f066e72018-08-16 14:56:31 +01005311 rec = hs->buffering.future_record.data;
5312 rec_len = hs->buffering.future_record.len;
5313 rec_epoch = hs->buffering.future_record.epoch;
5314
5315 if( rec == NULL )
5316 return( 0 );
5317
Hanno Becker4cb782d2018-08-20 11:19:05 +01005318 /* Only consider loading future records if the
5319 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005320 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005321 return( 0 );
5322
Hanno Becker5f066e72018-08-16 14:56:31 +01005323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5324
5325 if( rec_epoch != ssl->in_epoch )
5326 {
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5328 goto exit;
5329 }
5330
5331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5332
5333 /* Double-check that the record is not too large */
5334 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5335 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5336 {
5337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5339 }
5340
5341 memcpy( ssl->in_hdr, rec, rec_len );
5342 ssl->in_left = rec_len;
5343 ssl->next_record_offset = 0;
5344
5345 ssl_free_buffered_record( ssl );
5346
5347exit:
5348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5349 return( 0 );
5350}
5351
5352static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5353{
5354 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5355 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005356 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005357
5358 /* Don't buffer future records outside handshakes. */
5359 if( hs == NULL )
5360 return( 0 );
5361
5362 /* Only buffer handshake records (we are only interested
5363 * in Finished messages). */
5364 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5365 return( 0 );
5366
5367 /* Don't buffer more than one future epoch record. */
5368 if( hs->buffering.future_record.data != NULL )
5369 return( 0 );
5370
Hanno Becker01315ea2018-08-21 17:22:17 +01005371 /* Don't buffer record if there's not enough buffering space remaining. */
5372 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5373 hs->buffering.total_bytes_buffered ) )
5374 {
5375 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",
5376 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5377 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005378 return( 0 );
5379 }
5380
Hanno Becker5f066e72018-08-16 14:56:31 +01005381 /* Buffer record */
5382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5383 ssl->in_epoch + 1 ) );
5384 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5385 rec_hdr_len + ssl->in_msglen );
5386
5387 /* ssl_parse_record_header() only considers records
5388 * of the next epoch as candidates for buffering. */
5389 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005390 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005391
5392 hs->buffering.future_record.data =
5393 mbedtls_calloc( 1, hs->buffering.future_record.len );
5394 if( hs->buffering.future_record.data == NULL )
5395 {
5396 /* If we run out of RAM trying to buffer a
5397 * record from the next epoch, just ignore. */
5398 return( 0 );
5399 }
5400
Hanno Becker01315ea2018-08-21 17:22:17 +01005401 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005402
Hanno Becker01315ea2018-08-21 17:22:17 +01005403 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005404 return( 0 );
5405}
5406
5407#endif /* MBEDTLS_SSL_PROTO_DTLS */
5408
Hanno Beckere74d5562018-08-15 14:26:08 +01005409static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005410{
5411 int ret;
5412
Hanno Becker5f066e72018-08-16 14:56:31 +01005413#if defined(MBEDTLS_SSL_PROTO_DTLS)
5414 /* We might have buffered a future record; if so,
5415 * and if the epoch matches now, load it.
5416 * On success, this call will set ssl->in_left to
5417 * the length of the buffered record, so that
5418 * the calls to ssl_fetch_input() below will
5419 * essentially be no-ops. */
5420 ret = ssl_load_buffered_record( ssl );
5421 if( ret != 0 )
5422 return( ret );
5423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005428 return( ret );
5429 }
5430
5431 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5435 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005436 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005437 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5438 {
5439 ret = ssl_buffer_future_record( ssl );
5440 if( ret != 0 )
5441 return( ret );
5442
5443 /* Fall through to handling of unexpected records */
5444 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5445 }
5446
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005447 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5448 {
5449 /* Skip unexpected record (but not whole datagram) */
5450 ssl->next_record_offset = ssl->in_msglen
5451 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005452
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5454 "(header)" ) );
5455 }
5456 else
5457 {
5458 /* Skip invalid record and the rest of the datagram */
5459 ssl->next_record_offset = 0;
5460 ssl->in_left = 0;
5461
5462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5463 "(header)" ) );
5464 }
5465
5466 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005467 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005468 }
5469#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005470 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005471 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005472
5473 /*
5474 * Read and optionally decrypt the message contents
5475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5477 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005480 return( ret );
5481 }
5482
5483 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005488 if( ssl->next_record_offset < ssl->in_left )
5489 {
5490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5491 }
5492 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005493 else
5494#endif
5495 ssl->in_left = 0;
5496
5497 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005501 {
5502 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5504 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005505 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005506 /* Except when waiting for Finished as a bad mac here
5507 * probably means something went wrong in the handshake
5508 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5509 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5510 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5511 {
5512#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5513 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5514 {
5515 mbedtls_ssl_send_alert_message( ssl,
5516 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5517 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5518 }
5519#endif
5520 return( ret );
5521 }
5522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005524 if( ssl->conf->badmac_limit != 0 &&
5525 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5528 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005529 }
5530#endif
5531
Hanno Becker4a810fb2017-05-24 16:27:30 +01005532 /* As above, invalid records cause
5533 * dismissal of the whole datagram. */
5534
5535 ssl->next_record_offset = 0;
5536 ssl->in_left = 0;
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005539 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005540 }
5541
5542 return( ret );
5543 }
5544 else
5545#endif
5546 {
5547 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5549 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 mbedtls_ssl_send_alert_message( ssl,
5552 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5553 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005554 }
5555#endif
5556 return( ret );
5557 }
5558 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005559
Simon Butcher99000142016-10-13 17:21:01 +01005560 return( 0 );
5561}
5562
5563int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5564{
5565 int ret;
5566
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005567 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005568 * Handle particular types of records
5569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005571 {
Simon Butcher99000142016-10-13 17:21:01 +01005572 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5573 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005574 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 }
5577
Hanno Beckere678eaa2018-08-21 14:57:46 +01005578 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005579 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005580 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005581 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5583 ssl->in_msglen ) );
5584 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005585 }
5586
Hanno Beckere678eaa2018-08-21 14:57:46 +01005587 if( ssl->in_msg[0] != 1 )
5588 {
5589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5590 ssl->in_msg[0] ) );
5591 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5592 }
5593
5594#if defined(MBEDTLS_SSL_PROTO_DTLS)
5595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5596 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5597 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5598 {
5599 if( ssl->handshake == NULL )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5602 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5603 }
5604
5605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5606 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5607 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005608#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005609 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005613 if( ssl->in_msglen != 2 )
5614 {
5615 /* Note: Standard allows for more than one 2 byte alert
5616 to be packed in a single message, but Mbed TLS doesn't
5617 currently support this. */
5618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5619 ssl->in_msglen ) );
5620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5621 }
5622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 ssl->in_msg[0], ssl->in_msg[1] ) );
5625
5626 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005627 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005632 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 }
5635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5637 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5640 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005642
5643#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5644 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5645 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5646 {
Hanno Becker90333da2017-10-10 11:27:13 +01005647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005648 /* Will be handled when trying to parse ServerHello */
5649 return( 0 );
5650 }
5651#endif
5652
5653#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5654 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5655 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5656 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5657 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5658 {
5659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5660 /* Will be handled in mbedtls_ssl_parse_certificate() */
5661 return( 0 );
5662 }
5663#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5664
5665 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005666 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005667 }
5668
Hanno Beckerc76c6192017-06-06 10:03:17 +01005669#if defined(MBEDTLS_SSL_PROTO_DTLS)
5670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5671 ssl->handshake != NULL &&
5672 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5673 {
5674 ssl_handshake_wrapup_free_hs_transform( ssl );
5675 }
5676#endif
5677
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 return( 0 );
5679}
5680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005682{
5683 int ret;
5684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5686 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5687 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005688 {
5689 return( ret );
5690 }
5691
5692 return( 0 );
5693}
5694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005696 unsigned char level,
5697 unsigned char message )
5698{
5699 int ret;
5700
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005701 if( ssl == NULL || ssl->conf == NULL )
5702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005708 ssl->out_msglen = 2;
5709 ssl->out_msg[0] = level;
5710 ssl->out_msg[1] = message;
5711
Hanno Becker67bc7c32018-08-06 11:33:50 +01005712 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005715 return( ret );
5716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005718
5719 return( 0 );
5720}
5721
Hanno Beckerb9d44792019-02-08 07:19:04 +00005722#if defined(MBEDTLS_X509_CRT_PARSE_C)
5723static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5724{
5725#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5726 if( session->peer_cert != NULL )
5727 {
5728 mbedtls_x509_crt_free( session->peer_cert );
5729 mbedtls_free( session->peer_cert );
5730 session->peer_cert = NULL;
5731 }
5732#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5733 if( session->peer_cert_digest != NULL )
5734 {
5735 /* Zeroization is not necessary. */
5736 mbedtls_free( session->peer_cert_digest );
5737 session->peer_cert_digest = NULL;
5738 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5739 session->peer_cert_digest_len = 0;
5740 }
5741#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5742}
5743#endif /* MBEDTLS_X509_CRT_PARSE_C */
5744
Paul Bakker5121ce52009-01-03 21:22:43 +00005745/*
5746 * Handshake functions
5747 */
Hanno Becker21489932019-02-05 13:20:55 +00005748#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005749/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005751{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005752 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5753 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005756
Hanno Becker7177a882019-02-05 13:36:46 +00005757 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005760 ssl->state++;
5761 return( 0 );
5762 }
5763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005766}
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005769{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005770 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5771 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005774
Hanno Becker7177a882019-02-05 13:36:46 +00005775 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005778 ssl->state++;
5779 return( 0 );
5780 }
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005784}
Gilles Peskinef9828522017-05-03 12:28:43 +02005785
Hanno Becker21489932019-02-05 13:20:55 +00005786#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005787/* Some certificate support -> implement write and parse */
5788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005792 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005794 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5795 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005798
Hanno Becker7177a882019-02-05 13:36:46 +00005799 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005802 ssl->state++;
5803 return( 0 );
5804 }
5805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005807 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 {
5809 if( ssl->client_auth == 0 )
5810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 ssl->state++;
5813 return( 0 );
5814 }
5815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 /*
5818 * If using SSLv3 and got no cert, send an Alert message
5819 * (otherwise an empty Certificate message will be sent).
5820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5822 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 {
5824 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5826 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5827 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 goto write_msg;
5831 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#endif /* MBEDTLS_SSL_CLI_C */
5835#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5841 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 }
5843 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005847
5848 /*
5849 * 0 . 0 handshake type
5850 * 1 . 3 handshake length
5851 * 4 . 6 length of all certs
5852 * 7 . 9 length of cert. 1
5853 * 10 . n-1 peer certificate
5854 * n . n+2 length of cert. 2
5855 * n+3 . ... upper level cert, etc.
5856 */
5857 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005859
Paul Bakker29087132010-03-21 21:03:34 +00005860 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 {
5862 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005863 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005866 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005868 }
5869
5870 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5871 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5872 ssl->out_msg[i + 2] = (unsigned char)( n );
5873
5874 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5875 i += n; crt = crt->next;
5876 }
5877
5878 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5879 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5880 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5881
5882 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5884 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005885
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005886#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005887write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005888#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005889
5890 ssl->state++;
5891
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005892 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 return( ret );
5896 }
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005899
Paul Bakkered27a042013-04-18 22:46:23 +02005900 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005901}
5902
Hanno Becker84879e32019-01-31 07:44:03 +00005903#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005904
5905#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005906static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5907 unsigned char *crt_buf,
5908 size_t crt_buf_len )
5909{
5910 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5911
5912 if( peer_crt == NULL )
5913 return( -1 );
5914
5915 if( peer_crt->raw.len != crt_buf_len )
5916 return( -1 );
5917
Hanno Becker46f34d02019-02-08 14:00:04 +00005918 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005919}
Hanno Becker177475a2019-02-05 17:02:46 +00005920#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5921static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5922 unsigned char *crt_buf,
5923 size_t crt_buf_len )
5924{
5925 int ret;
5926 unsigned char const * const peer_cert_digest =
5927 ssl->session->peer_cert_digest;
5928 mbedtls_md_type_t const peer_cert_digest_type =
5929 ssl->session->peer_cert_digest_type;
5930 mbedtls_md_info_t const * const digest_info =
5931 mbedtls_md_info_from_type( peer_cert_digest_type );
5932 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5933 size_t digest_len;
5934
5935 if( peer_cert_digest == NULL || digest_info == NULL )
5936 return( -1 );
5937
5938 digest_len = mbedtls_md_get_size( digest_info );
5939 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5940 return( -1 );
5941
5942 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5943 if( ret != 0 )
5944 return( -1 );
5945
5946 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5947}
5948#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005949#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005950
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005951/*
5952 * Once the certificate message is read, parse it into a cert chain and
5953 * perform basic checks, but leave actual verification to the caller
5954 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005955static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5956 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005957{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005958 int ret;
5959#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5960 int crt_cnt=0;
5961#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005962 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005963 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005968 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5969 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005971 }
5972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5974 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005977 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5978 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005980 }
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005983
Paul Bakker5121ce52009-01-03 21:22:43 +00005984 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005986 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005987 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005989 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5994 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 }
5997
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005998 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5999 i += 3;
6000
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006001 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006002 while( i < ssl->in_hslen )
6003 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006004 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006005 if ( i + 3 > ssl->in_hslen ) {
6006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006007 mbedtls_ssl_send_alert_message( ssl,
6008 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6009 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006010 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6011 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006012 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6013 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 if( ssl->in_msg[i] != 0 )
6015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006017 mbedtls_ssl_send_alert_message( ssl,
6018 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6019 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 }
6022
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006023 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6025 | (unsigned int) ssl->in_msg[i + 2];
6026 i += 3;
6027
6028 if( n < 128 || i + n > ssl->in_hslen )
6029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006031 mbedtls_ssl_send_alert_message( ssl,
6032 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6033 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006035 }
6036
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006037 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006038#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6039 if( crt_cnt++ == 0 &&
6040 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6041 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006042 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006043 /* During client-side renegotiation, check that the server's
6044 * end-CRTs hasn't changed compared to the initial handshake,
6045 * mitigating the triple handshake attack. On success, reuse
6046 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6048 if( ssl_check_peer_crt_unchanged( ssl,
6049 &ssl->in_msg[i],
6050 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006051 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6053 mbedtls_ssl_send_alert_message( ssl,
6054 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6055 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6056 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006057 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006058
6059 /* Now we can safely free the original chain. */
6060 ssl_clear_peer_cert( ssl->session );
6061 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006062#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6063
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006064 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006065#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006066 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006067#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006068 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006069 * it in-place from the input buffer instead of making a copy. */
6070 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6071#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006072 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006074 case 0: /*ok*/
6075 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6076 /* Ignore certificate with an unknown algorithm: maybe a
6077 prior certificate was already trusted. */
6078 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006079
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006080 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6081 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6082 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006083
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006084 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6085 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6086 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006087
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006088 default:
6089 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6090 crt_parse_der_failed:
6091 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6092 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6093 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 }
6095
6096 i += n;
6097 }
6098
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006099 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006100 return( 0 );
6101}
6102
Hanno Becker4a55f632019-02-05 12:49:06 +00006103#if defined(MBEDTLS_SSL_SRV_C)
6104static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6105{
6106 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6107 return( -1 );
6108
6109#if defined(MBEDTLS_SSL_PROTO_SSL3)
6110 /*
6111 * Check if the client sent an empty certificate
6112 */
6113 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6114 {
6115 if( ssl->in_msglen == 2 &&
6116 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6117 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6118 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6121 return( 0 );
6122 }
6123
6124 return( -1 );
6125 }
6126#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6127
6128#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6129 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6130 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6131 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6132 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6133 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6134 {
6135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6136 return( 0 );
6137 }
6138
6139 return( -1 );
6140#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6141 MBEDTLS_SSL_PROTO_TLS1_2 */
6142}
6143#endif /* MBEDTLS_SSL_SRV_C */
6144
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006145/* Check if a certificate message is expected.
6146 * Return either
6147 * - SSL_CERTIFICATE_EXPECTED, or
6148 * - SSL_CERTIFICATE_SKIP
6149 * indicating whether a Certificate message is expected or not.
6150 */
6151#define SSL_CERTIFICATE_EXPECTED 0
6152#define SSL_CERTIFICATE_SKIP 1
6153static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6154 int authmode )
6155{
6156 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006157 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006158
6159 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6160 return( SSL_CERTIFICATE_SKIP );
6161
6162#if defined(MBEDTLS_SSL_SRV_C)
6163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6164 {
6165 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6166 return( SSL_CERTIFICATE_SKIP );
6167
6168 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6169 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006170 ssl->session_negotiate->verify_result =
6171 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6172 return( SSL_CERTIFICATE_SKIP );
6173 }
6174 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006175#else
6176 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006177#endif /* MBEDTLS_SSL_SRV_C */
6178
6179 return( SSL_CERTIFICATE_EXPECTED );
6180}
6181
Hanno Becker68636192019-02-05 14:36:34 +00006182static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6183 int authmode,
6184 mbedtls_x509_crt *chain,
6185 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006186{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006187 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006188 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006189 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006190 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006191
Hanno Becker8927c832019-04-03 12:52:50 +01006192 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6193 void *p_vrfy;
6194
Hanno Becker68636192019-02-05 14:36:34 +00006195 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6196 return( 0 );
6197
Hanno Becker8927c832019-04-03 12:52:50 +01006198 if( ssl->f_vrfy != NULL )
6199 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006201 f_vrfy = ssl->f_vrfy;
6202 p_vrfy = ssl->p_vrfy;
6203 }
6204 else
6205 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006207 f_vrfy = ssl->conf->f_vrfy;
6208 p_vrfy = ssl->conf->p_vrfy;
6209 }
6210
Hanno Becker68636192019-02-05 14:36:34 +00006211 /*
6212 * Main check: verify certificate
6213 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006214#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6215 if( ssl->conf->f_ca_cb != NULL )
6216 {
6217 ((void) rs_ctx);
6218 have_ca_chain = 1;
6219
6220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006221 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006222 chain,
6223 ssl->conf->f_ca_cb,
6224 ssl->conf->p_ca_cb,
6225 ssl->conf->cert_profile,
6226 ssl->hostname,
6227 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006228 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006229 }
6230 else
6231#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6232 {
6233 mbedtls_x509_crt *ca_chain;
6234 mbedtls_x509_crl *ca_crl;
6235
6236#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6237 if( ssl->handshake->sni_ca_chain != NULL )
6238 {
6239 ca_chain = ssl->handshake->sni_ca_chain;
6240 ca_crl = ssl->handshake->sni_ca_crl;
6241 }
6242 else
6243#endif
6244 {
6245 ca_chain = ssl->conf->ca_chain;
6246 ca_crl = ssl->conf->ca_crl;
6247 }
6248
6249 if( ca_chain != NULL )
6250 have_ca_chain = 1;
6251
6252 ret = mbedtls_x509_crt_verify_restartable(
6253 chain,
6254 ca_chain, ca_crl,
6255 ssl->conf->cert_profile,
6256 ssl->hostname,
6257 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006258 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006259 }
Hanno Becker68636192019-02-05 14:36:34 +00006260
6261 if( ret != 0 )
6262 {
6263 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6264 }
6265
6266#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6267 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6268 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6269#endif
6270
6271 /*
6272 * Secondary checks: always done, but change 'ret' only if it was 0
6273 */
6274
6275#if defined(MBEDTLS_ECP_C)
6276 {
6277 const mbedtls_pk_context *pk = &chain->pk;
6278
6279 /* If certificate uses an EC key, make sure the curve is OK */
6280 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6281 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6282 {
6283 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6284
6285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6286 if( ret == 0 )
6287 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6288 }
6289 }
6290#endif /* MBEDTLS_ECP_C */
6291
6292 if( mbedtls_ssl_check_cert_usage( chain,
6293 ciphersuite_info,
6294 ! ssl->conf->endpoint,
6295 &ssl->session_negotiate->verify_result ) != 0 )
6296 {
6297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6298 if( ret == 0 )
6299 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6300 }
6301
6302 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6303 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6304 * with details encoded in the verification flags. All other kinds
6305 * of error codes, including those from the user provided f_vrfy
6306 * functions, are treated as fatal and lead to a failure of
6307 * ssl_parse_certificate even if verification was optional. */
6308 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6309 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6310 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6311 {
6312 ret = 0;
6313 }
6314
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006315 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6318 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6319 }
6320
6321 if( ret != 0 )
6322 {
6323 uint8_t alert;
6324
6325 /* The certificate may have been rejected for several reasons.
6326 Pick one and send the corresponding alert. Which alert to send
6327 may be a subject of debate in some cases. */
6328 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6329 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6330 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6331 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6332 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6333 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6334 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6335 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6336 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6337 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6338 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6339 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6340 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6341 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6342 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6343 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6344 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6345 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6346 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6347 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6348 else
6349 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6350 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6351 alert );
6352 }
6353
6354#if defined(MBEDTLS_DEBUG_C)
6355 if( ssl->session_negotiate->verify_result != 0 )
6356 {
6357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6358 ssl->session_negotiate->verify_result ) );
6359 }
6360 else
6361 {
6362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6363 }
6364#endif /* MBEDTLS_DEBUG_C */
6365
6366 return( ret );
6367}
6368
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006369#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6370static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6371 unsigned char *start, size_t len )
6372{
6373 int ret;
6374 /* Remember digest of the peer's end-CRT. */
6375 ssl->session_negotiate->peer_cert_digest =
6376 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6377 if( ssl->session_negotiate->peer_cert_digest == NULL )
6378 {
6379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6380 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6381 mbedtls_ssl_send_alert_message( ssl,
6382 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6383 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6384
6385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6386 }
6387
6388 ret = mbedtls_md( mbedtls_md_info_from_type(
6389 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6390 start, len,
6391 ssl->session_negotiate->peer_cert_digest );
6392
6393 ssl->session_negotiate->peer_cert_digest_type =
6394 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6395 ssl->session_negotiate->peer_cert_digest_len =
6396 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6397
6398 return( ret );
6399}
6400
6401static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6402 unsigned char *start, size_t len )
6403{
6404 unsigned char *end = start + len;
6405 int ret;
6406
6407 /* Make a copy of the peer's raw public key. */
6408 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6409 ret = mbedtls_pk_parse_subpubkey( &start, end,
6410 &ssl->handshake->peer_pubkey );
6411 if( ret != 0 )
6412 {
6413 /* We should have parsed the public key before. */
6414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6415 }
6416
6417 return( 0 );
6418}
6419#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6420
Hanno Becker68636192019-02-05 14:36:34 +00006421int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6422{
6423 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006424 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006425#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6426 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6427 ? ssl->handshake->sni_authmode
6428 : ssl->conf->authmode;
6429#else
6430 const int authmode = ssl->conf->authmode;
6431#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006432 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006433 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006434
6435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6436
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006437 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6438 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006439 {
6440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006441 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006442 }
6443
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006444#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6445 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006446 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006447 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006448 chain = ssl->handshake->ecrs_peer_cert;
6449 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006450 goto crt_verify;
6451 }
6452#endif
6453
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006454 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006455 {
6456 /* mbedtls_ssl_read_record may have sent an alert already. We
6457 let it decide whether to alert. */
6458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006459 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006460 }
6461
Hanno Becker4a55f632019-02-05 12:49:06 +00006462#if defined(MBEDTLS_SSL_SRV_C)
6463 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6464 {
6465 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006466
6467 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006468 ret = 0;
6469 else
6470 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006471
Hanno Becker6bdfab22019-02-05 13:11:17 +00006472 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006473 }
6474#endif /* MBEDTLS_SSL_SRV_C */
6475
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006476 /* Clear existing peer CRT structure in case we tried to
6477 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006478 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006479
6480 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6481 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006482 {
6483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6484 sizeof( mbedtls_x509_crt ) ) );
6485 mbedtls_ssl_send_alert_message( ssl,
6486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6487 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006488
Hanno Becker3dad3112019-02-05 17:19:52 +00006489 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6490 goto exit;
6491 }
6492 mbedtls_x509_crt_init( chain );
6493
6494 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006495 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006496 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006497
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006498#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6499 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006500 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006501
6502crt_verify:
6503 if( ssl->handshake->ecrs_enabled)
6504 rs_ctx = &ssl->handshake->ecrs_ctx;
6505#endif
6506
Hanno Becker68636192019-02-05 14:36:34 +00006507 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006508 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006509 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006510 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006511
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006512#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006513 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006514 unsigned char *crt_start, *pk_start;
6515 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006516
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006517 /* We parse the CRT chain without copying, so
6518 * these pointers point into the input buffer,
6519 * and are hence still valid after freeing the
6520 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006521
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006522 crt_start = chain->raw.p;
6523 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006524
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006525 pk_start = chain->pk_raw.p;
6526 pk_len = chain->pk_raw.len;
6527
6528 /* Free the CRT structures before computing
6529 * digest and copying the peer's public key. */
6530 mbedtls_x509_crt_free( chain );
6531 mbedtls_free( chain );
6532 chain = NULL;
6533
6534 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006535 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006536 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006537
6538 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6539 if( ret != 0 )
6540 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006541 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006542#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6543 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006544 ssl->session_negotiate->peer_cert = chain;
6545 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006546#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
Hanno Becker6bdfab22019-02-05 13:11:17 +00006550exit:
6551
Hanno Becker3dad3112019-02-05 17:19:52 +00006552 if( ret == 0 )
6553 ssl->state++;
6554
6555#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6556 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6557 {
6558 ssl->handshake->ecrs_peer_cert = chain;
6559 chain = NULL;
6560 }
6561#endif
6562
6563 if( chain != NULL )
6564 {
6565 mbedtls_x509_crt_free( chain );
6566 mbedtls_free( chain );
6567 }
6568
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 return( ret );
6570}
Hanno Becker21489932019-02-05 13:20:55 +00006571#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006574{
6575 int ret;
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 ssl->out_msglen = 1;
6581 ssl->out_msg[0] = 1;
6582
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 ssl->state++;
6584
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006585 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 return( ret );
6589 }
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592
6593 return( 0 );
6594}
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006597{
6598 int ret;
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601
Hanno Becker327c93b2018-08-15 13:56:18 +01006602 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 return( ret );
6606 }
6607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6612 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 }
6615
Hanno Beckere678eaa2018-08-21 14:57:46 +01006616 /* CCS records are only accepted if they have length 1 and content '1',
6617 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006619 /*
6620 * Switch to our negotiated transform and session parameters for inbound
6621 * data.
6622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006624 ssl->transform_in = ssl->transform_negotiate;
6625 ssl->session_in = ssl->session_negotiate;
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006628 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006631 ssl_dtls_replay_reset( ssl );
6632#endif
6633
6634 /* Increment epoch */
6635 if( ++ssl->in_epoch == 0 )
6636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006638 /* This is highly unlikely to happen for legitimate reasons, so
6639 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006641 }
6642 }
6643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006645 memset( ssl->in_ctr, 0, 8 );
6646
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006647 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6650 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006655 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6656 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006658 }
6659 }
6660#endif
6661
Paul Bakker5121ce52009-01-03 21:22:43 +00006662 ssl->state++;
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006665
6666 return( 0 );
6667}
6668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6670 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006671{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006672 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6676 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006677 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006678 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6681#if defined(MBEDTLS_SHA512_C)
6682 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006683 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6684 else
6685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686#if defined(MBEDTLS_SHA256_C)
6687 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006688 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006689 else
6690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006694 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006695 }
Paul Bakker380da532012-04-18 16:10:25 +00006696}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6701 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006702 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6703 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6706#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006708 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006709 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6710#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006711 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006712#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006715#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006716 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006717 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006718#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006719 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006720#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006723}
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006726 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006727{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6729 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006730 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6731 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6734#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006735#if defined(MBEDTLS_USE_PSA_CRYPTO)
6736 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6737#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006738 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006739#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006743 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006744#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006745 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006746#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006749}
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6752 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6753static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006754 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006755{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006756 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6757 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006758}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006759#endif
Paul Bakker380da532012-04-18 16:10:25 +00006760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6762#if defined(MBEDTLS_SHA256_C)
6763static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006764 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006765{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006766#if defined(MBEDTLS_USE_PSA_CRYPTO)
6767 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6768#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006769 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006770#endif
Paul Bakker380da532012-04-18 16:10:25 +00006771}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006772#endif
Paul Bakker380da532012-04-18 16:10:25 +00006773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774#if defined(MBEDTLS_SHA512_C)
6775static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006777{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006778#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006779 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006780#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006781 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006782#endif
Paul Bakker380da532012-04-18 16:10:25 +00006783}
Paul Bakker769075d2012-11-24 11:26:46 +01006784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006788static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006790{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006791 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006792 mbedtls_md5_context md5;
6793 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006794
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 unsigned char padbuf[48];
6796 unsigned char md5sum[16];
6797 unsigned char sha1sum[20];
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006800 if( !session )
6801 session = ssl->session;
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006804
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006805 mbedtls_md5_init( &md5 );
6806 mbedtls_sha1_init( &sha1 );
6807
6808 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6809 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
6811 /*
6812 * SSLv3:
6813 * hash =
6814 * MD5( master + pad2 +
6815 * MD5( handshake + sender + master + pad1 ) )
6816 * + SHA1( master + pad2 +
6817 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006818 */
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006821 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6822 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006826 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6827 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006831 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Paul Bakker1ef83d62012-04-11 12:09:53 +00006833 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006835 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6836 mbedtls_md5_update_ret( &md5, session->master, 48 );
6837 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6838 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006840 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6841 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6842 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6843 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844
Paul Bakker1ef83d62012-04-11 12:09:53 +00006845 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006847 mbedtls_md5_starts_ret( &md5 );
6848 mbedtls_md5_update_ret( &md5, session->master, 48 );
6849 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6850 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6851 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006853 mbedtls_sha1_starts_ret( &sha1 );
6854 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6855 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6856 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6857 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006861 mbedtls_md5_free( &md5 );
6862 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006863
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006864 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6865 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6866 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006873static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006875{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006876 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006877 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006878 mbedtls_md5_context md5;
6879 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006880 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006883 if( !session )
6884 session = ssl->session;
6885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006887
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006888 mbedtls_md5_init( &md5 );
6889 mbedtls_sha1_init( &sha1 );
6890
6891 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6892 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006893
Paul Bakker1ef83d62012-04-11 12:09:53 +00006894 /*
6895 * TLSv1:
6896 * hash = PRF( master, finished_label,
6897 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6898 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006901 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6902 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006903#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006906 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6907 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006908#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006911 ? "client finished"
6912 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006913
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006914 mbedtls_md5_finish_ret( &md5, padbuf );
6915 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006916
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006917 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006918 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006921
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006922 mbedtls_md5_free( &md5 );
6923 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006924
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006925 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6932#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006933static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935{
6936 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006937 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006939#if defined(MBEDTLS_USE_PSA_CRYPTO)
6940 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006941 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006942 psa_status_t status;
6943#else
6944 mbedtls_sha256_context sha256;
6945#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006948 if( !session )
6949 session = ssl->session;
6950
Andrzej Kurekeb342242019-01-29 09:14:33 -05006951 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6952 ? "client finished"
6953 : "server finished";
6954
6955#if defined(MBEDTLS_USE_PSA_CRYPTO)
6956 sha256_psa = psa_hash_operation_init();
6957
6958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6959
6960 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6961 if( status != PSA_SUCCESS )
6962 {
6963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6964 return;
6965 }
6966
6967 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6968 if( status != PSA_SUCCESS )
6969 {
6970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6971 return;
6972 }
6973 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6974#else
6975
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006976 mbedtls_sha256_init( &sha256 );
6977
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006979
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006980 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006981
6982 /*
6983 * TLSv1.2:
6984 * hash = PRF( master, finished_label,
6985 * Hash( handshake ) )[0.11]
6986 */
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if !defined(MBEDTLS_SHA256_ALT)
6989 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006990 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006991#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006992
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006993 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006994 mbedtls_sha256_free( &sha256 );
6995#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006996
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006997 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006998 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007001
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007002 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007009static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007011{
7012 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007013 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007014 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007015#if defined(MBEDTLS_USE_PSA_CRYPTO)
7016 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007017 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007018 psa_status_t status;
7019#else
7020 mbedtls_sha512_context sha512;
7021#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007024 if( !session )
7025 session = ssl->session;
7026
Andrzej Kurekeb342242019-01-29 09:14:33 -05007027 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7028 ? "client finished"
7029 : "server finished";
7030
7031#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007032 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007033
7034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7035
Andrzej Kurek972fba52019-01-30 03:29:12 -05007036 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007037 if( status != PSA_SUCCESS )
7038 {
7039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7040 return;
7041 }
7042
Andrzej Kurek972fba52019-01-30 03:29:12 -05007043 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007044 if( status != PSA_SUCCESS )
7045 {
7046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7047 return;
7048 }
7049 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7050#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007051 mbedtls_sha512_init( &sha512 );
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007054
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007055 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007056
7057 /*
7058 * TLSv1.2:
7059 * hash = PRF( master, finished_label,
7060 * Hash( handshake ) )[0.11]
7061 */
7062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007064 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7065 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007066#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007067
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007068 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007069 mbedtls_sha512_free( &sha512 );
7070#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007071
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007072 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007073 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007076
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007077 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#endif /* MBEDTLS_SHA512_C */
7082#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007087
7088 /*
7089 * Free our handshake params
7090 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007091 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007093 ssl->handshake = NULL;
7094
7095 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007096 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007097 */
7098 if( ssl->transform )
7099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 mbedtls_ssl_transform_free( ssl->transform );
7101 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007102 }
7103 ssl->transform = ssl->transform_negotiate;
7104 ssl->transform_negotiate = NULL;
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007107}
7108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007110{
7111 int resume = ssl->handshake->resume;
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_SSL_RENEGOTIATION)
7116 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007119 ssl->renego_records_seen = 0;
7120 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007121#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007122
7123 /*
7124 * Free the previous session and switch in the current one
7125 */
Paul Bakker0a597072012-09-25 21:55:46 +00007126 if( ssl->session )
7127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007129 /* RFC 7366 3.1: keep the EtM state */
7130 ssl->session_negotiate->encrypt_then_mac =
7131 ssl->session->encrypt_then_mac;
7132#endif
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 mbedtls_ssl_session_free( ssl->session );
7135 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007136 }
7137 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007138 ssl->session_negotiate = NULL;
7139
Paul Bakker0a597072012-09-25 21:55:46 +00007140 /*
7141 * Add cache entry
7142 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007143 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007144 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007145 resume == 0 )
7146 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007147 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007149 }
Paul Bakker0a597072012-09-25 21:55:46 +00007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007153 ssl->handshake->flight != NULL )
7154 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007155 /* Cancel handshake timer */
7156 ssl_set_timer( ssl, 0 );
7157
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007158 /* Keep last flight around in case we need to resend it:
7159 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007161 }
7162 else
7163#endif
7164 ssl_handshake_wrapup_free_hs_transform( ssl );
7165
Paul Bakker48916f92012-09-16 19:57:18 +00007166 ssl->state++;
7167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007169}
7170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007173 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007176
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007177 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007178
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007179 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007180
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007181 /*
7182 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7183 * may define some other value. Currently (early 2016), no defined
7184 * ciphersuite does this (and this is unlikely to change as activity has
7185 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007190 ssl->verify_data_len = hash_len;
7191 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007192#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007193
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7196 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007197
7198 /*
7199 * In case of session resuming, invert the client and server
7200 * ChangeCipherSpec messages order.
7201 */
Paul Bakker0a597072012-09-25 21:55:46 +00007202 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007205 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007209 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007212 }
7213 else
7214 ssl->state++;
7215
Paul Bakker48916f92012-09-16 19:57:18 +00007216 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007217 * Switch to our negotiated transform and session parameters for outbound
7218 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007224 {
7225 unsigned char i;
7226
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007227 /* Remember current epoch settings for resending */
7228 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007229 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007230
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007231 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007232 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007233
7234 /* Increment epoch */
7235 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007236 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007237 break;
7238
7239 /* The loop goes to its end iff the counter is wrapping */
7240 if( i == 0 )
7241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7243 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007244 }
7245 }
7246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007248 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007250 ssl->transform_out = ssl->transform_negotiate;
7251 ssl->session_out = ssl->session_negotiate;
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7254 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7259 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007260 }
7261 }
7262#endif
7263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007267#endif
7268
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007269 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 return( ret );
7273 }
7274
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007275#if defined(MBEDTLS_SSL_PROTO_DTLS)
7276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7277 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7278 {
7279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7280 return( ret );
7281 }
7282#endif
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
7286 return( 0 );
7287}
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007290#define SSL_MAX_HASH_LEN 36
7291#else
7292#define SSL_MAX_HASH_LEN 12
7293#endif
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007296{
Paul Bakker23986e52011-04-24 08:57:21 +00007297 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007298 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007299 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007303 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007304
Hanno Becker327c93b2018-08-15 13:56:18 +01007305 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007308 return( ret );
7309 }
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7315 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007317 }
7318
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007319 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SSL_PROTO_SSL3)
7321 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007322 hash_len = 36;
7323 else
7324#endif
7325 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7328 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007331 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7332 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 }
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007337 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 }
7344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007346 ssl->verify_data_len = hash_len;
7347 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007348#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007349
Paul Bakker0a597072012-09-25 21:55:46 +00007350 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007355#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007360 }
7361 else
7362 ssl->state++;
7363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007367#endif
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
7371 return( 0 );
7372}
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7379 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7380 mbedtls_md5_init( &handshake->fin_md5 );
7381 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007382 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7383 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7386#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007387#if defined(MBEDTLS_USE_PSA_CRYPTO)
7388 handshake->fin_sha256_psa = psa_hash_operation_init();
7389 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7390#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007392 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007393#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007397 handshake->fin_sha384_psa = psa_hash_operation_init();
7398 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007399#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007401 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007402#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007405
7406 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007407
7408#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7409 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7410 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7411#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if defined(MBEDTLS_DHM_C)
7414 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if defined(MBEDTLS_ECDH_C)
7417 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007418#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007419#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007420 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007421#if defined(MBEDTLS_SSL_CLI_C)
7422 handshake->ecjpake_cache = NULL;
7423 handshake->ecjpake_cache_len = 0;
7424#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007425#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007426
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007427#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007428 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007429#endif
7430
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007431#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7432 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7433#endif
Hanno Becker75173122019-02-06 16:18:31 +00007434
7435#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7436 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7437 mbedtls_pk_init( &handshake->peer_pubkey );
7438#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007439}
7440
Hanno Beckera18d1322018-01-03 14:27:32 +00007441void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7446 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007447
Hanno Beckerd56ed242018-01-03 15:32:51 +00007448#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 mbedtls_md_init( &transform->md_ctx_enc );
7450 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007451#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007452}
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007457}
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007460{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007461 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007462 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007464 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007466 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007467 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007468
7469 /*
7470 * Either the pointers are now NULL or cleared properly and can be freed.
7471 * Now allocate missing structures.
7472 */
7473 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007474 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007475 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007476 }
Paul Bakker48916f92012-09-16 19:57:18 +00007477
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007478 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007479 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007480 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007481 }
Paul Bakker48916f92012-09-16 19:57:18 +00007482
Paul Bakker82788fb2014-10-20 13:59:19 +02007483 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007484 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007485 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007486 }
Paul Bakker48916f92012-09-16 19:57:18 +00007487
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007488 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007489 if( ssl->handshake == NULL ||
7490 ssl->transform_negotiate == NULL ||
7491 ssl->session_negotiate == NULL )
7492 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 mbedtls_free( ssl->handshake );
7496 mbedtls_free( ssl->transform_negotiate );
7497 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007498
7499 ssl->handshake = NULL;
7500 ssl->transform_negotiate = NULL;
7501 ssl->session_negotiate = NULL;
7502
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007503 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007504 }
7505
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007506 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007508 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007509 ssl_handshake_params_init( ssl->handshake );
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7513 {
7514 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007515
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007516 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7517 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7518 else
7519 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007520
7521 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007522 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007523#endif
7524
Paul Bakker48916f92012-09-16 19:57:18 +00007525 return( 0 );
7526}
7527
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007528#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007529/* Dummy cookie callbacks for defaults */
7530static int ssl_cookie_write_dummy( void *ctx,
7531 unsigned char **p, unsigned char *end,
7532 const unsigned char *cli_id, size_t cli_id_len )
7533{
7534 ((void) ctx);
7535 ((void) p);
7536 ((void) end);
7537 ((void) cli_id);
7538 ((void) cli_id_len);
7539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007541}
7542
7543static int ssl_cookie_check_dummy( void *ctx,
7544 const unsigned char *cookie, size_t cookie_len,
7545 const unsigned char *cli_id, size_t cli_id_len )
7546{
7547 ((void) ctx);
7548 ((void) cookie);
7549 ((void) cookie_len);
7550 ((void) cli_id);
7551 ((void) cli_id_len);
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007554}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007555#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007556
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007557/* Once ssl->out_hdr as the address of the beginning of the
7558 * next outgoing record is set, deduce the other pointers.
7559 *
7560 * Note: For TLS, we save the implicit record sequence number
7561 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7562 * and the caller has to make sure there's space for this.
7563 */
7564
7565static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7566 mbedtls_ssl_transform *transform )
7567{
7568#if defined(MBEDTLS_SSL_PROTO_DTLS)
7569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7570 {
7571 ssl->out_ctr = ssl->out_hdr + 3;
7572 ssl->out_len = ssl->out_hdr + 11;
7573 ssl->out_iv = ssl->out_hdr + 13;
7574 }
7575 else
7576#endif
7577 {
7578 ssl->out_ctr = ssl->out_hdr - 8;
7579 ssl->out_len = ssl->out_hdr + 3;
7580 ssl->out_iv = ssl->out_hdr + 5;
7581 }
7582
7583 /* Adjust out_msg to make space for explicit IV, if used. */
7584 if( transform != NULL &&
7585 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7586 {
7587 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7588 }
7589 else
7590 ssl->out_msg = ssl->out_iv;
7591}
7592
7593/* Once ssl->in_hdr as the address of the beginning of the
7594 * next incoming record is set, deduce the other pointers.
7595 *
7596 * Note: For TLS, we save the implicit record sequence number
7597 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7598 * and the caller has to make sure there's space for this.
7599 */
7600
7601static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7602 mbedtls_ssl_transform *transform )
7603{
7604#if defined(MBEDTLS_SSL_PROTO_DTLS)
7605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7606 {
7607 ssl->in_ctr = ssl->in_hdr + 3;
7608 ssl->in_len = ssl->in_hdr + 11;
7609 ssl->in_iv = ssl->in_hdr + 13;
7610 }
7611 else
7612#endif
7613 {
7614 ssl->in_ctr = ssl->in_hdr - 8;
7615 ssl->in_len = ssl->in_hdr + 3;
7616 ssl->in_iv = ssl->in_hdr + 5;
7617 }
7618
7619 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7620 if( transform != NULL &&
7621 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7622 {
7623 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7624 }
7625 else
7626 ssl->in_msg = ssl->in_iv;
7627}
7628
Paul Bakker5121ce52009-01-03 21:22:43 +00007629/*
7630 * Initialize an SSL context
7631 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007632void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7633{
7634 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7635}
7636
7637/*
7638 * Setup an SSL context
7639 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007640
7641static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7642{
7643 /* Set the incoming and outgoing record pointers. */
7644#if defined(MBEDTLS_SSL_PROTO_DTLS)
7645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7646 {
7647 ssl->out_hdr = ssl->out_buf;
7648 ssl->in_hdr = ssl->in_buf;
7649 }
7650 else
7651#endif /* MBEDTLS_SSL_PROTO_DTLS */
7652 {
7653 ssl->out_hdr = ssl->out_buf + 8;
7654 ssl->in_hdr = ssl->in_buf + 8;
7655 }
7656
7657 /* Derive other internal pointers. */
7658 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7659 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7660}
7661
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007662int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007663 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007664{
Paul Bakker48916f92012-09-16 19:57:18 +00007665 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007666
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007667 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007668
7669 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007670 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007671 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007672
7673 /* Set to NULL in case of an error condition */
7674 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007675
Angus Grattond8213d02016-05-25 20:56:48 +10007676 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7677 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007678 {
Angus Grattond8213d02016-05-25 20:56:48 +10007679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007680 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007681 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007682 }
7683
7684 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7685 if( ssl->out_buf == NULL )
7686 {
7687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007688 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007689 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007690 }
7691
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007692 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007693
Paul Bakker48916f92012-09-16 19:57:18 +00007694 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007695 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007696
7697 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007698
7699error:
7700 mbedtls_free( ssl->in_buf );
7701 mbedtls_free( ssl->out_buf );
7702
7703 ssl->conf = NULL;
7704
7705 ssl->in_buf = NULL;
7706 ssl->out_buf = NULL;
7707
7708 ssl->in_hdr = NULL;
7709 ssl->in_ctr = NULL;
7710 ssl->in_len = NULL;
7711 ssl->in_iv = NULL;
7712 ssl->in_msg = NULL;
7713
7714 ssl->out_hdr = NULL;
7715 ssl->out_ctr = NULL;
7716 ssl->out_len = NULL;
7717 ssl->out_iv = NULL;
7718 ssl->out_msg = NULL;
7719
k-stachowiak9f7798e2018-07-31 16:52:32 +02007720 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007721}
7722
7723/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007724 * Reset an initialized and used SSL context for re-use while retaining
7725 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007726 *
7727 * If partial is non-zero, keep data in the input buffer and client ID.
7728 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007729 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007730static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007731{
Paul Bakker48916f92012-09-16 19:57:18 +00007732 int ret;
7733
Hanno Becker7e772132018-08-10 12:38:21 +01007734#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7735 !defined(MBEDTLS_SSL_SRV_C)
7736 ((void) partial);
7737#endif
7738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007740
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007741 /* Cancel any possibly running timer */
7742 ssl_set_timer( ssl, 0 );
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_RENEGOTIATION)
7745 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007746 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007747
7748 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7750 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007753
Paul Bakker7eb013f2011-10-06 12:37:39 +00007754 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007755 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007756
7757 ssl->in_msgtype = 0;
7758 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007760 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007761 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007764 ssl_dtls_replay_reset( ssl );
7765#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007766
7767 ssl->in_hslen = 0;
7768 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007769
7770 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007771
7772 ssl->out_msgtype = 0;
7773 ssl->out_msglen = 0;
7774 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7776 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007777 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007778#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007779
Hanno Becker19859472018-08-06 09:40:20 +01007780 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7781
Paul Bakker48916f92012-09-16 19:57:18 +00007782 ssl->transform_in = NULL;
7783 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007784
Hanno Becker78640902018-08-13 16:35:15 +01007785 ssl->session_in = NULL;
7786 ssl->session_out = NULL;
7787
Angus Grattond8213d02016-05-25 20:56:48 +10007788 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007789
7790#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007791 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007792#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7793 {
7794 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007795 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007796 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7799 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7802 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7805 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007806 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007807 }
7808#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007809
Paul Bakker48916f92012-09-16 19:57:18 +00007810 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 mbedtls_ssl_transform_free( ssl->transform );
7813 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007814 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007815 }
Paul Bakker48916f92012-09-16 19:57:18 +00007816
Paul Bakkerc0463502013-02-14 11:19:38 +01007817 if( ssl->session )
7818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 mbedtls_ssl_session_free( ssl->session );
7820 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007821 ssl->session = NULL;
7822 }
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007825 ssl->alpn_chosen = NULL;
7826#endif
7827
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007828#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007829#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007830 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007831#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007832 {
7833 mbedtls_free( ssl->cli_id );
7834 ssl->cli_id = NULL;
7835 ssl->cli_id_len = 0;
7836 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007837#endif
7838
Paul Bakker48916f92012-09-16 19:57:18 +00007839 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7840 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007841
7842 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007843}
7844
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007845/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007846 * Reset an initialized and used SSL context for re-use while retaining
7847 * all application-set variables, function pointers and data.
7848 */
7849int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7850{
7851 return( ssl_session_reset_int( ssl, 0 ) );
7852}
7853
7854/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 * SSL set accessors
7856 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007857void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007858{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007859 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007860}
7861
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007862void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007864 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007865}
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007868void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007869{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007870 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007871}
7872#endif
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007875void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007877 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007878}
7879#endif
7880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007882
Hanno Becker1841b0a2018-08-24 11:13:57 +01007883void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7884 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007885{
7886 ssl->disable_datagram_packing = !allow_packing;
7887}
7888
7889void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7890 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007892 conf->hs_timeout_min = min;
7893 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007894}
7895#endif
7896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007897void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007898{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007899 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007900}
7901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007903void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007904 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007905 void *p_vrfy )
7906{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007907 conf->f_vrfy = f_vrfy;
7908 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007911
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007912void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007913 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007914 void *p_rng )
7915{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007916 conf->f_rng = f_rng;
7917 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007918}
7919
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007920void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007921 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007922 void *p_dbg )
7923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007924 conf->f_dbg = f_dbg;
7925 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007926}
7927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007929 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007930 mbedtls_ssl_send_t *f_send,
7931 mbedtls_ssl_recv_t *f_recv,
7932 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007933{
7934 ssl->p_bio = p_bio;
7935 ssl->f_send = f_send;
7936 ssl->f_recv = f_recv;
7937 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007938}
7939
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007940#if defined(MBEDTLS_SSL_PROTO_DTLS)
7941void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7942{
7943 ssl->mtu = mtu;
7944}
7945#endif
7946
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007947void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007948{
7949 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007950}
7951
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007952void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7953 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007954 mbedtls_ssl_set_timer_t *f_set_timer,
7955 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007956{
7957 ssl->p_timer = p_timer;
7958 ssl->f_set_timer = f_set_timer;
7959 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007960
7961 /* Make sure we start with no timer running */
7962 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007963}
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007966void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007967 void *p_cache,
7968 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7969 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007970{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007971 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007972 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007973 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_CLI_C)
7978int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007979{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007980 int ret;
7981
7982 if( ssl == NULL ||
7983 session == NULL ||
7984 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007985 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007988 }
7989
Hanno Becker52055ae2019-02-06 14:30:46 +00007990 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7991 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007992 return( ret );
7993
Paul Bakker0a597072012-09-25 21:55:46 +00007994 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007995
7996 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007999
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008000void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008001 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008002{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008003 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8004 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8005 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8006 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008007}
8008
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008009void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008010 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008011 int major, int minor )
8012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008014 return;
8015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008017 return;
8018
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008019 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008020}
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008023void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008024 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008025{
8026 conf->cert_profile = profile;
8027}
8028
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008029/* Append a new keycert entry to a (possibly empty) list */
8030static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8031 mbedtls_x509_crt *cert,
8032 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008033{
niisato8ee24222018-06-25 19:05:48 +09008034 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008035
niisato8ee24222018-06-25 19:05:48 +09008036 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8037 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008038 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008039
niisato8ee24222018-06-25 19:05:48 +09008040 new_cert->cert = cert;
8041 new_cert->key = key;
8042 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008043
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008044 /* Update head is the list was null, else add to the end */
8045 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008046 {
niisato8ee24222018-06-25 19:05:48 +09008047 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008048 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008049 else
8050 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008051 mbedtls_ssl_key_cert *cur = *head;
8052 while( cur->next != NULL )
8053 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008054 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008055 }
8056
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008057 return( 0 );
8058}
8059
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008060int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008061 mbedtls_x509_crt *own_cert,
8062 mbedtls_pk_context *pk_key )
8063{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008064 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008065}
8066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008067void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008068 mbedtls_x509_crt *ca_chain,
8069 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008070{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008071 conf->ca_chain = ca_chain;
8072 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008073
8074#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8075 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8076 * cannot be used together. */
8077 conf->f_ca_cb = NULL;
8078 conf->p_ca_cb = NULL;
8079#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008080}
Hanno Becker5adaad92019-03-27 16:54:37 +00008081
8082#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8083void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008084 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008085 void *p_ca_cb )
8086{
8087 conf->f_ca_cb = f_ca_cb;
8088 conf->p_ca_cb = p_ca_cb;
8089
8090 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8091 * cannot be used together. */
8092 conf->ca_chain = NULL;
8093 conf->ca_crl = NULL;
8094}
8095#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008097
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008098#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8099int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8100 mbedtls_x509_crt *own_cert,
8101 mbedtls_pk_context *pk_key )
8102{
8103 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8104 own_cert, pk_key ) );
8105}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008106
8107void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8108 mbedtls_x509_crt *ca_chain,
8109 mbedtls_x509_crl *ca_crl )
8110{
8111 ssl->handshake->sni_ca_chain = ca_chain;
8112 ssl->handshake->sni_ca_crl = ca_crl;
8113}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008114
8115void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8116 int authmode )
8117{
8118 ssl->handshake->sni_authmode = authmode;
8119}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008120#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8121
Hanno Becker8927c832019-04-03 12:52:50 +01008122#if defined(MBEDTLS_X509_CRT_PARSE_C)
8123void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8124 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8125 void *p_vrfy )
8126{
8127 ssl->f_vrfy = f_vrfy;
8128 ssl->p_vrfy = p_vrfy;
8129}
8130#endif
8131
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008132#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008133/*
8134 * Set EC J-PAKE password for current handshake
8135 */
8136int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8137 const unsigned char *pw,
8138 size_t pw_len )
8139{
8140 mbedtls_ecjpake_role role;
8141
Janos Follath8eb64132016-06-03 15:40:57 +01008142 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8144
8145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8146 role = MBEDTLS_ECJPAKE_SERVER;
8147 else
8148 role = MBEDTLS_ECJPAKE_CLIENT;
8149
8150 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8151 role,
8152 MBEDTLS_MD_SHA256,
8153 MBEDTLS_ECP_DP_SECP256R1,
8154 pw, pw_len ) );
8155}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008156#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008159
8160static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8161{
8162 /* Remove reference to existing PSK, if any. */
8163#if defined(MBEDTLS_USE_PSA_CRYPTO)
8164 if( conf->psk_opaque != 0 )
8165 {
8166 /* The maintenance of the PSK key slot is the
8167 * user's responsibility. */
8168 conf->psk_opaque = 0;
8169 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008170 /* This and the following branch should never
8171 * be taken simultaenously as we maintain the
8172 * invariant that raw and opaque PSKs are never
8173 * configured simultaneously. As a safeguard,
8174 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008175#endif /* MBEDTLS_USE_PSA_CRYPTO */
8176 if( conf->psk != NULL )
8177 {
8178 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8179
8180 mbedtls_free( conf->psk );
8181 conf->psk = NULL;
8182 conf->psk_len = 0;
8183 }
8184
8185 /* Remove reference to PSK identity, if any. */
8186 if( conf->psk_identity != NULL )
8187 {
8188 mbedtls_free( conf->psk_identity );
8189 conf->psk_identity = NULL;
8190 conf->psk_identity_len = 0;
8191 }
8192}
8193
Hanno Becker7390c712018-11-15 13:33:04 +00008194/* This function assumes that PSK identity in the SSL config is unset.
8195 * It checks that the provided identity is well-formed and attempts
8196 * to make a copy of it in the SSL config.
8197 * On failure, the PSK identity in the config remains unset. */
8198static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8199 unsigned char const *psk_identity,
8200 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008201{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008202 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008203 if( psk_identity == NULL ||
8204 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008205 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008206 {
8207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8208 }
8209
Hanno Becker7390c712018-11-15 13:33:04 +00008210 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8211 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008212 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008213
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008214 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008215 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008216
8217 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008218}
8219
Hanno Becker7390c712018-11-15 13:33:04 +00008220int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8221 const unsigned char *psk, size_t psk_len,
8222 const unsigned char *psk_identity, size_t psk_identity_len )
8223{
8224 int ret;
8225 /* Remove opaque/raw PSK + PSK Identity */
8226 ssl_conf_remove_psk( conf );
8227
8228 /* Check and set raw PSK */
8229 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8231 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8233 conf->psk_len = psk_len;
8234 memcpy( conf->psk, psk, conf->psk_len );
8235
8236 /* Check and set PSK Identity */
8237 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8238 if( ret != 0 )
8239 ssl_conf_remove_psk( conf );
8240
8241 return( ret );
8242}
8243
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008244static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8245{
8246#if defined(MBEDTLS_USE_PSA_CRYPTO)
8247 if( ssl->handshake->psk_opaque != 0 )
8248 {
8249 ssl->handshake->psk_opaque = 0;
8250 }
8251 else
8252#endif /* MBEDTLS_USE_PSA_CRYPTO */
8253 if( ssl->handshake->psk != NULL )
8254 {
8255 mbedtls_platform_zeroize( ssl->handshake->psk,
8256 ssl->handshake->psk_len );
8257 mbedtls_free( ssl->handshake->psk );
8258 ssl->handshake->psk_len = 0;
8259 }
8260}
8261
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008262int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8263 const unsigned char *psk, size_t psk_len )
8264{
8265 if( psk == NULL || ssl->handshake == NULL )
8266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8267
8268 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8270
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008271 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008272
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008273 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008274 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008275
8276 ssl->handshake->psk_len = psk_len;
8277 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8278
8279 return( 0 );
8280}
8281
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008282#if defined(MBEDTLS_USE_PSA_CRYPTO)
8283int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008284 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008285 const unsigned char *psk_identity,
8286 size_t psk_identity_len )
8287{
Hanno Becker7390c712018-11-15 13:33:04 +00008288 int ret;
8289 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008290 ssl_conf_remove_psk( conf );
8291
Hanno Becker7390c712018-11-15 13:33:04 +00008292 /* Check and set opaque PSK */
8293 if( psk_slot == 0 )
8294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008295 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008296
8297 /* Check and set PSK Identity */
8298 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8299 psk_identity_len );
8300 if( ret != 0 )
8301 ssl_conf_remove_psk( conf );
8302
8303 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008304}
8305
8306int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008307 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008308{
8309 if( psk_slot == 0 || ssl->handshake == NULL )
8310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8311
8312 ssl_remove_psk( ssl );
8313 ssl->handshake->psk_opaque = psk_slot;
8314 return( 0 );
8315}
8316#endif /* MBEDTLS_USE_PSA_CRYPTO */
8317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008318void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008320 size_t),
8321 void *p_psk )
8322{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008323 conf->f_psk = f_psk;
8324 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008327
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008328#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008329
8330#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008331int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008332{
8333 int ret;
8334
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008335 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8336 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8337 {
8338 mbedtls_mpi_free( &conf->dhm_P );
8339 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008340 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008341 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008342
8343 return( 0 );
8344}
Hanno Becker470a8c42017-10-04 15:28:46 +01008345#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008346
Hanno Beckera90658f2017-10-04 15:29:08 +01008347int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8348 const unsigned char *dhm_P, size_t P_len,
8349 const unsigned char *dhm_G, size_t G_len )
8350{
8351 int ret;
8352
8353 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8354 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8355 {
8356 mbedtls_mpi_free( &conf->dhm_P );
8357 mbedtls_mpi_free( &conf->dhm_G );
8358 return( ret );
8359 }
8360
8361 return( 0 );
8362}
Paul Bakker5121ce52009-01-03 21:22:43 +00008363
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008364int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008365{
8366 int ret;
8367
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008368 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8369 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8370 {
8371 mbedtls_mpi_free( &conf->dhm_P );
8372 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008373 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008374 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008375
8376 return( 0 );
8377}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008378#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008379
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008380#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8381/*
8382 * Set the minimum length for Diffie-Hellman parameters
8383 */
8384void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8385 unsigned int bitlen )
8386{
8387 conf->dhm_min_bitlen = bitlen;
8388}
8389#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8390
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008391#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008392/*
8393 * Set allowed/preferred hashes for handshake signatures
8394 */
8395void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8396 const int *hashes )
8397{
8398 conf->sig_hashes = hashes;
8399}
Hanno Becker947194e2017-04-07 13:25:49 +01008400#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008401
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008402#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008403/*
8404 * Set the allowed elliptic curves
8405 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008406void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008410}
Hanno Becker947194e2017-04-07 13:25:49 +01008411#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008412
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008413#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008415{
Hanno Becker947194e2017-04-07 13:25:49 +01008416 /* Initialize to suppress unnecessary compiler warning */
8417 size_t hostname_len = 0;
8418
8419 /* Check if new hostname is valid before
8420 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008421 if( hostname != NULL )
8422 {
8423 hostname_len = strlen( hostname );
8424
8425 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8427 }
8428
8429 /* Now it's clear that we will overwrite the old hostname,
8430 * so we can free it safely */
8431
8432 if( ssl->hostname != NULL )
8433 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008434 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008435 mbedtls_free( ssl->hostname );
8436 }
8437
8438 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008439
Paul Bakker5121ce52009-01-03 21:22:43 +00008440 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008441 {
8442 ssl->hostname = NULL;
8443 }
8444 else
8445 {
8446 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008447 if( ssl->hostname == NULL )
8448 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008449
Hanno Becker947194e2017-04-07 13:25:49 +01008450 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008451
Hanno Becker947194e2017-04-07 13:25:49 +01008452 ssl->hostname[hostname_len] = '\0';
8453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008454
8455 return( 0 );
8456}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008457#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008458
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008459#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008462 const unsigned char *, size_t),
8463 void *p_sni )
8464{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008465 conf->f_sni = f_sni;
8466 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008471int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008472{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008473 size_t cur_len, tot_len;
8474 const char **p;
8475
8476 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008477 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8478 * MUST NOT be truncated."
8479 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008480 */
8481 tot_len = 0;
8482 for( p = protos; *p != NULL; p++ )
8483 {
8484 cur_len = strlen( *p );
8485 tot_len += cur_len;
8486
8487 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008489 }
8490
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008492
8493 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008494}
8495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008497{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008498 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008501
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008502void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008504 conf->max_major_ver = major;
8505 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008506}
8507
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008508void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008510 conf->min_major_ver = major;
8511 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008512}
8513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008515void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008516{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008517 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008518}
8519#endif
8520
Janos Follath088ce432017-04-10 12:42:31 +01008521#if defined(MBEDTLS_SSL_SRV_C)
8522void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8523 char cert_req_ca_list )
8524{
8525 conf->cert_req_ca_list = cert_req_ca_list;
8526}
8527#endif
8528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008530void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008532 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008533}
8534#endif
8535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008537void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008539 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008540}
8541#endif
8542
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008543#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008544void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008546 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008547}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008548#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008551int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008554 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008557 }
8558
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008559 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008560
8561 return( 0 );
8562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008566void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008568 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008573void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008574{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008575 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008576}
8577#endif
8578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008581 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008582}
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008585void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008587 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008588}
8589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008592 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008593}
8594
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008595void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008596 const unsigned char period[8] )
8597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008598 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008603#if defined(MBEDTLS_SSL_CLI_C)
8604void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008605{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008606 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008607}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008608#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008609
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008610#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008611void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8612 mbedtls_ssl_ticket_write_t *f_ticket_write,
8613 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8614 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008615{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008616 conf->f_ticket_write = f_ticket_write;
8617 conf->f_ticket_parse = f_ticket_parse;
8618 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008619}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008620#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008622
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008623#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8624void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8625 mbedtls_ssl_export_keys_t *f_export_keys,
8626 void *p_export_keys )
8627{
8628 conf->f_export_keys = f_export_keys;
8629 conf->p_export_keys = p_export_keys;
8630}
8631#endif
8632
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008633#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008634void mbedtls_ssl_conf_async_private_cb(
8635 mbedtls_ssl_config *conf,
8636 mbedtls_ssl_async_sign_t *f_async_sign,
8637 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8638 mbedtls_ssl_async_resume_t *f_async_resume,
8639 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008640 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008641{
8642 conf->f_async_sign_start = f_async_sign;
8643 conf->f_async_decrypt_start = f_async_decrypt;
8644 conf->f_async_resume = f_async_resume;
8645 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008646 conf->p_async_config_data = async_config_data;
8647}
8648
Gilles Peskine8f97af72018-04-26 11:46:10 +02008649void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8650{
8651 return( conf->p_async_config_data );
8652}
8653
Gilles Peskine1febfef2018-04-30 11:54:39 +02008654void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008655{
8656 if( ssl->handshake == NULL )
8657 return( NULL );
8658 else
8659 return( ssl->handshake->user_async_ctx );
8660}
8661
Gilles Peskine1febfef2018-04-30 11:54:39 +02008662void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008663 void *ctx )
8664{
8665 if( ssl->handshake != NULL )
8666 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008667}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008668#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008669
Paul Bakker5121ce52009-01-03 21:22:43 +00008670/*
8671 * SSL get accessors
8672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008674{
8675 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8676}
8677
Hanno Becker8b170a02017-10-10 11:51:19 +01008678int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8679{
8680 /*
8681 * Case A: We're currently holding back
8682 * a message for further processing.
8683 */
8684
8685 if( ssl->keep_current_message == 1 )
8686 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008688 return( 1 );
8689 }
8690
8691 /*
8692 * Case B: Further records are pending in the current datagram.
8693 */
8694
8695#if defined(MBEDTLS_SSL_PROTO_DTLS)
8696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8697 ssl->in_left > ssl->next_record_offset )
8698 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008700 return( 1 );
8701 }
8702#endif /* MBEDTLS_SSL_PROTO_DTLS */
8703
8704 /*
8705 * Case C: A handshake message is being processed.
8706 */
8707
Hanno Becker8b170a02017-10-10 11:51:19 +01008708 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8709 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008711 return( 1 );
8712 }
8713
8714 /*
8715 * Case D: An application data message is being processed
8716 */
8717 if( ssl->in_offt != NULL )
8718 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008720 return( 1 );
8721 }
8722
8723 /*
8724 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008725 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008726 * we implement support for multiple alerts in single records.
8727 */
8728
8729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8730 return( 0 );
8731}
8732
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008733uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008734{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008735 if( ssl->session != NULL )
8736 return( ssl->session->verify_result );
8737
8738 if( ssl->session_negotiate != NULL )
8739 return( ssl->session_negotiate->verify_result );
8740
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008741 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008742}
8743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008745{
Paul Bakker926c8e42013-03-06 10:23:34 +01008746 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008747 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008750}
8751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008756 {
8757 switch( ssl->minor_ver )
8758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008760 return( "DTLSv1.0" );
8761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008763 return( "DTLSv1.2" );
8764
8765 default:
8766 return( "unknown (DTLS)" );
8767 }
8768 }
8769#endif
8770
Paul Bakker43ca69c2011-01-15 17:35:19 +00008771 switch( ssl->minor_ver )
8772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008774 return( "SSLv3.0" );
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008777 return( "TLSv1.0" );
8778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008780 return( "TLSv1.1" );
8781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008783 return( "TLSv1.2" );
8784
Paul Bakker43ca69c2011-01-15 17:35:19 +00008785 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008786 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008787 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008788}
8789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008791{
Hanno Becker3136ede2018-08-17 15:28:19 +01008792 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008794 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008795
Hanno Becker78640902018-08-13 16:35:15 +01008796 if( transform == NULL )
8797 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_ZLIB_SUPPORT)
8800 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8801 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008802#endif
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 case MBEDTLS_MODE_GCM:
8807 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008808 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008810 transform_expansion = transform->minlen;
8811 break;
8812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008814
8815 block_size = mbedtls_cipher_get_block_size(
8816 &transform->cipher_ctx_enc );
8817
Hanno Becker3136ede2018-08-17 15:28:19 +01008818 /* Expansion due to the addition of the MAC. */
8819 transform_expansion += transform->maclen;
8820
8821 /* Expansion due to the addition of CBC padding;
8822 * Theoretically up to 256 bytes, but we never use
8823 * more than the block size of the underlying cipher. */
8824 transform_expansion += block_size;
8825
8826 /* For TLS 1.1 or higher, an explicit IV is added
8827 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008828#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8829 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008830 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008831#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008832
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008833 break;
8834
8835 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008838 }
8839
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008840 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008841}
8842
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008843#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8844size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8845{
8846 size_t max_len;
8847
8848 /*
8849 * Assume mfl_code is correct since it was checked when set
8850 */
Angus Grattond8213d02016-05-25 20:56:48 +10008851 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008852
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008853 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008854 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008855 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008856 {
Angus Grattond8213d02016-05-25 20:56:48 +10008857 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008858 }
8859
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008860 /* During a handshake, use the value being negotiated */
8861 if( ssl->session_negotiate != NULL &&
8862 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8863 {
8864 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8865 }
8866
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008867 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008868}
8869#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8870
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008871#if defined(MBEDTLS_SSL_PROTO_DTLS)
8872static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8873{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008874 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8876 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8877 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8878 return ( 0 );
8879
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008880 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8881 return( ssl->mtu );
8882
8883 if( ssl->mtu == 0 )
8884 return( ssl->handshake->mtu );
8885
8886 return( ssl->mtu < ssl->handshake->mtu ?
8887 ssl->mtu : ssl->handshake->mtu );
8888}
8889#endif /* MBEDTLS_SSL_PROTO_DTLS */
8890
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008891int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8892{
8893 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8894
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008895#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8896 !defined(MBEDTLS_SSL_PROTO_DTLS)
8897 (void) ssl;
8898#endif
8899
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008900#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8901 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8902
8903 if( max_len > mfl )
8904 max_len = mfl;
8905#endif
8906
8907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008908 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008909 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008910 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008911 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8912 const size_t overhead = (size_t) ret;
8913
8914 if( ret < 0 )
8915 return( ret );
8916
8917 if( mtu <= overhead )
8918 {
8919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8920 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8921 }
8922
8923 if( max_len > mtu - overhead )
8924 max_len = mtu - overhead;
8925 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008926#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008927
Hanno Becker0defedb2018-08-10 12:35:02 +01008928#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8929 !defined(MBEDTLS_SSL_PROTO_DTLS)
8930 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008931#endif
8932
8933 return( (int) max_len );
8934}
8935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936#if defined(MBEDTLS_X509_CRT_PARSE_C)
8937const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008938{
8939 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008940 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008941
Hanno Beckere6824572019-02-07 13:18:46 +00008942#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008943 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008944#else
8945 return( NULL );
8946#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008951int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8952 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008953{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008954 if( ssl == NULL ||
8955 dst == NULL ||
8956 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008957 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008960 }
8961
Hanno Becker52055ae2019-02-06 14:30:46 +00008962 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008965
Paul Bakker5121ce52009-01-03 21:22:43 +00008966/*
Paul Bakker1961b702013-01-25 14:49:24 +01008967 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008972
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008973 if( ssl == NULL || ssl->conf == NULL )
8974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008977 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008981 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008983#endif
8984
Paul Bakker1961b702013-01-25 14:49:24 +01008985 return( ret );
8986}
8987
8988/*
8989 * Perform the SSL handshake
8990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008992{
8993 int ret = 0;
8994
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008995 if( ssl == NULL || ssl->conf == NULL )
8996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009003
9004 if( ret != 0 )
9005 break;
9006 }
9007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009009
9010 return( ret );
9011}
9012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#if defined(MBEDTLS_SSL_RENEGOTIATION)
9014#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009015/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009016 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009019{
9020 int ret;
9021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009023
9024 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9026 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009027
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009028 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009029 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009031 return( ret );
9032 }
9033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009035
9036 return( 0 );
9037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009039
9040/*
9041 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042 * - any side: calling mbedtls_ssl_renegotiate(),
9043 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9044 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009045 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009046 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009050{
9051 int ret;
9052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009054
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009055 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9056 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009057
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009058 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9059 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009063 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009064 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009065 ssl->handshake->out_msg_seq = 1;
9066 else
9067 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009068 }
9069#endif
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9072 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009077 return( ret );
9078 }
9079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009081
9082 return( 0 );
9083}
9084
9085/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009086 * Renegotiate current connection on client,
9087 * or request renegotiation on server
9088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009092
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009093 if( ssl == NULL || ssl->conf == NULL )
9094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009097 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009098 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009104
9105 /* Did we already try/start sending HelloRequest? */
9106 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009108
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009109 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009114 /*
9115 * On client, either start the renegotiation process or,
9116 * if already in progress, continue the handshake
9117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009122
9123 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009126 return( ret );
9127 }
9128 }
9129 else
9130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009134 return( ret );
9135 }
9136 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009137#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009138
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009139 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009140}
9141
9142/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009143 * Check record counters and renegotiate if they're above the limit.
9144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009146{
Andres AG2196c7f2016-12-15 17:01:16 +00009147 size_t ep_len = ssl_ep_len( ssl );
9148 int in_ctr_cmp;
9149 int out_ctr_cmp;
9150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9152 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009153 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009154 {
9155 return( 0 );
9156 }
9157
Andres AG2196c7f2016-12-15 17:01:16 +00009158 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9159 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009160 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009161 ssl->conf->renego_period + ep_len, 8 - ep_len );
9162
9163 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009164 {
9165 return( 0 );
9166 }
9167
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009172
9173/*
9174 * Receive application data decrypted from the SSL layer
9175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009177{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009178 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009179 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009181 if( ssl == NULL || ssl->conf == NULL )
9182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009190 return( ret );
9191
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009192 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009194 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009195 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009196 return( ret );
9197 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009198 }
9199#endif
9200
Hanno Becker4a810fb2017-05-24 16:27:30 +01009201 /*
9202 * Check if renegotiation is necessary and/or handshake is
9203 * in process. If yes, perform/continue, and fall through
9204 * if an unexpected packet is received while the client
9205 * is waiting for the ServerHello.
9206 *
9207 * (There is no equivalent to the last condition on
9208 * the server-side as it is not treated as within
9209 * a handshake while waiting for the ClientHello
9210 * after a renegotiation request.)
9211 */
9212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009214 ret = ssl_check_ctr_renegotiate( ssl );
9215 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9216 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009219 return( ret );
9220 }
9221#endif
9222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009226 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9227 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009230 return( ret );
9231 }
9232 }
9233
Hanno Beckere41158b2017-10-23 13:30:32 +01009234 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009235 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009236 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009237 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009238 if( ssl->f_get_timer != NULL &&
9239 ssl->f_get_timer( ssl->p_timer ) == -1 )
9240 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009241 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009242 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009243
Hanno Becker327c93b2018-08-15 13:56:18 +01009244 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009245 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009246 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9247 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009248
Hanno Becker4a810fb2017-05-24 16:27:30 +01009249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9250 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009251 }
9252
9253 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009255 {
9256 /*
9257 * OpenSSL sends empty messages to randomize the IV
9258 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009259 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009262 return( 0 );
9263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009265 return( ret );
9266 }
9267 }
9268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009272
Hanno Becker4a810fb2017-05-24 16:27:30 +01009273 /*
9274 * - For client-side, expect SERVER_HELLO_REQUEST.
9275 * - For server-side, expect CLIENT_HELLO.
9276 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9277 */
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009282 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009285
9286 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009289 {
9290 continue;
9291 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009294 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009295#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009296
Hanno Becker4a810fb2017-05-24 16:27:30 +01009297#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009302
9303 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009306 {
9307 continue;
9308 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009309#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009311 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009312#endif /* MBEDTLS_SSL_SRV_C */
9313
Hanno Becker21df7f92017-10-17 11:03:26 +01009314#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009315 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009316 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9317 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9318 ssl->conf->allow_legacy_renegotiation ==
9319 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9320 {
9321 /*
9322 * Accept renegotiation request
9323 */
Paul Bakker48916f92012-09-16 19:57:18 +00009324
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009325 /* DTLS clients need to know renego is server-initiated */
9326#if defined(MBEDTLS_SSL_PROTO_DTLS)
9327 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9328 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9329 {
9330 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9331 }
9332#endif
9333 ret = ssl_start_renegotiation( ssl );
9334 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9335 ret != 0 )
9336 {
9337 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9338 return( ret );
9339 }
9340 }
9341 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009342#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009343 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009344 /*
9345 * Refuse renegotiation
9346 */
9347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_SSL_PROTO_SSL3)
9351 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009352 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009353 /* SSLv3 does not have a "no_renegotiation" warning, so
9354 we send a fatal alert and abort the connection. */
9355 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9356 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9357 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009358 }
9359 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9362 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9363 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9366 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9367 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009368 {
9369 return( ret );
9370 }
Paul Bakker48916f92012-09-16 19:57:18 +00009371 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9374 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9377 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009378 }
Paul Bakker48916f92012-09-16 19:57:18 +00009379 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009380
Hanno Becker90333da2017-10-10 11:27:13 +01009381 /* At this point, we don't know whether the renegotiation has been
9382 * completed or not. The cases to consider are the following:
9383 * 1) The renegotiation is complete. In this case, no new record
9384 * has been read yet.
9385 * 2) The renegotiation is incomplete because the client received
9386 * an application data record while awaiting the ServerHello.
9387 * 3) The renegotiation is incomplete because the client received
9388 * a non-handshake, non-application data message while awaiting
9389 * the ServerHello.
9390 * In each of these case, looping will be the proper action:
9391 * - For 1), the next iteration will read a new record and check
9392 * if it's application data.
9393 * - For 2), the loop condition isn't satisfied as application data
9394 * is present, hence continue is the same as break
9395 * - For 3), the loop condition is satisfied and read_record
9396 * will re-deliver the message that was held back by the client
9397 * when expecting the ServerHello.
9398 */
9399 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009400 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009401#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009403 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009404 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009405 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009406 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009409 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009411 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009412 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9417 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009420 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009421 }
9422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9426 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009427 }
9428
9429 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009430
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009431 /* We're going to return something now, cancel timer,
9432 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009434 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009435
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009437 /* If we requested renego but received AppData, resend HelloRequest.
9438 * Do it now, after setting in_offt, to avoid taking this branch
9439 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009441 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009443 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009444 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009447 return( ret );
9448 }
9449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009451#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009452 }
9453
9454 n = ( len < ssl->in_msglen )
9455 ? len : ssl->in_msglen;
9456
9457 memcpy( buf, ssl->in_offt, n );
9458 ssl->in_msglen -= n;
9459
9460 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009461 {
9462 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009463 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009464 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009466 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009467 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009468 /* more data available */
9469 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009473
Paul Bakker23986e52011-04-24 08:57:21 +00009474 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009475}
9476
9477/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009478 * Send application data to be encrypted by the SSL layer, taking care of max
9479 * fragment length and buffer size.
9480 *
9481 * According to RFC 5246 Section 6.2.1:
9482 *
9483 * Zero-length fragments of Application data MAY be sent as they are
9484 * potentially useful as a traffic analysis countermeasure.
9485 *
9486 * Therefore, it is possible that the input message length is 0 and the
9487 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009488 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009489static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009490 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009491{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009492 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9493 const size_t max_len = (size_t) ret;
9494
9495 if( ret < 0 )
9496 {
9497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9498 return( ret );
9499 }
9500
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009501 if( len > max_len )
9502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009507 "maximum fragment length: %d > %d",
9508 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009510 }
9511 else
9512#endif
9513 len = max_len;
9514 }
Paul Bakker887bd502011-06-08 13:10:54 +00009515
Paul Bakker5121ce52009-01-03 21:22:43 +00009516 if( ssl->out_left != 0 )
9517 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009518 /*
9519 * The user has previously tried to send the data and
9520 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9521 * written. In this case, we expect the high-level write function
9522 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009527 return( ret );
9528 }
9529 }
Paul Bakker887bd502011-06-08 13:10:54 +00009530 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009531 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009532 /*
9533 * The user is trying to send a message the first time, so we need to
9534 * copy the data into the internal buffers and setup the data structure
9535 * to keep track of partial writes
9536 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009537 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009539 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009540
Hanno Becker67bc7c32018-08-06 11:33:50 +01009541 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009544 return( ret );
9545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009546 }
9547
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009548 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009549}
9550
9551/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009552 * Write application data, doing 1/n-1 splitting if necessary.
9553 *
9554 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009555 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009556 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009559static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009560 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009561{
9562 int ret;
9563
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009564 if( ssl->conf->cbc_record_splitting ==
9565 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009566 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9568 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9569 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009570 {
9571 return( ssl_write_real( ssl, buf, len ) );
9572 }
9573
9574 if( ssl->split_done == 0 )
9575 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009576 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009577 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009578 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009579 }
9580
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009581 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9582 return( ret );
9583 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009584
9585 return( ret + 1 );
9586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009588
9589/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009590 * Write application data (public-facing wrapper)
9591 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009592int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009593{
9594 int ret;
9595
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009597
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009598 if( ssl == NULL || ssl->conf == NULL )
9599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9600
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009601#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009602 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9603 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009604 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009605 return( ret );
9606 }
9607#endif
9608
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009609 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009610 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009611 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009612 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009614 return( ret );
9615 }
9616 }
9617
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009618#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009619 ret = ssl_write_split( ssl, buf, len );
9620#else
9621 ret = ssl_write_real( ssl, buf, len );
9622#endif
9623
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009625
9626 return( ret );
9627}
9628
9629/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 * Notify the peer that the connection is being closed
9631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009633{
9634 int ret;
9635
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009636 if( ssl == NULL || ssl->conf == NULL )
9637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009640
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009641 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9647 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9648 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009651 return( ret );
9652 }
9653 }
9654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009656
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009657 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009658}
9659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009661{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009662 if( transform == NULL )
9663 return;
9664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009666 deflateEnd( &transform->ctx_deflate );
9667 inflateEnd( &transform->ctx_inflate );
9668#endif
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9671 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009672
Hanno Beckerd56ed242018-01-03 15:32:51 +00009673#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 mbedtls_md_free( &transform->md_ctx_enc );
9675 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009676#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009677
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009678 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009679}
9680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#if defined(MBEDTLS_X509_CRT_PARSE_C)
9682static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009685
9686 while( cur != NULL )
9687 {
9688 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009690 cur = next;
9691 }
9692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009694
Hanno Becker0271f962018-08-16 13:23:47 +01009695#if defined(MBEDTLS_SSL_PROTO_DTLS)
9696
9697static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9698{
9699 unsigned offset;
9700 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9701
9702 if( hs == NULL )
9703 return;
9704
Hanno Becker283f5ef2018-08-24 09:34:47 +01009705 ssl_free_buffered_record( ssl );
9706
Hanno Becker0271f962018-08-16 13:23:47 +01009707 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009708 ssl_buffering_free_slot( ssl, offset );
9709}
9710
9711static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9712 uint8_t slot )
9713{
9714 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9715 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009716
9717 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9718 return;
9719
Hanno Beckere605b192018-08-21 15:59:07 +01009720 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009721 {
Hanno Beckere605b192018-08-21 15:59:07 +01009722 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009723 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009724 mbedtls_free( hs_buf->data );
9725 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009726 }
9727}
9728
9729#endif /* MBEDTLS_SSL_PROTO_DTLS */
9730
Gilles Peskine9b562d52018-04-25 20:32:43 +02009731void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009732{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009733 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9734
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009735 if( handshake == NULL )
9736 return;
9737
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009738#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9739 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9740 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009741 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009742 handshake->async_in_progress = 0;
9743 }
9744#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9745
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009746#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9747 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9748 mbedtls_md5_free( &handshake->fin_md5 );
9749 mbedtls_sha1_free( &handshake->fin_sha1 );
9750#endif
9751#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9752#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009753#if defined(MBEDTLS_USE_PSA_CRYPTO)
9754 psa_hash_abort( &handshake->fin_sha256_psa );
9755#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009756 mbedtls_sha256_free( &handshake->fin_sha256 );
9757#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009758#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009759#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009760#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009761 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009762#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009763 mbedtls_sha512_free( &handshake->fin_sha512 );
9764#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009765#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768#if defined(MBEDTLS_DHM_C)
9769 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771#if defined(MBEDTLS_ECDH_C)
9772 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009773#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009774#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009775 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009776#if defined(MBEDTLS_SSL_CLI_C)
9777 mbedtls_free( handshake->ecjpake_cache );
9778 handshake->ecjpake_cache = NULL;
9779 handshake->ecjpake_cache_len = 0;
9780#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009781#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009782
Janos Follath4ae5c292016-02-10 11:27:43 +00009783#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9784 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009785 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009787#endif
9788
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009789#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9790 if( handshake->psk != NULL )
9791 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009792 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009793 mbedtls_free( handshake->psk );
9794 }
9795#endif
9796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9798 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009799 /*
9800 * Free only the linked list wrapper, not the keys themselves
9801 * since the belong to the SNI callback
9802 */
9803 if( handshake->sni_key_cert != NULL )
9804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009806
9807 while( cur != NULL )
9808 {
9809 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009811 cur = next;
9812 }
9813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009815
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009816#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009817 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009818 if( handshake->ecrs_peer_cert != NULL )
9819 {
9820 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9821 mbedtls_free( handshake->ecrs_peer_cert );
9822 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009823#endif
9824
Hanno Becker75173122019-02-06 16:18:31 +00009825#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9826 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9827 mbedtls_pk_free( &handshake->peer_pubkey );
9828#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830#if defined(MBEDTLS_SSL_PROTO_DTLS)
9831 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009832 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009833 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009834#endif
9835
Hanno Becker4a63ed42019-01-08 11:39:35 +00009836#if defined(MBEDTLS_ECDH_C) && \
9837 defined(MBEDTLS_USE_PSA_CRYPTO)
9838 psa_destroy_key( handshake->ecdh_psa_privkey );
9839#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9840
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009841 mbedtls_platform_zeroize( handshake,
9842 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009843}
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009846{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009847 if( session == NULL )
9848 return;
9849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009851 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009852#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009853
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009854#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009856#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009857
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009858 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009859}
9860
Paul Bakker5121ce52009-01-03 21:22:43 +00009861/*
9862 * Free an SSL context
9863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009865{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009866 if( ssl == NULL )
9867 return;
9868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009870
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009871 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009872 {
Angus Grattond8213d02016-05-25 20:56:48 +10009873 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009875 }
9876
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009877 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009878 {
Angus Grattond8213d02016-05-25 20:56:48 +10009879 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009881 }
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009884 if( ssl->compress_buf != NULL )
9885 {
Angus Grattond8213d02016-05-25 20:56:48 +10009886 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009888 }
9889#endif
9890
Paul Bakker48916f92012-09-16 19:57:18 +00009891 if( ssl->transform )
9892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 mbedtls_ssl_transform_free( ssl->transform );
9894 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009895 }
9896
9897 if( ssl->handshake )
9898 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009899 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9901 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 mbedtls_free( ssl->handshake );
9904 mbedtls_free( ssl->transform_negotiate );
9905 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009906 }
9907
Paul Bakkerc0463502013-02-14 11:19:38 +01009908 if( ssl->session )
9909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 mbedtls_ssl_session_free( ssl->session );
9911 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009912 }
9913
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009914#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009915 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009917 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009919 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9923 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9926 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009927 }
9928#endif
9929
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009930#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009932#endif
9933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009935
Paul Bakker86f04f42013-02-14 11:20:09 +01009936 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009937 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009938}
9939
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009940/*
9941 * Initialze mbedtls_ssl_config
9942 */
9943void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9944{
9945 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9946}
9947
Simon Butcherc97b6972015-12-27 23:48:17 +00009948#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009949static int ssl_preset_default_hashes[] = {
9950#if defined(MBEDTLS_SHA512_C)
9951 MBEDTLS_MD_SHA512,
9952 MBEDTLS_MD_SHA384,
9953#endif
9954#if defined(MBEDTLS_SHA256_C)
9955 MBEDTLS_MD_SHA256,
9956 MBEDTLS_MD_SHA224,
9957#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009958#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009959 MBEDTLS_MD_SHA1,
9960#endif
9961 MBEDTLS_MD_NONE
9962};
Simon Butcherc97b6972015-12-27 23:48:17 +00009963#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009964
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009965static int ssl_preset_suiteb_ciphersuites[] = {
9966 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9967 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9968 0
9969};
9970
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009971#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009972static int ssl_preset_suiteb_hashes[] = {
9973 MBEDTLS_MD_SHA256,
9974 MBEDTLS_MD_SHA384,
9975 MBEDTLS_MD_NONE
9976};
9977#endif
9978
9979#if defined(MBEDTLS_ECP_C)
9980static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9981 MBEDTLS_ECP_DP_SECP256R1,
9982 MBEDTLS_ECP_DP_SECP384R1,
9983 MBEDTLS_ECP_DP_NONE
9984};
9985#endif
9986
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009987/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009988 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009989 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009990int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009991 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009992{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009993#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009994 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009995#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009996
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009997 /* Use the functions here so that they are covered in tests,
9998 * but otherwise access member directly for efficiency */
9999 mbedtls_ssl_conf_endpoint( conf, endpoint );
10000 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010001
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010002 /*
10003 * Things that are common to all presets
10004 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010005#if defined(MBEDTLS_SSL_CLI_C)
10006 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10007 {
10008 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10010 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10011#endif
10012 }
10013#endif
10014
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010015#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010016 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010017#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010018
10019#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10020 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10021#endif
10022
10023#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10024 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10025#endif
10026
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010027#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10028 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10029#endif
10030
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010031#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010032 conf->f_cookie_write = ssl_cookie_write_dummy;
10033 conf->f_cookie_check = ssl_cookie_check_dummy;
10034#endif
10035
10036#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10037 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10038#endif
10039
Janos Follath088ce432017-04-10 12:42:31 +010010040#if defined(MBEDTLS_SSL_SRV_C)
10041 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10042#endif
10043
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010044#if defined(MBEDTLS_SSL_PROTO_DTLS)
10045 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10046 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10047#endif
10048
10049#if defined(MBEDTLS_SSL_RENEGOTIATION)
10050 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010051 memset( conf->renego_period, 0x00, 2 );
10052 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010053#endif
10054
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010055#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10056 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10057 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010058 const unsigned char dhm_p[] =
10059 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10060 const unsigned char dhm_g[] =
10061 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10062
Hanno Beckera90658f2017-10-04 15:29:08 +010010063 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10064 dhm_p, sizeof( dhm_p ),
10065 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010066 {
10067 return( ret );
10068 }
10069 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010070#endif
10071
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010072 /*
10073 * Preset-specific defaults
10074 */
10075 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010076 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010077 /*
10078 * NSA Suite B
10079 */
10080 case MBEDTLS_SSL_PRESET_SUITEB:
10081 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10082 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10083 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10084 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10085
10086 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10087 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10088 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10089 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10090 ssl_preset_suiteb_ciphersuites;
10091
10092#if defined(MBEDTLS_X509_CRT_PARSE_C)
10093 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010094#endif
10095
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010096#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010097 conf->sig_hashes = ssl_preset_suiteb_hashes;
10098#endif
10099
10100#if defined(MBEDTLS_ECP_C)
10101 conf->curve_list = ssl_preset_suiteb_curves;
10102#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010103 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010104
10105 /*
10106 * Default
10107 */
10108 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010109 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10110 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10111 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10112 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10113 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10114 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10115 MBEDTLS_SSL_MIN_MINOR_VERSION :
10116 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010117 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10118 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10119
10120#if defined(MBEDTLS_SSL_PROTO_DTLS)
10121 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10122 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10123#endif
10124
10125 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10126 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10127 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10128 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10129 mbedtls_ssl_list_ciphersuites();
10130
10131#if defined(MBEDTLS_X509_CRT_PARSE_C)
10132 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10133#endif
10134
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010135#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010136 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010137#endif
10138
10139#if defined(MBEDTLS_ECP_C)
10140 conf->curve_list = mbedtls_ecp_grp_id_list();
10141#endif
10142
10143#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10144 conf->dhm_min_bitlen = 1024;
10145#endif
10146 }
10147
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010148 return( 0 );
10149}
10150
10151/*
10152 * Free mbedtls_ssl_config
10153 */
10154void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10155{
10156#if defined(MBEDTLS_DHM_C)
10157 mbedtls_mpi_free( &conf->dhm_P );
10158 mbedtls_mpi_free( &conf->dhm_G );
10159#endif
10160
10161#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10162 if( conf->psk != NULL )
10163 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010164 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010165 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010166 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010167 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010168 }
10169
10170 if( conf->psk_identity != NULL )
10171 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010172 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010173 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010174 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010175 conf->psk_identity_len = 0;
10176 }
10177#endif
10178
10179#if defined(MBEDTLS_X509_CRT_PARSE_C)
10180 ssl_key_cert_free( conf->key_cert );
10181#endif
10182
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010183 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010184}
10185
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010186#if defined(MBEDTLS_PK_C) && \
10187 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010188/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193#if defined(MBEDTLS_RSA_C)
10194 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10195 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#if defined(MBEDTLS_ECDSA_C)
10198 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10199 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010202}
10203
Hanno Becker7e5437a2017-04-28 17:15:26 +010010204unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10205{
10206 switch( type ) {
10207 case MBEDTLS_PK_RSA:
10208 return( MBEDTLS_SSL_SIG_RSA );
10209 case MBEDTLS_PK_ECDSA:
10210 case MBEDTLS_PK_ECKEY:
10211 return( MBEDTLS_SSL_SIG_ECDSA );
10212 default:
10213 return( MBEDTLS_SSL_SIG_ANON );
10214 }
10215}
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010218{
10219 switch( sig )
10220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221#if defined(MBEDTLS_RSA_C)
10222 case MBEDTLS_SSL_SIG_RSA:
10223 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225#if defined(MBEDTLS_ECDSA_C)
10226 case MBEDTLS_SSL_SIG_ECDSA:
10227 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010228#endif
10229 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010231 }
10232}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010233#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010234
Hanno Becker7e5437a2017-04-28 17:15:26 +010010235#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10236 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10237
10238/* Find an entry in a signature-hash set matching a given hash algorithm. */
10239mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10240 mbedtls_pk_type_t sig_alg )
10241{
10242 switch( sig_alg )
10243 {
10244 case MBEDTLS_PK_RSA:
10245 return( set->rsa );
10246 case MBEDTLS_PK_ECDSA:
10247 return( set->ecdsa );
10248 default:
10249 return( MBEDTLS_MD_NONE );
10250 }
10251}
10252
10253/* Add a signature-hash-pair to a signature-hash set */
10254void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10255 mbedtls_pk_type_t sig_alg,
10256 mbedtls_md_type_t md_alg )
10257{
10258 switch( sig_alg )
10259 {
10260 case MBEDTLS_PK_RSA:
10261 if( set->rsa == MBEDTLS_MD_NONE )
10262 set->rsa = md_alg;
10263 break;
10264
10265 case MBEDTLS_PK_ECDSA:
10266 if( set->ecdsa == MBEDTLS_MD_NONE )
10267 set->ecdsa = md_alg;
10268 break;
10269
10270 default:
10271 break;
10272 }
10273}
10274
10275/* Allow exactly one hash algorithm for each signature. */
10276void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10277 mbedtls_md_type_t md_alg )
10278{
10279 set->rsa = md_alg;
10280 set->ecdsa = md_alg;
10281}
10282
10283#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10284 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10285
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010286/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010287 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010290{
10291 switch( hash )
10292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293#if defined(MBEDTLS_MD5_C)
10294 case MBEDTLS_SSL_HASH_MD5:
10295 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297#if defined(MBEDTLS_SHA1_C)
10298 case MBEDTLS_SSL_HASH_SHA1:
10299 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010301#if defined(MBEDTLS_SHA256_C)
10302 case MBEDTLS_SSL_HASH_SHA224:
10303 return( MBEDTLS_MD_SHA224 );
10304 case MBEDTLS_SSL_HASH_SHA256:
10305 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307#if defined(MBEDTLS_SHA512_C)
10308 case MBEDTLS_SSL_HASH_SHA384:
10309 return( MBEDTLS_MD_SHA384 );
10310 case MBEDTLS_SSL_HASH_SHA512:
10311 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010312#endif
10313 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010315 }
10316}
10317
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010318/*
10319 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10320 */
10321unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10322{
10323 switch( md )
10324 {
10325#if defined(MBEDTLS_MD5_C)
10326 case MBEDTLS_MD_MD5:
10327 return( MBEDTLS_SSL_HASH_MD5 );
10328#endif
10329#if defined(MBEDTLS_SHA1_C)
10330 case MBEDTLS_MD_SHA1:
10331 return( MBEDTLS_SSL_HASH_SHA1 );
10332#endif
10333#if defined(MBEDTLS_SHA256_C)
10334 case MBEDTLS_MD_SHA224:
10335 return( MBEDTLS_SSL_HASH_SHA224 );
10336 case MBEDTLS_MD_SHA256:
10337 return( MBEDTLS_SSL_HASH_SHA256 );
10338#endif
10339#if defined(MBEDTLS_SHA512_C)
10340 case MBEDTLS_MD_SHA384:
10341 return( MBEDTLS_SSL_HASH_SHA384 );
10342 case MBEDTLS_MD_SHA512:
10343 return( MBEDTLS_SSL_HASH_SHA512 );
10344#endif
10345 default:
10346 return( MBEDTLS_SSL_HASH_NONE );
10347 }
10348}
10349
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010350#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010351/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010352 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010353 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010354 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010355int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010358
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010359 if( ssl->conf->curve_list == NULL )
10360 return( -1 );
10361
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010362 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010363 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010364 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010365
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010366 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010367}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010368#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010369
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010370#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010371/*
10372 * Check if a hash proposed by the peer is in our list.
10373 * Return 0 if we're willing to use it, -1 otherwise.
10374 */
10375int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10376 mbedtls_md_type_t md )
10377{
10378 const int *cur;
10379
10380 if( ssl->conf->sig_hashes == NULL )
10381 return( -1 );
10382
10383 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10384 if( *cur == (int) md )
10385 return( 0 );
10386
10387 return( -1 );
10388}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010389#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391#if defined(MBEDTLS_X509_CRT_PARSE_C)
10392int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10393 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010394 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010395 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010396{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010397 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010399 int usage = 0;
10400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010402 const char *ext_oid;
10403 size_t ext_len;
10404#endif
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10407 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010408 ((void) cert);
10409 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010410 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010411#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10414 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010415 {
10416 /* Server part of the key exchange */
10417 switch( ciphersuite->key_exchange )
10418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 case MBEDTLS_KEY_EXCHANGE_RSA:
10420 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010421 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010422 break;
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10425 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10426 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10427 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010428 break;
10429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10431 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010432 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010433 break;
10434
10435 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 case MBEDTLS_KEY_EXCHANGE_NONE:
10437 case MBEDTLS_KEY_EXCHANGE_PSK:
10438 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10439 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010440 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010441 usage = 0;
10442 }
10443 }
10444 else
10445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10447 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010448 }
10449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010451 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010452 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010453 ret = -1;
10454 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010455#else
10456 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10460 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10463 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010464 }
10465 else
10466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10468 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010469 }
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010472 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010473 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010474 ret = -1;
10475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010477
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010478 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010481
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010482/*
10483 * Convert version numbers to/from wire format
10484 * and, for DTLS, to/from TLS equivalent.
10485 *
10486 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010487 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010488 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10489 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010492 unsigned char ver[2] )
10493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494#if defined(MBEDTLS_SSL_PROTO_DTLS)
10495 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010498 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10499
10500 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10501 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10502 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010503 else
10504#else
10505 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010506#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010507 {
10508 ver[0] = (unsigned char) major;
10509 ver[1] = (unsigned char) minor;
10510 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010511}
10512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010514 const unsigned char ver[2] )
10515{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516#if defined(MBEDTLS_SSL_PROTO_DTLS)
10517 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010518 {
10519 *major = 255 - ver[0] + 2;
10520 *minor = 255 - ver[1] + 1;
10521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010523 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10524 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010525 else
10526#else
10527 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010528#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010529 {
10530 *major = ver[0];
10531 *minor = ver[1];
10532 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010533}
10534
Simon Butcher99000142016-10-13 17:21:01 +010010535int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10536{
10537#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10538 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10539 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10540
10541 switch( md )
10542 {
10543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10544#if defined(MBEDTLS_MD5_C)
10545 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010546 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010547#endif
10548#if defined(MBEDTLS_SHA1_C)
10549 case MBEDTLS_SSL_HASH_SHA1:
10550 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10551 break;
10552#endif
10553#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10554#if defined(MBEDTLS_SHA512_C)
10555 case MBEDTLS_SSL_HASH_SHA384:
10556 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10557 break;
10558#endif
10559#if defined(MBEDTLS_SHA256_C)
10560 case MBEDTLS_SSL_HASH_SHA256:
10561 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10562 break;
10563#endif
10564 default:
10565 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10566 }
10567
10568 return 0;
10569#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10570 (void) ssl;
10571 (void) md;
10572
10573 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10574#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10575}
10576
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010577#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10578 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10579int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10580 unsigned char *output,
10581 unsigned char *data, size_t data_len )
10582{
10583 int ret = 0;
10584 mbedtls_md5_context mbedtls_md5;
10585 mbedtls_sha1_context mbedtls_sha1;
10586
10587 mbedtls_md5_init( &mbedtls_md5 );
10588 mbedtls_sha1_init( &mbedtls_sha1 );
10589
10590 /*
10591 * digitally-signed struct {
10592 * opaque md5_hash[16];
10593 * opaque sha_hash[20];
10594 * };
10595 *
10596 * md5_hash
10597 * MD5(ClientHello.random + ServerHello.random
10598 * + ServerParams);
10599 * sha_hash
10600 * SHA(ClientHello.random + ServerHello.random
10601 * + ServerParams);
10602 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010603 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010604 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010606 goto exit;
10607 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010608 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010609 ssl->handshake->randbytes, 64 ) ) != 0 )
10610 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010612 goto exit;
10613 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010614 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010615 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010617 goto exit;
10618 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010619 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010620 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010622 goto exit;
10623 }
10624
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010625 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010626 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010628 goto exit;
10629 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010630 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010631 ssl->handshake->randbytes, 64 ) ) != 0 )
10632 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010634 goto exit;
10635 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010636 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010637 data_len ) ) != 0 )
10638 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010640 goto exit;
10641 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010642 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010643 output + 16 ) ) != 0 )
10644 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010646 goto exit;
10647 }
10648
10649exit:
10650 mbedtls_md5_free( &mbedtls_md5 );
10651 mbedtls_sha1_free( &mbedtls_sha1 );
10652
10653 if( ret != 0 )
10654 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10655 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10656
10657 return( ret );
10658
10659}
10660#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10661 MBEDTLS_SSL_PROTO_TLS1_1 */
10662
10663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10664 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010665
10666#if defined(MBEDTLS_USE_PSA_CRYPTO)
10667int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10668 unsigned char *hash, size_t *hashlen,
10669 unsigned char *data, size_t data_len,
10670 mbedtls_md_type_t md_alg )
10671{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010672 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010673 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010674 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10675
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010677
10678 if( ( status = psa_hash_setup( &hash_operation,
10679 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010680 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010681 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010682 goto exit;
10683 }
10684
Andrzej Kurek814feff2019-01-14 04:35:19 -050010685 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10686 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010687 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010688 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010689 goto exit;
10690 }
10691
Andrzej Kurek814feff2019-01-14 04:35:19 -050010692 if( ( status = psa_hash_update( &hash_operation,
10693 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010694 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010695 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010696 goto exit;
10697 }
10698
Andrzej Kurek814feff2019-01-14 04:35:19 -050010699 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10700 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010701 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010702 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010703 goto exit;
10704 }
10705
10706exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010707 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010708 {
10709 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10710 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010711 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010712 {
10713 case PSA_ERROR_NOT_SUPPORTED:
10714 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010715 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010716 case PSA_ERROR_BUFFER_TOO_SMALL:
10717 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10718 case PSA_ERROR_INSUFFICIENT_MEMORY:
10719 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10720 default:
10721 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10722 }
10723 }
10724 return( 0 );
10725}
10726
10727#else
10728
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010729int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010730 unsigned char *hash, size_t *hashlen,
10731 unsigned char *data, size_t data_len,
10732 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010733{
10734 int ret = 0;
10735 mbedtls_md_context_t ctx;
10736 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010737 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010738
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010740
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010741 mbedtls_md_init( &ctx );
10742
10743 /*
10744 * digitally-signed struct {
10745 * opaque client_random[32];
10746 * opaque server_random[32];
10747 * ServerDHParams params;
10748 * };
10749 */
10750 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10751 {
10752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10753 goto exit;
10754 }
10755 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10756 {
10757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10758 goto exit;
10759 }
10760 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10761 {
10762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10763 goto exit;
10764 }
10765 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10766 {
10767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10768 goto exit;
10769 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010770 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010771 {
10772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10773 goto exit;
10774 }
10775
10776exit:
10777 mbedtls_md_free( &ctx );
10778
10779 if( ret != 0 )
10780 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10781 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10782
10783 return( ret );
10784}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010785#endif /* MBEDTLS_USE_PSA_CRYPTO */
10786
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010787#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10788 MBEDTLS_SSL_PROTO_TLS1_2 */
10789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790#endif /* MBEDTLS_SSL_TLS_C */