blob: b5e850ada5abff6a86b4d8af6381b64d45c77ddf [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
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 /*
992 * Determine the appropriate key, IV and MAC length.
993 */
Paul Bakker68884e32013-01-07 18:20:04 +0100994
Hanno Becker88aaf652017-12-27 08:17:40 +0000995 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200996
Hanno Becker8031d062018-01-03 15:32:31 +0000997#if defined(MBEDTLS_GCM_C) || \
998 defined(MBEDTLS_CCM_C) || \
999 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001 cipher_info->mode == MBEDTLS_MODE_CCM ||
1002 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001004 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001006 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001007 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001008 transform->taglen =
1009 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001010
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001011 /* All modes haves 96-bit IVs;
1012 * GCM and CCM has 4 implicit and 8 explicit bytes
1013 * ChachaPoly has all 12 bytes implicit
1014 */
Paul Bakker68884e32013-01-07 18:20:04 +01001015 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001016 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1017 transform->fixed_ivlen = 12;
1018 else
1019 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001020
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001021 /* Minimum length of encrypted record */
1022 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025 else
Hanno Becker8031d062018-01-03 15:32:31 +00001026#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1027#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1028 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1029 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001030 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1033 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001036 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001040 mac_key_len = mbedtls_md_get_size( md_info );
1041 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001044 /*
1045 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1046 * (rfc 6066 page 13 or rfc 2104 section 4),
1047 * so we only need to adjust the length here.
1048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001052
1053#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1054 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001055 * HMAC implementation which also truncates the key
1056 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001057 mac_key_len = transform->maclen;
1058#endif
1059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001061
1062 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001063 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001067 transform->minlen = transform->maclen;
1068 else
Paul Bakker68884e32013-01-07 18:20:04 +01001069 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 /*
1071 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001072 * 1. if EtM is in use: one block plus MAC
1073 * otherwise: * first multiple of blocklen greater than maclen
1074 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1077 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001078 {
1079 transform->minlen = transform->maclen
1080 + cipher_info->block_size;
1081 }
1082 else
1083#endif
1084 {
1085 transform->minlen = transform->maclen
1086 + cipher_info->block_size
1087 - transform->maclen % cipher_info->block_size;
1088 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1091 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1092 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001093 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001094 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1097 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1098 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001099 {
1100 transform->minlen += transform->ivlen;
1101 }
1102 else
1103#endif
1104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001106 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1107 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001108 }
Paul Bakker68884e32013-01-07 18:20:04 +01001109 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
Hanno Becker8031d062018-01-03 15:32:31 +00001111 else
1112#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1113 {
1114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1115 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Hanno Becker88aaf652017-12-27 08:17:40 +00001118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1119 (unsigned) keylen,
1120 (unsigned) transform->minlen,
1121 (unsigned) transform->ivlen,
1122 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
1124 /*
1125 * Finally setup the cipher contexts, IVs and MAC secrets.
1126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001128 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001130 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001131 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Paul Bakker68884e32013-01-07 18:20:04 +01001133 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001134 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001136 /*
1137 * This is not used in TLS v1.1.
1138 */
Paul Bakker48916f92012-09-16 19:57:18 +00001139 iv_copy_len = ( transform->fixed_ivlen ) ?
1140 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001141 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1142 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001143 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
1145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_SSL_CLI_C */
1147#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001148 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001150 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001151 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Hanno Becker81c7b182017-11-09 18:39:33 +00001153 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001154 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001156 /*
1157 * This is not used in TLS v1.1.
1158 */
Paul Bakker48916f92012-09-16 19:57:18 +00001159 iv_copy_len = ( transform->fixed_ivlen ) ?
1160 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001161 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1162 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001163 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001165 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001169 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1170 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Hanno Beckerd56ed242018-01-03 15:32:51 +00001173#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_SSL_PROTO_SSL3)
1175 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001176 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001177 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001180 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1181 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001182 }
1183
Hanno Becker81c7b182017-11-09 18:39:33 +00001184 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1185 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001186 }
1187 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1189#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1190 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1191 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001192 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001193 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1194 For AEAD-based ciphersuites, there is nothing to do here. */
1195 if( mac_key_len != 0 )
1196 {
1197 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1198 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1199 }
Paul Bakker68884e32013-01-07 18:20:04 +01001200 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001201 else
1202#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001205 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1206 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001207 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001208#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1211 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001212 {
1213 int ret = 0;
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001216
Hanno Becker88aaf652017-12-27 08:17:40 +00001217 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001218 transform->iv_enc, transform->iv_dec,
1219 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001220 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001221 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001224 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1225 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001226 }
1227 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001228#else
1229 ((void) mac_dec);
1230 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001232
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001233#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1234 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001235 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001236 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1237 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001238 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001239 iv_copy_len );
1240 }
1241#endif
1242
Hanno Beckerf704bef2018-11-16 15:21:18 +00001243#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001244
1245 /* Only use PSA-based ciphers for TLS-1.2.
1246 * That's relevant at least for TLS-1.0, where
1247 * we assume that mbedtls_cipher_crypt() updates
1248 * the structure field for the IV, which the PSA-based
1249 * implementation currently doesn't. */
1250#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001252 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001253 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001254 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001255 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1256 {
1257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001258 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001259 }
1260
1261 if( ret == 0 )
1262 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001264 psa_fallthrough = 0;
1265 }
1266 else
1267 {
1268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1269 psa_fallthrough = 1;
1270 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001271 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001273 psa_fallthrough = 1;
1274#else
1275 psa_fallthrough = 1;
1276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001277
Hanno Beckercb1cc802018-11-17 22:27:38 +00001278 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001279#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001280 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001281 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001284 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001285 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001286
Hanno Beckerf704bef2018-11-16 15:21:18 +00001287#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001288 /* Only use PSA-based ciphers for TLS-1.2.
1289 * That's relevant at least for TLS-1.0, where
1290 * we assume that mbedtls_cipher_crypt() updates
1291 * the structure field for the IV, which the PSA-based
1292 * implementation currently doesn't. */
1293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1294 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001295 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001296 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001297 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001298 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1299 {
1300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001301 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001302 }
1303
1304 if( ret == 0 )
1305 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001307 psa_fallthrough = 0;
1308 }
1309 else
1310 {
1311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1312 psa_fallthrough = 1;
1313 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001314 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001315 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001316 psa_fallthrough = 1;
1317#else
1318 psa_fallthrough = 1;
1319#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001320
Hanno Beckercb1cc802018-11-17 22:27:38 +00001321 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001322#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001323 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001324 cipher_info ) ) != 0 )
1325 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001327 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001328 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001331 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001335 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001339 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001343 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001344 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_CIPHER_MODE_CBC)
1347 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1350 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001353 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001354 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1357 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001360 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001367 // Initialize compression
1368 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001370 {
Paul Bakker16770332013-10-11 09:59:44 +02001371 if( ssl->compress_buf == NULL )
1372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001374 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001375 if( ssl->compress_buf == NULL )
1376 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001378 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001379 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1380 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001381 }
1382 }
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001385
Paul Bakker48916f92012-09-16 19:57:18 +00001386 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1387 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001388
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001389 if( deflateInit( &transform->ctx_deflate,
1390 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001391 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001394 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1395 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001396 }
1397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001401end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001402 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1403 mbedtls_platform_zeroize( handshake->randbytes,
1404 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001405 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406}
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_PROTO_SSL3)
1409void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001410{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001411 mbedtls_md5_context md5;
1412 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 unsigned char pad_1[48];
1414 unsigned char pad_2[48];
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001418 mbedtls_md5_init( &md5 );
1419 mbedtls_sha1_init( &sha1 );
1420
1421 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1422 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Paul Bakker380da532012-04-18 16:10:25 +00001424 memset( pad_1, 0x36, 48 );
1425 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1428 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1429 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001431 mbedtls_md5_starts_ret( &md5 );
1432 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1433 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1434 mbedtls_md5_update_ret( &md5, hash, 16 );
1435 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001437 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1438 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1439 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001441 mbedtls_sha1_starts_ret( &sha1 );
1442 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1443 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1444 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1458void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001459{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001460 mbedtls_md5_context md5;
1461 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001465 mbedtls_md5_init( &md5 );
1466 mbedtls_sha1_init( &sha1 );
1467
1468 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1469 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Andrzej Kurekeb342242019-01-29 09:14:33 -05001471 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001472 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001476
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001477 mbedtls_md5_free( &md5 );
1478 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001479
Paul Bakker380da532012-04-18 16:10:25 +00001480 return;
1481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1485#if defined(MBEDTLS_SHA256_C)
1486void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001487{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001488#if defined(MBEDTLS_USE_PSA_CRYPTO)
1489 size_t hash_size;
1490 psa_status_t status;
1491 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1492
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1494 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1495 if( status != PSA_SUCCESS )
1496 {
1497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1498 return;
1499 }
1500
1501 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1502 if( status != PSA_SUCCESS )
1503 {
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1505 return;
1506 }
1507 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1509#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha256_init( &sha256 );
1513
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001516 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001521
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001522 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001523#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001524 return;
1525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SHA512_C)
1529void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001530{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001531#if defined(MBEDTLS_USE_PSA_CRYPTO)
1532 size_t hash_size;
1533 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001534 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001535
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001537 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001538 if( status != PSA_SUCCESS )
1539 {
1540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1541 return;
1542 }
1543
Andrzej Kurek972fba52019-01-30 03:29:12 -05001544 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001545 if( status != PSA_SUCCESS )
1546 {
1547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1548 return;
1549 }
1550 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1552#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001553 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001554
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001555 mbedtls_sha512_init( &sha512 );
1556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001558
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001559 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001560 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001564
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001565 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001566#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 return;
1568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_SHA512_C */
1570#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1573int 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 +02001574{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001575 unsigned char *p = ssl->handshake->premaster;
1576 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001577 const unsigned char *psk = ssl->conf->psk;
1578 size_t psk_len = ssl->conf->psk_len;
1579
1580 /* If the psk callback was called, use its result */
1581 if( ssl->handshake->psk != NULL )
1582 {
1583 psk = ssl->handshake->psk;
1584 psk_len = ssl->handshake->psk_len;
1585 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001586
1587 /*
1588 * PMS = struct {
1589 * opaque other_secret<0..2^16-1>;
1590 * opaque psk<0..2^16-1>;
1591 * };
1592 * with "other_secret" depending on the particular key exchange
1593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1595 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001597 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001600 *(p++) = (unsigned char)( psk_len >> 8 );
1601 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001602
1603 if( end < p || (size_t)( end - p ) < psk_len )
1604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1605
1606 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001607 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 }
1609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1611#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1612 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001613 {
1614 /*
1615 * other_secret already set by the ClientKeyExchange message,
1616 * and is 48 bytes long
1617 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001618 if( end - p < 2 )
1619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1620
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001621 *p++ = 0;
1622 *p++ = 48;
1623 p += 48;
1624 }
1625 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1627#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1628 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001630 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001631 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001633 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001635 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001636 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639 return( ret );
1640 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001641 *(p++) = (unsigned char)( len >> 8 );
1642 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001643 p += len;
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001646 }
1647 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1649#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1650 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001651 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001652 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001653 size_t zlen;
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001656 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001657 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001660 return( ret );
1661 }
1662
1663 *(p++) = (unsigned char)( zlen >> 8 );
1664 *(p++) = (unsigned char)( zlen );
1665 p += zlen;
1666
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001667 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1668 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001669 }
1670 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001675 }
1676
1677 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001678 if( end - p < 2 )
1679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001680
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001681 *(p++) = (unsigned char)( psk_len >> 8 );
1682 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001683
1684 if( end < p || (size_t)( end - p ) < psk_len )
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001687 memcpy( p, psk, psk_len );
1688 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001689
1690 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1691
1692 return( 0 );
1693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697/*
1698 * SSLv3.0 MAC functions
1699 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001700#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001701static void ssl_mac( mbedtls_md_context_t *md_ctx,
1702 const unsigned char *secret,
1703 const unsigned char *buf, size_t len,
1704 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001705 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706{
1707 unsigned char header[11];
1708 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001709 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1711 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001712
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001713 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001715 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001716 else
Paul Bakker68884e32013-01-07 18:20:04 +01001717 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
1719 memcpy( header, ctr, 8 );
1720 header[ 8] = (unsigned char) type;
1721 header[ 9] = (unsigned char)( len >> 8 );
1722 header[10] = (unsigned char)( len );
1723
Paul Bakker68884e32013-01-07 18:20:04 +01001724 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_md_starts( md_ctx );
1726 mbedtls_md_update( md_ctx, secret, md_size );
1727 mbedtls_md_update( md_ctx, padding, padlen );
1728 mbedtls_md_update( md_ctx, header, 11 );
1729 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001730 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
Paul Bakker68884e32013-01-07 18:20:04 +01001732 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_md_starts( md_ctx );
1734 mbedtls_md_update( md_ctx, secret, md_size );
1735 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001736 mbedtls_md_update( md_ctx, out, md_size );
1737 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001740
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001741/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001742 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001743#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001744 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1745 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1746 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1747/* This function makes sure every byte in the memory region is accessed
1748 * (in ascending addresses order) */
1749static void ssl_read_memory( unsigned char *p, size_t len )
1750{
1751 unsigned char acc = 0;
1752 volatile unsigned char force;
1753
1754 for( ; len != 0; p++, len-- )
1755 acc ^= *p;
1756
1757 force = acc;
1758 (void) force;
1759}
1760#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1761
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001762/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001764 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001765
1766static void ssl_extract_add_data_from_record( unsigned char* add_data,
1767 mbedtls_record *rec )
1768{
1769 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1770 add_data[8] = rec->type;
1771 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1772 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1773 add_data[12] = rec->data_len & 0xFF;
1774}
1775
Hanno Beckera18d1322018-01-03 14:27:32 +00001776int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1777 mbedtls_ssl_transform *transform,
1778 mbedtls_record *rec,
1779 int (*f_rng)(void *, unsigned char *, size_t),
1780 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001783 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001784 unsigned char * data;
1785 unsigned char add_data[13];
1786 size_t post_avail;
1787
1788 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001789#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001790 ((void) ssl);
1791#endif
1792
1793 /* The PRNG is used for dynamic IV generation that's used
1794 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1795#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1796 ( defined(MBEDTLS_AES_C) || \
1797 defined(MBEDTLS_ARIA_C) || \
1798 defined(MBEDTLS_CAMELLIA_C) ) && \
1799 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1800 ((void) f_rng);
1801 ((void) p_rng);
1802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001806 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001807 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1810 }
1811 if( rec == NULL ||
1812 rec->buf == NULL ||
1813 rec->buf_len < rec->data_offset ||
1814 rec->buf_len - rec->data_offset < rec->data_len )
1815 {
1816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001818 }
1819
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001820 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1821 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001823 data, rec->data_len );
1824
1825 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1826
1827 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1828 {
1829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1830 (unsigned) rec->data_len,
1831 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1833 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001834
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001836 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 */
Hanno Becker52344c22018-01-03 15:24:20 +00001838#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 if( mode == MBEDTLS_MODE_STREAM ||
1840 ( mode == MBEDTLS_MODE_CBC
1841#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001842 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001843#endif
1844 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001846 if( post_avail < transform->maclen )
1847 {
1848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1849 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1850 }
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001853 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001855 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001856 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1857 data, rec->data_len, rec->ctr, rec->type, mac );
1858 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001859 }
1860 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1863 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001864 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001865 {
Hanno Becker992b6872017-11-09 18:57:39 +00001866 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1867
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001868 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001869
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001870 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1871 sizeof( add_data ) );
1872 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1873 data, rec->data_len );
1874 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1875 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1876
1877 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001878 }
1879 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001880#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001884 }
1885
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001886 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1887 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001888
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001889 rec->data_len += transform->maclen;
1890 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001891 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001892 }
Hanno Becker52344c22018-01-03 15:24:20 +00001893#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001895 /*
1896 * Encrypt
1897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1899 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001901 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001902 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001904 "including %d bytes of padding",
1905 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001907 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1908 transform->iv_enc, transform->ivlen,
1909 data, rec->data_len,
1910 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001913 return( ret );
1914 }
1915
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001916 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 }
Paul Bakker68884e32013-01-07 18:20:04 +01001922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001924
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001925#if defined(MBEDTLS_GCM_C) || \
1926 defined(MBEDTLS_CCM_C) || \
1927 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001929 mode == MBEDTLS_MODE_CCM ||
1930 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001931 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001932 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001933 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001934 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001935
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001936 /* Check that there's space for both the authentication tag
1937 * and the explicit IV before and after the record content. */
1938 if( post_avail < transform->taglen ||
1939 rec->data_offset < explicit_iv_len )
1940 {
1941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1942 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1943 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001944
Paul Bakker68884e32013-01-07 18:20:04 +01001945 /*
1946 * Generate IV
1947 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001948 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1949 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001950 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001951 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001952 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1953 explicit_iv_len );
1954 /* Prefix record content with explicit IV. */
1955 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001956 }
1957 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1958 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001959 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001960 unsigned char i;
1961
1962 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1963
1964 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001965 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001966 }
1967 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001968 {
1969 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001972 }
1973
Hanno Becker1f10d762019-04-26 13:34:37 +01001974 ssl_extract_add_data_from_record( add_data, rec );
1975
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001976 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1977 iv, transform->ivlen );
1978 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001979 data - explicit_iv_len, explicit_iv_len );
1980 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1981 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001983 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001984 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001985
Paul Bakker68884e32013-01-07 18:20:04 +01001986 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001987 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001988 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001989
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001990 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001991 iv, transform->ivlen,
1992 add_data, 13, /* add data */
1993 data, rec->data_len, /* source */
1994 data, &rec->data_len, /* destination */
1995 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001998 return( ret );
1999 }
2000
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002001 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2002 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002003
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002004 rec->data_len += transform->taglen + explicit_iv_len;
2005 rec->data_offset -= explicit_iv_len;
2006 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2011#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002012 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002015 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002016 size_t padlen, i;
2017 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002019 /* Currently we're always using minimal padding
2020 * (up to 255 bytes would be allowed). */
2021 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2022 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 padlen = 0;
2024
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002025 /* Check there's enough space in the buffer for the padding. */
2026 if( post_avail < padlen + 1 )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2030 }
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002033 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002035 rec->data_len += padlen + 1;
2036 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002039 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002040 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2041 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002042 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002043 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002044 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002045 if( f_rng == NULL )
2046 {
2047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2049 }
2050
2051 if( rec->data_offset < transform->ivlen )
2052 {
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2054 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2055 }
2056
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002057 /*
2058 * Generate IV
2059 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002060 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002061 if( ret != 0 )
2062 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002063
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002064 memcpy( data - transform->ivlen, transform->iv_enc,
2065 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002066
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002071 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002073 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002075 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2076 transform->iv_enc,
2077 transform->ivlen,
2078 data, rec->data_len,
2079 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002082 return( ret );
2083 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002084
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2088 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002089 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002093 {
2094 /*
2095 * Save IV in SSL3 and TLS1
2096 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002097 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2098 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002100 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002101#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 {
2103 data -= transform->ivlen;
2104 rec->data_offset -= transform->ivlen;
2105 rec->data_len += transform->ivlen;
2106 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002109 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002110 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002111 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2112
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002113 /*
2114 * MAC(MAC_write_key, seq_num +
2115 * TLSCipherText.type +
2116 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002117 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002118 * IV + // except for TLS 1.0
2119 * ENC(content + padding + padding_length));
2120 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002121
2122 if( post_avail < transform->maclen)
2123 {
2124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2125 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2126 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002127
Hanno Becker1f10d762019-04-26 13:34:37 +01002128 ssl_extract_add_data_from_record( add_data, rec );
2129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002131 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2132 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2135 sizeof( add_data ) );
2136 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2137 data, rec->data_len );
2138 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2139 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002140
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002142
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 rec->data_len += transform->maclen;
2144 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002145 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002151 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002157 /* Make extra sure authentication was performed, exactly once */
2158 if( auth_done != 1 )
2159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002162 }
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
2166 return( 0 );
2167}
2168
Hanno Beckera18d1322018-01-03 14:27:32 +00002169int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2170 mbedtls_ssl_transform *transform,
2171 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002173 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002175 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002176#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002177 size_t padlen = 0, correct = 1;
2178#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002179 unsigned char* data;
2180 unsigned char add_data[13];
2181
Hanno Beckera18d1322018-01-03 14:27:32 +00002182#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002183 ((void) ssl);
2184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002187 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002188 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2191 }
2192 if( rec == NULL ||
2193 rec->buf == NULL ||
2194 rec->buf_len < rec->data_offset ||
2195 rec->buf_len - rec->data_offset < rec->data_len )
2196 {
2197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002199 }
2200
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002201 data = rec->buf + rec->data_offset;
2202 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2205 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002206 {
2207 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002208 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2209 transform->iv_dec,
2210 transform->ivlen,
2211 data, rec->data_len,
2212 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002215 return( ret );
2216 }
2217
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002218 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002222 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 }
Paul Bakker68884e32013-01-07 18:20:04 +01002224 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002226#if defined(MBEDTLS_GCM_C) || \
2227 defined(MBEDTLS_CCM_C) || \
2228 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002230 mode == MBEDTLS_MODE_CCM ||
2231 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002232 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002233 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002234 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002235
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002236 /*
2237 * Compute and update sizes
2238 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002239 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002242 "+ taglen (%d)", rec->data_len,
2243 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002245 }
Paul Bakker68884e32013-01-07 18:20:04 +01002246
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002247 /*
2248 * Prepare IV
2249 */
2250 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2251 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002252 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002253 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002254 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002255
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002256 }
2257 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2258 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002259 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002260 unsigned char i;
2261
2262 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2263
2264 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002265 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002266 }
2267 else
2268 {
2269 /* Reminder if we ever add an AEAD mode with a different size */
2270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2272 }
2273
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002274 data += explicit_iv_len;
2275 rec->data_offset += explicit_iv_len;
2276 rec->data_len -= explicit_iv_len + transform->taglen;
2277
2278 ssl_extract_add_data_from_record( add_data, rec );
2279 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2280 add_data, 13 );
2281
2282 memcpy( transform->iv_dec + transform->fixed_ivlen,
2283 data - explicit_iv_len, explicit_iv_len );
2284
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002285 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002286 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002287 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002288
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002289
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002290 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002291 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002292 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002293 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2294 iv, transform->ivlen,
2295 add_data, 13,
2296 data, rec->data_len,
2297 data, &olen,
2298 data + rec->data_len,
2299 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2304 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002305
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002306 return( ret );
2307 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002308 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002309
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002310 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002314 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2318#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002319 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002322 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002323
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 /*
Paul Bakker45829992013-01-03 14:52:21 +01002325 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002328 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2329 {
2330 /* The ciphertext is prefixed with the CBC IV. */
2331 minlen += transform->ivlen;
2332 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002333#endif
Paul Bakker45829992013-01-03 14:52:21 +01002334
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002335 /* Size considerations:
2336 *
2337 * - The CBC cipher text must not be empty and hence
2338 * at least of size transform->ivlen.
2339 *
2340 * Together with the potential IV-prefix, this explains
2341 * the first of the two checks below.
2342 *
2343 * - The record must contain a MAC, either in plain or
2344 * encrypted, depending on whether Encrypt-then-MAC
2345 * is used or not.
2346 * - If it is, the message contains the IV-prefix,
2347 * the CBC ciphertext, and the MAC.
2348 * - If it is not, the padded plaintext, and hence
2349 * the CBC ciphertext, has at least length maclen + 1
2350 * because there is at least the padding length byte.
2351 *
2352 * As the CBC ciphertext is not empty, both cases give the
2353 * lower bound minlen + maclen + 1 on the record size, which
2354 * we test for in the second check below.
2355 */
2356 if( rec->data_len < minlen + transform->ivlen ||
2357 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002360 "+ 1 ) ( + expl IV )", rec->data_len,
2361 transform->ivlen,
2362 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002364 }
2365
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002366 /*
2367 * Authenticate before decrypt if enabled
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002370 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002371 {
Hanno Becker992b6872017-11-09 18:57:39 +00002372 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002375
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002376 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2377 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002378
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002379 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002380
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2382 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2383 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2384 data, rec->data_len );
2385 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2386 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002387
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2389 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002390 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002391 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002392
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002393 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2394 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002398 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002399 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002402
2403 /*
2404 * Check length sanity
2405 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002406 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002409 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002411 }
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002414 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002415 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002417 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419 /* This is safe because data_len >= minlen + maclen + 1 initially,
2420 * and at this point we have at most subtracted maclen (note that
2421 * minlen == transform->ivlen here). */
2422 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002424 data += transform->ivlen;
2425 rec->data_offset += transform->ivlen;
2426 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002430 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2431 transform->iv_dec, transform->ivlen,
2432 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002435 return( ret );
2436 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002437
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002438 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002442 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002445 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002446 {
2447 /*
2448 * Save IV in SSL3 and TLS1
2449 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002450 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2451 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002453#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002455 /* Safe since data_len >= minlen + maclen + 1, so after having
2456 * subtracted at most minlen and maclen up to this point,
2457 * data_len > 0. */
2458 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002459
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002460 if( auth_done == 1 )
2461 {
2462 correct *= ( rec->data_len >= padlen + 1 );
2463 padlen *= ( rec->data_len >= padlen + 1 );
2464 }
2465 else
Paul Bakker45829992013-01-03 14:52:21 +01002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002468 if( rec->data_len < transform->maclen + padlen + 1 )
2469 {
2470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2471 rec->data_len,
2472 transform->maclen,
2473 padlen + 1 ) );
2474 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002475#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002476
2477 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2478 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002481 padlen++;
2482
2483 /* Regardless of the validity of the padding,
2484 * we have data_len >= padlen here. */
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002487 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002489 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_SSL_DEBUG_ALL)
2492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002493 "should be no more than %d",
2494 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002495#endif
Paul Bakker45829992013-01-03 14:52:21 +01002496 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498 }
2499 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2501#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2502 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002503 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002505 /* The padding check involves a series of up to 256
2506 * consecutive memory reads at the end of the record
2507 * plaintext buffer. In order to hide the length and
2508 * validity of the padding, always perform exactly
2509 * `min(256,plaintext_len)` reads (but take into account
2510 * only the last `padlen` bytes for the padding check). */
2511 size_t pad_count = 0;
2512 size_t real_count = 0;
2513 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002514
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002515 /* Index of first padding byte; it has been ensured above
2516 * that the subtraction is safe. */
2517 size_t const padding_idx = rec->data_len - padlen;
2518 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2519 size_t const start_idx = rec->data_len - num_checks;
2520 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002521
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002522 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002523 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002524 real_count |= ( idx >= padding_idx );
2525 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002526 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002527 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002530 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002532#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002533 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2537 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002541 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002542
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 /* If the padding was found to be invalid, padlen == 0
2544 * and the subtraction is safe. If the padding was found valid,
2545 * padlen hasn't been changed and the previous assertion
2546 * data_len >= padlen still holds. */
2547 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002549 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002551 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002557#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002563 * Authenticate if not done yet.
2564 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 */
Hanno Becker52344c22018-01-03 15:24:20 +00002566#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002568 {
Hanno Becker992b6872017-11-09 18:57:39 +00002569 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002570
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 /* If the initial value of padlen was such that
2572 * data_len < maclen + padlen + 1, then padlen
2573 * got reset to 1, and the initial check
2574 * data_len >= minlen + maclen + 1
2575 * guarantees that at this point we still
2576 * have at least data_len >= maclen.
2577 *
2578 * If the initial value of padlen was such that
2579 * data_len >= maclen + padlen + 1, then we have
2580 * subtracted either padlen + 1 (if the padding was correct)
2581 * or 0 (if the padding was incorrect) since then,
2582 * hence data_len >= maclen in any case.
2583 */
2584 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002586 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002590 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 ssl_mac( &transform->md_ctx_dec,
2592 transform->mac_dec,
2593 data, rec->data_len,
2594 rec->ctr, rec->type,
2595 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002596 }
2597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2599#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2600 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002601 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002602 {
2603 /*
2604 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002605 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002606 *
2607 * Known timing attacks:
2608 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2609 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002610 * To compensate for different timings for the MAC calculation
2611 * depending on how much padding was removed (which is determined
2612 * by padlen), process extra_run more blocks through the hash
2613 * function.
2614 *
2615 * The formula in the paper is
2616 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2617 * where L1 is the size of the header plus the decrypted message
2618 * plus CBC padding and L2 is the size of the header plus the
2619 * decrypted message. This is for an underlying hash function
2620 * with 64-byte blocks.
2621 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2622 * correctly. We round down instead of up, so -56 is the correct
2623 * value for our calculations instead of -55.
2624 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002625 * Repeat the formula rather than defining a block_size variable.
2626 * This avoids requiring division by a variable at runtime
2627 * (which would be marginally less efficient and would require
2628 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002629 */
2630 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002632
2633 /*
2634 * The next two sizes are the minimum and maximum values of
2635 * in_msglen over all padlen values.
2636 *
2637 * They're independent of padlen, since we previously did
2638 * in_msglen -= padlen.
2639 *
2640 * Note that max_len + maclen is never more than the buffer
2641 * length, as we previously did in_msglen -= maclen too.
2642 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002643 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002644 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2645
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002646 memset( tmp, 0, sizeof( tmp ) );
2647
2648 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002649 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002650#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2651 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002652 case MBEDTLS_MD_MD5:
2653 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002654 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002655 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2657 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002658 break;
2659#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002660#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002661 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002662 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002663 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2664 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002665 break;
2666#endif
2667 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2670 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002671
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002672 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002673
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2675 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2676 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002677 /* Make sure we access everything even when padlen > 0. This
2678 * makes the synchronisation requirements for just-in-time
2679 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680 ssl_read_memory( data + rec->data_len, padlen );
2681 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002682
2683 /* Call mbedtls_md_process at least once due to cache attacks
2684 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002685 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002686 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002687
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002689
2690 /* Make sure we access all the memory that could contain the MAC,
2691 * before we check it in the next code block. This makes the
2692 * synchronisation requirements for just-in-time Prime+Probe
2693 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 ssl_read_memory( data + min_len,
2695 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002696 }
2697 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2699 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002705#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2707 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002710 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2711 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_DEBUG_ALL)
2714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002715#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002716 correct = 0;
2717 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002718 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002720
2721 /*
2722 * Finally check the correct flag
2723 */
2724 if( correct == 0 )
2725 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002726#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002727
2728 /* Make extra sure authentication was performed, exactly once */
2729 if( auth_done != 1 )
2730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
2737 return( 0 );
2738}
2739
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002740#undef MAC_NONE
2741#undef MAC_PLAINTEXT
2742#undef MAC_CIPHERTEXT
2743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745/*
2746 * Compression/decompression functions
2747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749{
2750 int ret;
2751 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002752 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002754 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002758 if( len_pre == 0 )
2759 return( 0 );
2760
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761 memcpy( msg_pre, ssl->out_msg, len_pre );
2762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002764 ssl->out_msglen ) );
2765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767 ssl->out_msg, ssl->out_msglen );
2768
Paul Bakker48916f92012-09-16 19:57:18 +00002769 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2770 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2771 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002772 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773
Paul Bakker48916f92012-09-16 19:57:18 +00002774 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775 if( ret != Z_OK )
2776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2778 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779 }
2780
Angus Grattond8213d02016-05-25 20:56:48 +10002781 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002782 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002785 ssl->out_msglen ) );
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788 ssl->out_msg, ssl->out_msglen );
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002791
2792 return( 0 );
2793}
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002796{
2797 int ret;
2798 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002799 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002800 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002801 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002805 if( len_pre == 0 )
2806 return( 0 );
2807
Paul Bakker2770fbd2012-07-03 13:30:23 +00002808 memcpy( msg_pre, ssl->in_msg, len_pre );
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002811 ssl->in_msglen ) );
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814 ssl->in_msg, ssl->in_msglen );
2815
Paul Bakker48916f92012-09-16 19:57:18 +00002816 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2817 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2818 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002819 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002820 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002821
Paul Bakker48916f92012-09-16 19:57:18 +00002822 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002823 if( ret != Z_OK )
2824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2826 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002827 }
2828
Angus Grattond8213d02016-05-25 20:56:48 +10002829 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002830 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002833 ssl->in_msglen ) );
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002836 ssl->in_msg, ssl->in_msglen );
2837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002839
2840 return( 0 );
2841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2845static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_PROTO_DTLS)
2848static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002849{
2850 /* If renegotiation is not enforced, retransmit until we would reach max
2851 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002852 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002853 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002854 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002855 unsigned char doublings = 1;
2856
2857 while( ratio != 0 )
2858 {
2859 ++doublings;
2860 ratio >>= 1;
2861 }
2862
2863 if( ++ssl->renego_records_seen > doublings )
2864 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002866 return( 0 );
2867 }
2868 }
2869
2870 return( ssl_write_hello_request( ssl ) );
2871}
2872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002874
Paul Bakker5121ce52009-01-03 21:22:43 +00002875/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002876 * Fill the input message buffer by appending data to it.
2877 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002878 *
2879 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2880 * available (from this read and/or a previous one). Otherwise, an error code
2881 * is returned (possibly EOF or WANT_READ).
2882 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002883 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2884 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2885 * since we always read a whole datagram at once.
2886 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002887 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002888 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891{
Paul Bakker23986e52011-04-24 08:57:21 +00002892 int ret;
2893 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002897 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002900 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002902 }
2903
Angus Grattond8213d02016-05-25 20:56:48 +10002904 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002908 }
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002913 uint32_t timeout;
2914
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002915 /* Just to be sure */
2916 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2917 {
2918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2919 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2921 }
2922
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002923 /*
2924 * The point is, we need to always read a full datagram at once, so we
2925 * sometimes read more then requested, and handle the additional data.
2926 * It could be the rest of the current record (while fetching the
2927 * header) and/or some other records in the same datagram.
2928 */
2929
2930 /*
2931 * Move to the next record in the already read datagram if applicable
2932 */
2933 if( ssl->next_record_offset != 0 )
2934 {
2935 if( ssl->in_left < ssl->next_record_offset )
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002939 }
2940
2941 ssl->in_left -= ssl->next_record_offset;
2942
2943 if( ssl->in_left != 0 )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002946 ssl->next_record_offset ) );
2947 memmove( ssl->in_hdr,
2948 ssl->in_hdr + ssl->next_record_offset,
2949 ssl->in_left );
2950 }
2951
2952 ssl->next_record_offset = 0;
2953 }
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002957
2958 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002959 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002960 */
2961 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002964 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002965 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002966
2967 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002968 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002969 * are not at the beginning of a new record, the caller did something
2970 * wrong.
2971 */
2972 if( ssl->in_left != 0 )
2973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002976 }
2977
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002978 /*
2979 * Don't even try to read if time's out already.
2980 * This avoids by-passing the timer when repeatedly receiving messages
2981 * that will end up being dropped.
2982 */
2983 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002984 {
2985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002986 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002987 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002988 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002989 {
Angus Grattond8213d02016-05-25 20:56:48 +10002990 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002993 timeout = ssl->handshake->retransmit_timeout;
2994 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002995 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002998
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002999 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003000 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3001 timeout );
3002 else
3003 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003006
3007 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003009 }
3010
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003011 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003014 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003017 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003018 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003021 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003022 }
3023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003027 return( ret );
3028 }
3029
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003030 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003033 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003035 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003036 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003039 return( ret );
3040 }
3041
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003042 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003045 }
3046
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 if( ret < 0 )
3048 return( ret );
3049
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003050 ssl->in_left = ret;
3051 }
3052 else
3053#endif
3054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003056 ssl->in_left, nb_want ) );
3057
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003058 while( ssl->in_left < nb_want )
3059 {
3060 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003061
3062 if( ssl_check_timer( ssl ) != 0 )
3063 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3064 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003065 {
3066 if( ssl->f_recv_timeout != NULL )
3067 {
3068 ret = ssl->f_recv_timeout( ssl->p_bio,
3069 ssl->in_hdr + ssl->in_left, len,
3070 ssl->conf->read_timeout );
3071 }
3072 else
3073 {
3074 ret = ssl->f_recv( ssl->p_bio,
3075 ssl->in_hdr + ssl->in_left, len );
3076 }
3077 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003080 ssl->in_left, nb_want ) );
3081 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003082
3083 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003085
3086 if( ret < 0 )
3087 return( ret );
3088
mohammad160352aecb92018-03-28 23:41:40 -07003089 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003090 {
Darryl Green11999bb2018-03-13 15:22:58 +00003091 MBEDTLS_SSL_DEBUG_MSG( 1,
3092 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003093 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3095 }
3096
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003097 ssl->in_left += ret;
3098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 }
3100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102
3103 return( 0 );
3104}
3105
3106/*
3107 * Flush any data not yet written
3108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003110{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003111 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003112 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003116 if( ssl->f_send == NULL )
3117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003119 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003121 }
3122
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003123 /* Avoid incrementing counter if data is flushed */
3124 if( ssl->out_left == 0 )
3125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003127 return( 0 );
3128 }
3129
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 while( ssl->out_left > 0 )
3131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3133 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Hanno Becker2b1e3542018-08-06 11:19:13 +01003135 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003136 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
3140 if( ret <= 0 )
3141 return( ret );
3142
mohammad160352aecb92018-03-28 23:41:40 -07003143 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003144 {
Darryl Green11999bb2018-03-13 15:22:58 +00003145 MBEDTLS_SSL_DEBUG_MSG( 1,
3146 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003147 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3149 }
3150
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 ssl->out_left -= ret;
3152 }
3153
Hanno Becker2b1e3542018-08-06 11:19:13 +01003154#if defined(MBEDTLS_SSL_PROTO_DTLS)
3155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003156 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003157 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003158 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003159 else
3160#endif
3161 {
3162 ssl->out_hdr = ssl->out_buf + 8;
3163 }
3164 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
3168 return( 0 );
3169}
3170
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003171/*
3172 * Functions to handle the DTLS retransmission state machine
3173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003175/*
3176 * Append current handshake message to current outgoing flight
3177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3182 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3183 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184
3185 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003186 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003187 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003190 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191 }
3192
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003193 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003194 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003197 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003198 }
3199
3200 /* Copy current handshake message with headers */
3201 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3202 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003203 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003204 msg->next = NULL;
3205
3206 /* Append to the current flight */
3207 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003208 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003209 else
3210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003212 while( cur->next != NULL )
3213 cur = cur->next;
3214 cur->next = msg;
3215 }
3216
Hanno Becker3b235902018-08-06 09:54:53 +01003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003218 return( 0 );
3219}
3220
3221/*
3222 * Free the current flight of handshake messages
3223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 mbedtls_ssl_flight_item *cur = flight;
3227 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003228
3229 while( cur != NULL )
3230 {
3231 next = cur->next;
3232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 mbedtls_free( cur->p );
3234 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003235
3236 cur = next;
3237 }
3238}
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3241static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003242#endif
3243
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003244/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003245 * Swap transform_out and out_ctr with the alternative ones
3246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003250 unsigned char tmp_out_ctr[8];
3251
3252 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003255 return;
3256 }
3257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003259
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003260 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261 tmp_transform = ssl->transform_out;
3262 ssl->transform_out = ssl->handshake->alt_transform_out;
3263 ssl->handshake->alt_transform_out = tmp_transform;
3264
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003265 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003266 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3267 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003268 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003269
3270 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003271 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3274 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3279 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003280 }
3281 }
3282#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003283}
3284
3285/*
3286 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003287 */
3288int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3289{
3290 int ret = 0;
3291
3292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3293
3294 ret = mbedtls_ssl_flight_transmit( ssl );
3295
3296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3297
3298 return( ret );
3299}
3300
3301/*
3302 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303 *
3304 * Need to remember the current message in case flush_output returns
3305 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003306 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003308int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003310 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003314 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003316
3317 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003318 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003319 ssl_swap_epochs( ssl );
3320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003322 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323
3324 while( ssl->handshake->cur_msg != NULL )
3325 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003327 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003328
Hanno Beckere1dcb032018-08-17 16:47:58 +01003329 int const is_finished =
3330 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3331 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3332
Hanno Becker04da1892018-08-14 13:22:10 +01003333 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3334 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3335
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003336 /* Swap epochs before sending Finished: we can't do it after
3337 * sending ChangeCipherSpec, in case write returns WANT_READ.
3338 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003339 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003340 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003342 ssl_swap_epochs( ssl );
3343 }
3344
Hanno Becker67bc7c32018-08-06 11:33:50 +01003345 ret = ssl_get_remaining_payload_in_datagram( ssl );
3346 if( ret < 0 )
3347 return( ret );
3348 max_frag_len = (size_t) ret;
3349
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003350 /* CCS is copied as is, while HS messages may need fragmentation */
3351 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3352 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003353 if( max_frag_len == 0 )
3354 {
3355 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3356 return( ret );
3357
3358 continue;
3359 }
3360
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003361 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003362 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003363 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003365 /* Update position inside current message */
3366 ssl->handshake->cur_msg_p += cur->len;
3367 }
3368 else
3369 {
3370 const unsigned char * const p = ssl->handshake->cur_msg_p;
3371 const size_t hs_len = cur->len - 12;
3372 const size_t frag_off = p - ( cur->p + 12 );
3373 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003374 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375
Hanno Beckere1dcb032018-08-17 16:47:58 +01003376 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003377 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003378 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003379 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003380
Hanno Becker67bc7c32018-08-06 11:33:50 +01003381 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3382 return( ret );
3383
3384 continue;
3385 }
3386 max_hs_frag_len = max_frag_len - 12;
3387
3388 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3389 max_hs_frag_len : rem_len;
3390
3391 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003392 {
3393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003394 (unsigned) cur_hs_frag_len,
3395 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003396 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003397
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003398 /* Messages are stored with handshake headers as if not fragmented,
3399 * copy beginning of headers then fill fragmentation fields.
3400 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3401 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003402
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003403 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3404 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3405 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3406
Hanno Becker67bc7c32018-08-06 11:33:50 +01003407 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3408 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3409 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003410
3411 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3412
Hanno Becker3f7b9732018-08-28 09:53:25 +01003413 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003414 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3415 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003416 ssl->out_msgtype = cur->type;
3417
3418 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003419 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003420 }
3421
3422 /* If done with the current message move to the next one if any */
3423 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3424 {
3425 if( cur->next != NULL )
3426 {
3427 ssl->handshake->cur_msg = cur->next;
3428 ssl->handshake->cur_msg_p = cur->next->p + 12;
3429 }
3430 else
3431 {
3432 ssl->handshake->cur_msg = NULL;
3433 ssl->handshake->cur_msg_p = NULL;
3434 }
3435 }
3436
3437 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003438 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003441 return( ret );
3442 }
3443 }
3444
Hanno Becker67bc7c32018-08-06 11:33:50 +01003445 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3446 return( ret );
3447
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003448 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3450 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003451 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003454 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3455 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003456
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003458
3459 return( 0 );
3460}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461
3462/*
3463 * To be called when the last message of an incoming flight is received.
3464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003466{
3467 /* We won't need to resend that one any more */
3468 ssl_flight_free( ssl->handshake->flight );
3469 ssl->handshake->flight = NULL;
3470 ssl->handshake->cur_msg = NULL;
3471
3472 /* The next incoming flight will start with this msg_seq */
3473 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3474
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003475 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003476 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003477
Hanno Becker0271f962018-08-16 13:23:47 +01003478 /* Clear future message buffering structure. */
3479 ssl_buffering_free( ssl );
3480
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003481 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003482 ssl_set_timer( ssl, 0 );
3483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3485 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003488 }
3489 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003491}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003492
3493/*
3494 * To be called when the last message of an outgoing flight is send.
3495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003497{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003498 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003499 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3502 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003505 }
3506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510
Paul Bakker5121ce52009-01-03 21:22:43 +00003511/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003512 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
3515/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003516 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003517 *
3518 * - fill in handshake headers
3519 * - update handshake checksum
3520 * - DTLS: save message for resending
3521 * - then pass to the record layer
3522 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003523 * DTLS: except for HelloRequest, messages are only queued, and will only be
3524 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003525 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003526 * Inputs:
3527 * - ssl->out_msglen: 4 + actual handshake message len
3528 * (4 is the size of handshake headers for TLS)
3529 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3530 * - ssl->out_msg + 4: the handshake message body
3531 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003532 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003533 * - ssl->out_msglen: the length of the record contents
3534 * (including handshake headers but excluding record headers)
3535 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003536 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003537int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003538{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003539 int ret;
3540 const size_t hs_len = ssl->out_msglen - 4;
3541 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3544
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003545 /*
3546 * Sanity checks
3547 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003548 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003549 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3550 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003551 /* In SSLv3, the client might send a NoCertificate alert. */
3552#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3553 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3554 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3555 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3556#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3557 {
3558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3560 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003563 /* Whenever we send anything different from a
3564 * HelloRequest we should be in a handshake - double check. */
3565 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3566 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003567 ssl->handshake == NULL )
3568 {
3569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003582
Hanno Beckerb50a2532018-08-06 11:52:54 +01003583 /* Double-check that we did not exceed the bounds
3584 * of the outgoing record buffer.
3585 * This should never fail as the various message
3586 * writing functions must obey the bounds of the
3587 * outgoing record buffer, but better be safe.
3588 *
3589 * Note: We deliberately do not check for the MTU or MFL here.
3590 */
3591 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3592 {
3593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3594 "size %u, maximum %u",
3595 (unsigned) ssl->out_msglen,
3596 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3598 }
3599
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003600 /*
3601 * Fill handshake headers
3602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003605 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3606 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3607 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003609 /*
3610 * DTLS has additional fields in the Handshake layer,
3611 * between the length field and the actual payload:
3612 * uint16 message_seq;
3613 * uint24 fragment_offset;
3614 * uint24 fragment_length;
3615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003618 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003619 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003620 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003621 {
3622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3623 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003624 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003625 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3627 }
3628
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003629 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003630 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003631
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003632 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003633 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003634 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003635 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3636 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3637 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003638 }
3639 else
3640 {
3641 ssl->out_msg[4] = 0;
3642 ssl->out_msg[5] = 0;
3643 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003644
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003645 /* Handshake hashes are computed without fragmentation,
3646 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003647 memset( ssl->out_msg + 6, 0x00, 3 );
3648 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003651
Hanno Becker0207e532018-08-28 10:28:28 +01003652 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003653 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3654 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 }
3656
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003657 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003660 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3661 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003662 {
3663 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003666 return( ret );
3667 }
3668 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003669 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003670#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003671 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003672 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003673 {
3674 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3675 return( ret );
3676 }
3677 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003678
3679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3680
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003681 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682}
3683
3684/*
3685 * Record layer functions
3686 */
3687
3688/*
3689 * Write current record.
3690 *
3691 * Uses:
3692 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3693 * - ssl->out_msglen: length of the record content (excl headers)
3694 * - ssl->out_msg: record content
3695 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003696int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003697{
3698 int ret, done = 0;
3699 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003700 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701
3702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003705 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003707 {
3708 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003711 return( ret );
3712 }
3713
3714 len = ssl->out_msglen;
3715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3719 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 ret = mbedtls_ssl_hw_record_write( ssl );
3724 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3727 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003728 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003729
3730 if( ret == 0 )
3731 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003734 if( !done )
3735 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003736 unsigned i;
3737 size_t protected_record_size;
3738
Paul Bakker05ef8352012-05-08 09:17:57 +00003739 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003741 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003742
Hanno Becker19859472018-08-06 09:40:20 +01003743 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003744 ssl->out_len[0] = (unsigned char)( len >> 8 );
3745 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003746
Paul Bakker48916f92012-09-16 19:57:18 +00003747 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003748 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003749 mbedtls_record rec;
3750
3751 rec.buf = ssl->out_iv;
3752 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3753 ( ssl->out_iv - ssl->out_buf );
3754 rec.data_len = ssl->out_msglen;
3755 rec.data_offset = ssl->out_msg - rec.buf;
3756
3757 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3758 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3759 ssl->conf->transport, rec.ver );
3760 rec.type = ssl->out_msgtype;
3761
Hanno Beckera18d1322018-01-03 14:27:32 +00003762 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003763 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003766 return( ret );
3767 }
3768
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003769 if( rec.data_offset != 0 )
3770 {
3771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3773 }
3774
Hanno Becker78f839d2019-03-14 12:56:23 +00003775 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003776 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3777 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003778 }
3779
Hanno Becker2b1e3542018-08-06 11:19:13 +01003780 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3781
3782#if defined(MBEDTLS_SSL_PROTO_DTLS)
3783 /* In case of DTLS, double-check that we don't exceed
3784 * the remaining space in the datagram. */
3785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3786 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003787 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003788 if( ret < 0 )
3789 return( ret );
3790
3791 if( protected_record_size > (size_t) ret )
3792 {
3793 /* Should never happen */
3794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3795 }
3796 }
3797#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003800 "version = [%d:%d], msglen = %d",
3801 ssl->out_hdr[0], ssl->out_hdr[1],
3802 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003805 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003806
3807 ssl->out_left += protected_record_size;
3808 ssl->out_hdr += protected_record_size;
3809 ssl_update_out_pointers( ssl, ssl->transform_out );
3810
Hanno Becker04484622018-08-06 09:49:38 +01003811 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3812 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3813 break;
3814
3815 /* The loop goes to its end iff the counter is wrapping */
3816 if( i == ssl_ep_len( ssl ) )
3817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3819 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 }
3822
Hanno Becker67bc7c32018-08-06 11:33:50 +01003823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003824 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3825 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003826 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003827 size_t remaining;
3828 ret = ssl_get_remaining_payload_in_datagram( ssl );
3829 if( ret < 0 )
3830 {
3831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3832 ret );
3833 return( ret );
3834 }
3835
3836 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003837 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003838 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003840 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003841 else
3842 {
Hanno Becker513815a2018-08-20 11:56:09 +01003843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003844 }
3845 }
3846#endif /* MBEDTLS_SSL_PROTO_DTLS */
3847
3848 if( ( flush == SSL_FORCE_FLUSH ) &&
3849 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 return( ret );
3853 }
3854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
3857 return( 0 );
3858}
3859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003861
3862static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3863{
3864 if( ssl->in_msglen < ssl->in_hslen ||
3865 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3866 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3867 {
3868 return( 1 );
3869 }
3870 return( 0 );
3871}
Hanno Becker44650b72018-08-16 12:51:11 +01003872
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003873static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003874{
3875 return( ( ssl->in_msg[9] << 16 ) |
3876 ( ssl->in_msg[10] << 8 ) |
3877 ssl->in_msg[11] );
3878}
3879
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003880static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003881{
3882 return( ( ssl->in_msg[6] << 16 ) |
3883 ( ssl->in_msg[7] << 8 ) |
3884 ssl->in_msg[8] );
3885}
3886
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003887static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003888{
3889 uint32_t msg_len, frag_off, frag_len;
3890
3891 msg_len = ssl_get_hs_total_len( ssl );
3892 frag_off = ssl_get_hs_frag_off( ssl );
3893 frag_len = ssl_get_hs_frag_len( ssl );
3894
3895 if( frag_off > msg_len )
3896 return( -1 );
3897
3898 if( frag_len > msg_len - frag_off )
3899 return( -1 );
3900
3901 if( frag_len + 12 > ssl->in_msglen )
3902 return( -1 );
3903
3904 return( 0 );
3905}
3906
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003907/*
3908 * Mark bits in bitmask (used for DTLS HS reassembly)
3909 */
3910static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3911{
3912 unsigned int start_bits, end_bits;
3913
3914 start_bits = 8 - ( offset % 8 );
3915 if( start_bits != 8 )
3916 {
3917 size_t first_byte_idx = offset / 8;
3918
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003919 /* Special case */
3920 if( len <= start_bits )
3921 {
3922 for( ; len != 0; len-- )
3923 mask[first_byte_idx] |= 1 << ( start_bits - len );
3924
3925 /* Avoid potential issues with offset or len becoming invalid */
3926 return;
3927 }
3928
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003929 offset += start_bits; /* Now offset % 8 == 0 */
3930 len -= start_bits;
3931
3932 for( ; start_bits != 0; start_bits-- )
3933 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3934 }
3935
3936 end_bits = len % 8;
3937 if( end_bits != 0 )
3938 {
3939 size_t last_byte_idx = ( offset + len ) / 8;
3940
3941 len -= end_bits; /* Now len % 8 == 0 */
3942
3943 for( ; end_bits != 0; end_bits-- )
3944 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3945 }
3946
3947 memset( mask + offset / 8, 0xFF, len / 8 );
3948}
3949
3950/*
3951 * Check that bitmask is full
3952 */
3953static int ssl_bitmask_check( unsigned char *mask, size_t len )
3954{
3955 size_t i;
3956
3957 for( i = 0; i < len / 8; i++ )
3958 if( mask[i] != 0xFF )
3959 return( -1 );
3960
3961 for( i = 0; i < len % 8; i++ )
3962 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3963 return( -1 );
3964
3965 return( 0 );
3966}
3967
Hanno Becker56e205e2018-08-16 09:06:12 +01003968/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003969static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003970 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003971{
Hanno Becker56e205e2018-08-16 09:06:12 +01003972 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003973
Hanno Becker56e205e2018-08-16 09:06:12 +01003974 alloc_len = 12; /* Handshake header */
3975 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003976
Hanno Beckerd07df862018-08-16 09:14:58 +01003977 if( add_bitmap )
3978 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003979
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003980 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003981}
Hanno Becker56e205e2018-08-16 09:06:12 +01003982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003984
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003985static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003986{
3987 return( ( ssl->in_msg[1] << 16 ) |
3988 ( ssl->in_msg[2] << 8 ) |
3989 ssl->in_msg[3] );
3990}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991
Simon Butcher99000142016-10-13 17:21:01 +01003992int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003997 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003999 }
4000
Hanno Becker12555c62018-08-16 12:47:53 +01004001 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004005 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004008 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004009 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004010 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004011 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004012
Hanno Becker44650b72018-08-16 12:51:11 +01004013 if( ssl_check_hs_header( ssl ) != 0 )
4014 {
4015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4016 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4017 }
4018
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004019 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004020 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4021 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4022 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4023 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004024 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004025 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4026 {
4027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4028 recv_msg_seq,
4029 ssl->handshake->in_msg_seq ) );
4030 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4031 }
4032
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004033 /* Retransmit only on last message from previous flight, to avoid
4034 * too many retransmissions.
4035 * Besides, No sane server ever retransmits HelloVerifyRequest */
4036 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004040 "message_seq = %d, start_of_flight = %d",
4041 recv_msg_seq,
4042 ssl->handshake->in_flight_start_seq ) );
4043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004047 return( ret );
4048 }
4049 }
4050 else
4051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004053 "message_seq = %d, expected = %d",
4054 recv_msg_seq,
4055 ssl->handshake->in_msg_seq ) );
4056 }
4057
Hanno Becker90333da2017-10-10 11:27:13 +01004058 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004059 }
4060 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004061
Hanno Becker6d97ef52018-08-16 13:09:04 +01004062 /* Message reassembly is handled alongside buffering of future
4063 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004064 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004065 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004066 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004069 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004070 }
4071 }
4072 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004074 /* With TLS we don't handle fragmentation (for now) */
4075 if( ssl->in_msglen < ssl->in_hslen )
4076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4078 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004079 }
4080
Simon Butcher99000142016-10-13 17:21:01 +01004081 return( 0 );
4082}
4083
4084void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4085{
Hanno Becker0271f962018-08-16 13:23:47 +01004086 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004087
Hanno Becker0271f962018-08-16 13:23:47 +01004088 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004089 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004090 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004091 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004092
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004093 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004096 ssl->handshake != NULL )
4097 {
Hanno Becker0271f962018-08-16 13:23:47 +01004098 unsigned offset;
4099 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004100
Hanno Becker0271f962018-08-16 13:23:47 +01004101 /* Increment handshake sequence number */
4102 hs->in_msg_seq++;
4103
4104 /*
4105 * Clear up handshake buffering and reassembly structure.
4106 */
4107
4108 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004109 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004110
4111 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004112 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4113 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004114 offset++, hs_buf++ )
4115 {
4116 *hs_buf = *(hs_buf + 1);
4117 }
4118
4119 /* Create a fresh last entry */
4120 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004121 }
4122#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004123}
4124
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004125/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004126 * DTLS anti-replay: RFC 6347 4.1.2.6
4127 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004128 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4129 * Bit n is set iff record number in_window_top - n has been seen.
4130 *
4131 * Usually, in_window_top is the last record number seen and the lsb of
4132 * in_window is set. The only exception is the initial state (record number 0
4133 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4136static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004137{
4138 ssl->in_window_top = 0;
4139 ssl->in_window = 0;
4140}
4141
4142static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4143{
4144 return( ( (uint64_t) buf[0] << 40 ) |
4145 ( (uint64_t) buf[1] << 32 ) |
4146 ( (uint64_t) buf[2] << 24 ) |
4147 ( (uint64_t) buf[3] << 16 ) |
4148 ( (uint64_t) buf[4] << 8 ) |
4149 ( (uint64_t) buf[5] ) );
4150}
4151
4152/*
4153 * Return 0 if sequence number is acceptable, -1 otherwise
4154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004156{
4157 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4158 uint64_t bit;
4159
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004160 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004161 return( 0 );
4162
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004163 if( rec_seqnum > ssl->in_window_top )
4164 return( 0 );
4165
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004166 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004167
4168 if( bit >= 64 )
4169 return( -1 );
4170
4171 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4172 return( -1 );
4173
4174 return( 0 );
4175}
4176
4177/*
4178 * Update replay window on new validated record
4179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004181{
4182 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4183
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004184 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004185 return;
4186
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187 if( rec_seqnum > ssl->in_window_top )
4188 {
4189 /* Update window_top and the contents of the window */
4190 uint64_t shift = rec_seqnum - ssl->in_window_top;
4191
4192 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004193 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004194 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004195 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004196 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004197 ssl->in_window |= 1;
4198 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004199
4200 ssl->in_window_top = rec_seqnum;
4201 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004202 else
4203 {
4204 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004205 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004206
4207 if( bit < 64 ) /* Always true, but be extra sure */
4208 ssl->in_window |= (uint64_t) 1 << bit;
4209 }
4210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004212
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004213#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004214/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004215static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4216
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004217/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004218 * Without any SSL context, check if a datagram looks like a ClientHello with
4219 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004220 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004221 *
4222 * - if cookie is valid, return 0
4223 * - if ClientHello looks superficially valid but cookie is not,
4224 * fill obuf and set olen, then
4225 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4226 * - otherwise return a specific error code
4227 */
4228static int ssl_check_dtls_clihlo_cookie(
4229 mbedtls_ssl_cookie_write_t *f_cookie_write,
4230 mbedtls_ssl_cookie_check_t *f_cookie_check,
4231 void *p_cookie,
4232 const unsigned char *cli_id, size_t cli_id_len,
4233 const unsigned char *in, size_t in_len,
4234 unsigned char *obuf, size_t buf_len, size_t *olen )
4235{
4236 size_t sid_len, cookie_len;
4237 unsigned char *p;
4238
4239 if( f_cookie_write == NULL || f_cookie_check == NULL )
4240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4241
4242 /*
4243 * Structure of ClientHello with record and handshake headers,
4244 * and expected values. We don't need to check a lot, more checks will be
4245 * done when actually parsing the ClientHello - skipping those checks
4246 * avoids code duplication and does not make cookie forging any easier.
4247 *
4248 * 0-0 ContentType type; copied, must be handshake
4249 * 1-2 ProtocolVersion version; copied
4250 * 3-4 uint16 epoch; copied, must be 0
4251 * 5-10 uint48 sequence_number; copied
4252 * 11-12 uint16 length; (ignored)
4253 *
4254 * 13-13 HandshakeType msg_type; (ignored)
4255 * 14-16 uint24 length; (ignored)
4256 * 17-18 uint16 message_seq; copied
4257 * 19-21 uint24 fragment_offset; copied, must be 0
4258 * 22-24 uint24 fragment_length; (ignored)
4259 *
4260 * 25-26 ProtocolVersion client_version; (ignored)
4261 * 27-58 Random random; (ignored)
4262 * 59-xx SessionID session_id; 1 byte len + sid_len content
4263 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4264 * ...
4265 *
4266 * Minimum length is 61 bytes.
4267 */
4268 if( in_len < 61 ||
4269 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4270 in[3] != 0 || in[4] != 0 ||
4271 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4272 {
4273 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4274 }
4275
4276 sid_len = in[59];
4277 if( sid_len > in_len - 61 )
4278 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4279
4280 cookie_len = in[60 + sid_len];
4281 if( cookie_len > in_len - 60 )
4282 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4283
4284 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4285 cli_id, cli_id_len ) == 0 )
4286 {
4287 /* Valid cookie */
4288 return( 0 );
4289 }
4290
4291 /*
4292 * If we get here, we've got an invalid cookie, let's prepare HVR.
4293 *
4294 * 0-0 ContentType type; copied
4295 * 1-2 ProtocolVersion version; copied
4296 * 3-4 uint16 epoch; copied
4297 * 5-10 uint48 sequence_number; copied
4298 * 11-12 uint16 length; olen - 13
4299 *
4300 * 13-13 HandshakeType msg_type; hello_verify_request
4301 * 14-16 uint24 length; olen - 25
4302 * 17-18 uint16 message_seq; copied
4303 * 19-21 uint24 fragment_offset; copied
4304 * 22-24 uint24 fragment_length; olen - 25
4305 *
4306 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4307 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4308 *
4309 * Minimum length is 28.
4310 */
4311 if( buf_len < 28 )
4312 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4313
4314 /* Copy most fields and adapt others */
4315 memcpy( obuf, in, 25 );
4316 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4317 obuf[25] = 0xfe;
4318 obuf[26] = 0xff;
4319
4320 /* Generate and write actual cookie */
4321 p = obuf + 28;
4322 if( f_cookie_write( p_cookie,
4323 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4324 {
4325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4326 }
4327
4328 *olen = p - obuf;
4329
4330 /* Go back and fill length fields */
4331 obuf[27] = (unsigned char)( *olen - 28 );
4332
4333 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4334 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4335 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4336
4337 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4338 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4339
4340 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4341}
4342
4343/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004344 * Handle possible client reconnect with the same UDP quadruplet
4345 * (RFC 6347 Section 4.2.8).
4346 *
4347 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4348 * that looks like a ClientHello.
4349 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004350 * - if the input looks like a ClientHello without cookies,
4351 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004352 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004353 * - if the input looks like a ClientHello with a valid cookie,
4354 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004355 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004356 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004357 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004358 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004359 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4360 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004361 */
4362static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4363{
4364 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004365 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004366
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004367 ret = ssl_check_dtls_clihlo_cookie(
4368 ssl->conf->f_cookie_write,
4369 ssl->conf->f_cookie_check,
4370 ssl->conf->p_cookie,
4371 ssl->cli_id, ssl->cli_id_len,
4372 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004373 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004374
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004375 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4376
4377 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004378 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004379 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 * If the error is permanent we'll catch it later,
4381 * if it's not, then hopefully it'll work next time. */
4382 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4383
4384 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004385 }
4386
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004387 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004388 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004389 /* Got a valid cookie, partially reset context */
4390 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4391 {
4392 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4393 return( ret );
4394 }
4395
4396 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004397 }
4398
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004399 return( ret );
4400}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004401#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004402
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004403/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004404 * ContentType type;
4405 * ProtocolVersion version;
4406 * uint16 epoch; // DTLS only
4407 * uint48 sequence_number; // DTLS only
4408 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004409 *
4410 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004411 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004412 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4413 *
4414 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004415 * 1. proceed with the record if this function returns 0
4416 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4417 * 3. return CLIENT_RECONNECT if this function return that value
4418 * 4. drop the whole datagram if this function returns anything else.
4419 * Point 2 is needed when the peer is resending, and we have already received
4420 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004423{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004424 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 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 +02004427
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004429 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004430 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004434 ssl->in_msgtype,
4435 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004436
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004437 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4439 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4440 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4441 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004444
4445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004446 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4447 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004448 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4449#endif /* MBEDTLS_SSL_PROTO_DTLS */
4450 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4451 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004454 }
4455
4456 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004457 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4460 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004461 }
4462
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004463 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4466 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 }
4468
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004469 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004470 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004471 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4474 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004475 }
4476
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004477 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004478 * DTLS-related tests.
4479 * Check epoch before checking length constraint because
4480 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4481 * message gets duplicated before the corresponding Finished message,
4482 * the second ChangeCipherSpec should be discarded because it belongs
4483 * to an old epoch, but not because its length is shorter than
4484 * the minimum record length for packets using the new record transform.
4485 * Note that these two kinds of failures are handled differently,
4486 * as an unexpected record is silently skipped but an invalid
4487 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004488 */
4489#if defined(MBEDTLS_SSL_PROTO_DTLS)
4490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4491 {
4492 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4493
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004494 /* Check epoch (and sequence number) with DTLS */
4495 if( rec_epoch != ssl->in_epoch )
4496 {
4497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4498 "expected %d, received %d",
4499 ssl->in_epoch, rec_epoch ) );
4500
4501#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4502 /*
4503 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4504 * access the first byte of record content (handshake type), as we
4505 * have an active transform (possibly iv_len != 0), so use the
4506 * fact that the record header len is 13 instead.
4507 */
4508 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4509 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4510 rec_epoch == 0 &&
4511 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4512 ssl->in_left > 13 &&
4513 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4516 "from the same port" ) );
4517 return( ssl_handle_possible_reconnect( ssl ) );
4518 }
4519 else
4520#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004521 {
4522 /* Consider buffering the record. */
4523 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4524 {
4525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4526 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4527 }
4528
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004529 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004530 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004531 }
4532
4533#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4534 /* Replay detection only works for the current epoch */
4535 if( rec_epoch == ssl->in_epoch &&
4536 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4537 {
4538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4539 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4540 }
4541#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004542
Hanno Becker52c6dc62017-05-26 16:07:36 +01004543 /* Drop unexpected ApplicationData records,
4544 * except at the beginning of renegotiations */
4545 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4546 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4547#if defined(MBEDTLS_SSL_RENEGOTIATION)
4548 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4549 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4550#endif
4551 )
4552 {
4553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4554 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4555 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004556 }
4557#endif /* MBEDTLS_SSL_PROTO_DTLS */
4558
Hanno Becker52c6dc62017-05-26 16:07:36 +01004559
4560 /* Check length against bounds of the current transform and version */
4561 if( ssl->transform_in == NULL )
4562 {
4563 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004564 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004565 {
4566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4567 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4568 }
4569 }
4570 else
4571 {
4572 if( ssl->in_msglen < ssl->transform_in->minlen )
4573 {
4574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4576 }
4577
4578#if defined(MBEDTLS_SSL_PROTO_SSL3)
4579 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004580 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004581 {
4582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4583 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4584 }
4585#endif
4586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4587 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4588 /*
4589 * TLS encrypted messages can have up to 256 bytes of padding
4590 */
4591 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4592 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004593 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004594 {
4595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4596 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4597 }
4598#endif
4599 }
4600
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601 return( 0 );
4602}
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004604/*
4605 * If applicable, decrypt (and decompress) record content
4606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004608{
4609 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4612 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4615 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 ret = mbedtls_ssl_hw_record_read( ssl );
4620 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4623 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004624 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004625
4626 if( ret == 0 )
4627 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004630 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004632 mbedtls_record rec;
4633
4634 rec.buf = ssl->in_iv;
4635 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4636 - ( ssl->in_iv - ssl->in_buf );
4637 rec.data_len = ssl->in_msglen;
4638 rec.data_offset = 0;
4639
4640 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4641 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4642 ssl->conf->transport, rec.ver );
4643 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004644 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4645 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 return( ret );
4649 }
4650
Hanno Becker29800d22018-08-07 14:30:18 +01004651 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4652 {
4653 /* Should never happen */
4654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004657 ssl->in_msglen = rec.data_len;
4658 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4659 ssl->in_len[1] = (unsigned char)( rec.data_len );
4660
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004661 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4662 ssl->in_msg, ssl->in_msglen );
4663
Angus Grattond8213d02016-05-25 20:56:48 +10004664 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4667 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004669 else if( ssl->in_msglen == 0 )
4670 {
4671#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4672 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4673 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4674 {
4675 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4677 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4678 }
4679#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4680
4681 ssl->nb_zero++;
4682
4683 /*
4684 * Three or more empty messages may be a DoS attack
4685 * (excessive CPU consumption).
4686 */
4687 if( ssl->nb_zero > 3 )
4688 {
4689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4690 "messages, possible DoS attack" ) );
4691 /* Q: Is that the right error code? */
4692 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4693 }
4694 }
4695 else
4696 ssl->nb_zero = 0;
4697
4698#if defined(MBEDTLS_SSL_PROTO_DTLS)
4699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4700 {
4701 ; /* in_ctr read from peer, not maintained internally */
4702 }
4703 else
4704#endif
4705 {
4706 unsigned i;
4707 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4708 if( ++ssl->in_ctr[i - 1] != 0 )
4709 break;
4710
4711 /* The loop goes to its end iff the counter is wrapping */
4712 if( i == ssl_ep_len( ssl ) )
4713 {
4714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4715 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4716 }
4717 }
4718
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 }
4720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004722 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004724 {
4725 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004728 return( ret );
4729 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004737 }
4738#endif
4739
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004740 return( 0 );
4741}
4742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004744
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004745/*
4746 * Read a record.
4747 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004748 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4749 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4750 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004751 */
Hanno Becker1097b342018-08-15 14:09:41 +01004752
4753/* Helper functions for mbedtls_ssl_read_record(). */
4754static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004755static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4756static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004757
Hanno Becker327c93b2018-08-15 13:56:18 +01004758int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004759 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004760{
4761 int ret;
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004764
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004765 if( ssl->keep_current_message == 0 )
4766 {
4767 do {
Simon Butcher99000142016-10-13 17:21:01 +01004768
Hanno Becker26994592018-08-15 14:14:59 +01004769 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004770 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004771 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004772
Hanno Beckere74d5562018-08-15 14:26:08 +01004773 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004774 {
Hanno Becker40f50842018-08-15 14:48:01 +01004775#if defined(MBEDTLS_SSL_PROTO_DTLS)
4776 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004777
Hanno Becker40f50842018-08-15 14:48:01 +01004778 /* We only check for buffered messages if the
4779 * current datagram is fully consumed. */
4780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004781 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004782 {
Hanno Becker40f50842018-08-15 14:48:01 +01004783 if( ssl_load_buffered_message( ssl ) == 0 )
4784 have_buffered = 1;
4785 }
4786
4787 if( have_buffered == 0 )
4788#endif /* MBEDTLS_SSL_PROTO_DTLS */
4789 {
4790 ret = ssl_get_next_record( ssl );
4791 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4792 continue;
4793
4794 if( ret != 0 )
4795 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004796 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004797 return( ret );
4798 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004799 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004800 }
4801
4802 ret = mbedtls_ssl_handle_message_type( ssl );
4803
Hanno Becker40f50842018-08-15 14:48:01 +01004804#if defined(MBEDTLS_SSL_PROTO_DTLS)
4805 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4806 {
4807 /* Buffer future message */
4808 ret = ssl_buffer_message( ssl );
4809 if( ret != 0 )
4810 return( ret );
4811
4812 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4813 }
4814#endif /* MBEDTLS_SSL_PROTO_DTLS */
4815
Hanno Becker90333da2017-10-10 11:27:13 +01004816 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4817 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004818
4819 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004820 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004821 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004822 return( ret );
4823 }
4824
Hanno Becker327c93b2018-08-15 13:56:18 +01004825 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004826 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004827 {
4828 mbedtls_ssl_update_handshake_status( ssl );
4829 }
Simon Butcher99000142016-10-13 17:21:01 +01004830 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004831 else
Simon Butcher99000142016-10-13 17:21:01 +01004832 {
Hanno Becker02f59072018-08-15 14:00:24 +01004833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004834 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004835 }
4836
4837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4838
4839 return( 0 );
4840}
4841
Hanno Becker40f50842018-08-15 14:48:01 +01004842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004843static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004844{
Hanno Becker40f50842018-08-15 14:48:01 +01004845 if( ssl->in_left > ssl->next_record_offset )
4846 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004847
Hanno Becker40f50842018-08-15 14:48:01 +01004848 return( 0 );
4849}
4850
4851static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4852{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004853 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004854 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004855 int ret = 0;
4856
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004857 if( hs == NULL )
4858 return( -1 );
4859
Hanno Beckere00ae372018-08-20 09:39:42 +01004860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4861
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004862 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4863 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4864 {
4865 /* Check if we have seen a ChangeCipherSpec before.
4866 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004867 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004868 {
4869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4870 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004871 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004872 }
4873
Hanno Becker39b8bc92018-08-28 17:17:13 +01004874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004875 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4876 ssl->in_msglen = 1;
4877 ssl->in_msg[0] = 1;
4878
4879 /* As long as they are equal, the exact value doesn't matter. */
4880 ssl->in_left = 0;
4881 ssl->next_record_offset = 0;
4882
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004883 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004884 goto exit;
4885 }
Hanno Becker37f95322018-08-16 13:55:32 +01004886
Hanno Beckerb8f50142018-08-28 10:01:34 +01004887#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004888 /* Debug only */
4889 {
4890 unsigned offset;
4891 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4892 {
4893 hs_buf = &hs->buffering.hs[offset];
4894 if( hs_buf->is_valid == 1 )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4897 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004898 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004899 }
4900 }
4901 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004902#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004903
4904 /* Check if we have buffered and/or fully reassembled the
4905 * next handshake message. */
4906 hs_buf = &hs->buffering.hs[0];
4907 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4908 {
4909 /* Synthesize a record containing the buffered HS message. */
4910 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4911 ( hs_buf->data[2] << 8 ) |
4912 hs_buf->data[3];
4913
4914 /* Double-check that we haven't accidentally buffered
4915 * a message that doesn't fit into the input buffer. */
4916 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4917 {
4918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4920 }
4921
4922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4923 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4924 hs_buf->data, msg_len + 12 );
4925
4926 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4927 ssl->in_hslen = msg_len + 12;
4928 ssl->in_msglen = msg_len + 12;
4929 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4930
4931 ret = 0;
4932 goto exit;
4933 }
4934 else
4935 {
4936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4937 hs->in_msg_seq ) );
4938 }
4939
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004940 ret = -1;
4941
4942exit:
4943
4944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4945 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004946}
4947
Hanno Beckera02b0b42018-08-21 17:20:27 +01004948static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4949 size_t desired )
4950{
4951 int offset;
4952 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4954 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004955
Hanno Becker01315ea2018-08-21 17:22:17 +01004956 /* Get rid of future records epoch first, if such exist. */
4957 ssl_free_buffered_record( ssl );
4958
4959 /* Check if we have enough space available now. */
4960 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4961 hs->buffering.total_bytes_buffered ) )
4962 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004964 return( 0 );
4965 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004966
Hanno Becker4f432ad2018-08-28 10:02:32 +01004967 /* We don't have enough space to buffer the next expected handshake
4968 * message. Remove buffers used for future messages to gain space,
4969 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004970 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4971 offset >= 0; offset-- )
4972 {
4973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4974 offset ) );
4975
Hanno Beckerb309b922018-08-23 13:18:05 +01004976 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004977
4978 /* Check if we have enough space available now. */
4979 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4980 hs->buffering.total_bytes_buffered ) )
4981 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004983 return( 0 );
4984 }
4985 }
4986
4987 return( -1 );
4988}
4989
Hanno Becker40f50842018-08-15 14:48:01 +01004990static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4991{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004992 int ret = 0;
4993 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4994
4995 if( hs == NULL )
4996 return( 0 );
4997
4998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4999
5000 switch( ssl->in_msgtype )
5001 {
5002 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005004
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005005 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005006 break;
5007
5008 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005009 {
5010 unsigned recv_msg_seq_offset;
5011 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5012 mbedtls_ssl_hs_buffer *hs_buf;
5013 size_t msg_len = ssl->in_hslen - 12;
5014
5015 /* We should never receive an old handshake
5016 * message - double-check nonetheless. */
5017 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5020 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5021 }
5022
5023 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5024 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5025 {
5026 /* Silently ignore -- message too far in the future */
5027 MBEDTLS_SSL_DEBUG_MSG( 2,
5028 ( "Ignore future HS message with sequence number %u, "
5029 "buffering window %u - %u",
5030 recv_msg_seq, ssl->handshake->in_msg_seq,
5031 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5032
5033 goto exit;
5034 }
5035
5036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5037 recv_msg_seq, recv_msg_seq_offset ) );
5038
5039 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5040
5041 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005042 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005043 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005044 size_t reassembly_buf_sz;
5045
Hanno Becker37f95322018-08-16 13:55:32 +01005046 hs_buf->is_fragmented =
5047 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5048
5049 /* We copy the message back into the input buffer
5050 * after reassembly, so check that it's not too large.
5051 * This is an implementation-specific limitation
5052 * and not one from the standard, hence it is not
5053 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005054 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005055 {
5056 /* Ignore message */
5057 goto exit;
5058 }
5059
Hanno Beckere0b150f2018-08-21 15:51:03 +01005060 /* Check if we have enough space to buffer the message. */
5061 if( hs->buffering.total_bytes_buffered >
5062 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5063 {
5064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5066 }
5067
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005068 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5069 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005070
5071 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5072 hs->buffering.total_bytes_buffered ) )
5073 {
5074 if( recv_msg_seq_offset > 0 )
5075 {
5076 /* If we can't buffer a future message because
5077 * of space limitations -- ignore. */
5078 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",
5079 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5080 (unsigned) hs->buffering.total_bytes_buffered ) );
5081 goto exit;
5082 }
Hanno Beckere1801392018-08-21 16:51:05 +01005083 else
5084 {
5085 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",
5086 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5087 (unsigned) hs->buffering.total_bytes_buffered ) );
5088 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005089
Hanno Beckera02b0b42018-08-21 17:20:27 +01005090 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005091 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005092 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",
5093 (unsigned) msg_len,
5094 (unsigned) reassembly_buf_sz,
5095 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005096 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005097 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5098 goto exit;
5099 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005100 }
5101
5102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5103 msg_len ) );
5104
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005105 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5106 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005107 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005108 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005109 goto exit;
5110 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005111 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005112
5113 /* Prepare final header: copy msg_type, length and message_seq,
5114 * then add standardised fragment_offset and fragment_length */
5115 memcpy( hs_buf->data, ssl->in_msg, 6 );
5116 memset( hs_buf->data + 6, 0, 3 );
5117 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5118
5119 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005120
5121 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005122 }
5123 else
5124 {
5125 /* Make sure msg_type and length are consistent */
5126 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5127 {
5128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5129 /* Ignore */
5130 goto exit;
5131 }
5132 }
5133
Hanno Becker4422bbb2018-08-20 09:40:19 +01005134 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005135 {
5136 size_t frag_len, frag_off;
5137 unsigned char * const msg = hs_buf->data + 12;
5138
5139 /*
5140 * Check and copy current fragment
5141 */
5142
5143 /* Validation of header fields already done in
5144 * mbedtls_ssl_prepare_handshake_record(). */
5145 frag_off = ssl_get_hs_frag_off( ssl );
5146 frag_len = ssl_get_hs_frag_len( ssl );
5147
5148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5149 frag_off, frag_len ) );
5150 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5151
5152 if( hs_buf->is_fragmented )
5153 {
5154 unsigned char * const bitmask = msg + msg_len;
5155 ssl_bitmask_set( bitmask, frag_off, frag_len );
5156 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5157 msg_len ) == 0 );
5158 }
5159 else
5160 {
5161 hs_buf->is_complete = 1;
5162 }
5163
5164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5165 hs_buf->is_complete ? "" : "not yet " ) );
5166 }
5167
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005168 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005169 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005170
5171 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005172 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005173 break;
5174 }
5175
5176exit:
5177
5178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5179 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005180}
5181#endif /* MBEDTLS_SSL_PROTO_DTLS */
5182
Hanno Becker1097b342018-08-15 14:09:41 +01005183static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005184{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005185 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005186 * Consume last content-layer message and potentially
5187 * update in_msglen which keeps track of the contents'
5188 * consumption state.
5189 *
5190 * (1) Handshake messages:
5191 * Remove last handshake message, move content
5192 * and adapt in_msglen.
5193 *
5194 * (2) Alert messages:
5195 * Consume whole record content, in_msglen = 0.
5196 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005197 * (3) Change cipher spec:
5198 * Consume whole record content, in_msglen = 0.
5199 *
5200 * (4) Application data:
5201 * Don't do anything - the record layer provides
5202 * the application data as a stream transport
5203 * and consumes through mbedtls_ssl_read only.
5204 *
5205 */
5206
5207 /* Case (1): Handshake messages */
5208 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005209 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005210 /* Hard assertion to be sure that no application data
5211 * is in flight, as corrupting ssl->in_msglen during
5212 * ssl->in_offt != NULL is fatal. */
5213 if( ssl->in_offt != NULL )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5217 }
5218
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005219 /*
5220 * Get next Handshake message in the current record
5221 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005222
Hanno Becker4a810fb2017-05-24 16:27:30 +01005223 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005224 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005225 * current handshake content: If DTLS handshake
5226 * fragmentation is used, that's the fragment
5227 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005228 * size here is faulty and should be changed at
5229 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005230 * (2) While it doesn't seem to cause problems, one
5231 * has to be very careful not to assume that in_hslen
5232 * is always <= in_msglen in a sensible communication.
5233 * Again, it's wrong for DTLS handshake fragmentation.
5234 * The following check is therefore mandatory, and
5235 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005236 * Additionally, ssl->in_hslen might be arbitrarily out of
5237 * bounds after handling a DTLS message with an unexpected
5238 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005239 */
5240 if( ssl->in_hslen < ssl->in_msglen )
5241 {
5242 ssl->in_msglen -= ssl->in_hslen;
5243 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5244 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005245
Hanno Becker4a810fb2017-05-24 16:27:30 +01005246 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5247 ssl->in_msg, ssl->in_msglen );
5248 }
5249 else
5250 {
5251 ssl->in_msglen = 0;
5252 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005253
Hanno Becker4a810fb2017-05-24 16:27:30 +01005254 ssl->in_hslen = 0;
5255 }
5256 /* Case (4): Application data */
5257 else if( ssl->in_offt != NULL )
5258 {
5259 return( 0 );
5260 }
5261 /* Everything else (CCS & Alerts) */
5262 else
5263 {
5264 ssl->in_msglen = 0;
5265 }
5266
Hanno Becker1097b342018-08-15 14:09:41 +01005267 return( 0 );
5268}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005269
Hanno Beckere74d5562018-08-15 14:26:08 +01005270static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5271{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005272 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005273 return( 1 );
5274
5275 return( 0 );
5276}
5277
Hanno Becker5f066e72018-08-16 14:56:31 +01005278#if defined(MBEDTLS_SSL_PROTO_DTLS)
5279
5280static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5281{
5282 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5283 if( hs == NULL )
5284 return;
5285
Hanno Becker01315ea2018-08-21 17:22:17 +01005286 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005287 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005288 hs->buffering.total_bytes_buffered -=
5289 hs->buffering.future_record.len;
5290
5291 mbedtls_free( hs->buffering.future_record.data );
5292 hs->buffering.future_record.data = NULL;
5293 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005294}
5295
5296static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5297{
5298 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5299 unsigned char * rec;
5300 size_t rec_len;
5301 unsigned rec_epoch;
5302
5303 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5304 return( 0 );
5305
5306 if( hs == NULL )
5307 return( 0 );
5308
Hanno Becker5f066e72018-08-16 14:56:31 +01005309 rec = hs->buffering.future_record.data;
5310 rec_len = hs->buffering.future_record.len;
5311 rec_epoch = hs->buffering.future_record.epoch;
5312
5313 if( rec == NULL )
5314 return( 0 );
5315
Hanno Becker4cb782d2018-08-20 11:19:05 +01005316 /* Only consider loading future records if the
5317 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005318 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005319 return( 0 );
5320
Hanno Becker5f066e72018-08-16 14:56:31 +01005321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5322
5323 if( rec_epoch != ssl->in_epoch )
5324 {
5325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5326 goto exit;
5327 }
5328
5329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5330
5331 /* Double-check that the record is not too large */
5332 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5333 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5334 {
5335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5337 }
5338
5339 memcpy( ssl->in_hdr, rec, rec_len );
5340 ssl->in_left = rec_len;
5341 ssl->next_record_offset = 0;
5342
5343 ssl_free_buffered_record( ssl );
5344
5345exit:
5346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5347 return( 0 );
5348}
5349
5350static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5351{
5352 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5353 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005354 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005355
5356 /* Don't buffer future records outside handshakes. */
5357 if( hs == NULL )
5358 return( 0 );
5359
5360 /* Only buffer handshake records (we are only interested
5361 * in Finished messages). */
5362 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5363 return( 0 );
5364
5365 /* Don't buffer more than one future epoch record. */
5366 if( hs->buffering.future_record.data != NULL )
5367 return( 0 );
5368
Hanno Becker01315ea2018-08-21 17:22:17 +01005369 /* Don't buffer record if there's not enough buffering space remaining. */
5370 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5371 hs->buffering.total_bytes_buffered ) )
5372 {
5373 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",
5374 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5375 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005376 return( 0 );
5377 }
5378
Hanno Becker5f066e72018-08-16 14:56:31 +01005379 /* Buffer record */
5380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5381 ssl->in_epoch + 1 ) );
5382 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5383 rec_hdr_len + ssl->in_msglen );
5384
5385 /* ssl_parse_record_header() only considers records
5386 * of the next epoch as candidates for buffering. */
5387 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005388 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005389
5390 hs->buffering.future_record.data =
5391 mbedtls_calloc( 1, hs->buffering.future_record.len );
5392 if( hs->buffering.future_record.data == NULL )
5393 {
5394 /* If we run out of RAM trying to buffer a
5395 * record from the next epoch, just ignore. */
5396 return( 0 );
5397 }
5398
Hanno Becker01315ea2018-08-21 17:22:17 +01005399 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005400
Hanno Becker01315ea2018-08-21 17:22:17 +01005401 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005402 return( 0 );
5403}
5404
5405#endif /* MBEDTLS_SSL_PROTO_DTLS */
5406
Hanno Beckere74d5562018-08-15 14:26:08 +01005407static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005408{
5409 int ret;
5410
Hanno Becker5f066e72018-08-16 14:56:31 +01005411#if defined(MBEDTLS_SSL_PROTO_DTLS)
5412 /* We might have buffered a future record; if so,
5413 * and if the epoch matches now, load it.
5414 * On success, this call will set ssl->in_left to
5415 * the length of the buffered record, so that
5416 * the calls to ssl_fetch_input() below will
5417 * essentially be no-ops. */
5418 ret = ssl_load_buffered_record( ssl );
5419 if( ret != 0 )
5420 return( ret );
5421#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005426 return( ret );
5427 }
5428
5429 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005432 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5433 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005434 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005435 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5436 {
5437 ret = ssl_buffer_future_record( ssl );
5438 if( ret != 0 )
5439 return( ret );
5440
5441 /* Fall through to handling of unexpected records */
5442 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5443 }
5444
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005445 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5446 {
5447 /* Skip unexpected record (but not whole datagram) */
5448 ssl->next_record_offset = ssl->in_msglen
5449 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005450
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5452 "(header)" ) );
5453 }
5454 else
5455 {
5456 /* Skip invalid record and the rest of the datagram */
5457 ssl->next_record_offset = 0;
5458 ssl->in_left = 0;
5459
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5461 "(header)" ) );
5462 }
5463
5464 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005465 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005466 }
5467#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005468 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005469 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005470
5471 /*
5472 * Read and optionally decrypt the message contents
5473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5475 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005478 return( ret );
5479 }
5480
5481 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005486 if( ssl->next_record_offset < ssl->in_left )
5487 {
5488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5489 }
5490 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005491 else
5492#endif
5493 ssl->in_left = 0;
5494
5495 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005499 {
5500 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5502 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005503 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005504 /* Except when waiting for Finished as a bad mac here
5505 * probably means something went wrong in the handshake
5506 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5507 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5508 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5509 {
5510#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5511 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5512 {
5513 mbedtls_ssl_send_alert_message( ssl,
5514 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5515 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5516 }
5517#endif
5518 return( ret );
5519 }
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005522 if( ssl->conf->badmac_limit != 0 &&
5523 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5526 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005527 }
5528#endif
5529
Hanno Becker4a810fb2017-05-24 16:27:30 +01005530 /* As above, invalid records cause
5531 * dismissal of the whole datagram. */
5532
5533 ssl->next_record_offset = 0;
5534 ssl->in_left = 0;
5535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005537 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005538 }
5539
5540 return( ret );
5541 }
5542 else
5543#endif
5544 {
5545 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5547 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 mbedtls_ssl_send_alert_message( ssl,
5550 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5551 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005552 }
5553#endif
5554 return( ret );
5555 }
5556 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005557
Simon Butcher99000142016-10-13 17:21:01 +01005558 return( 0 );
5559}
5560
5561int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5562{
5563 int ret;
5564
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005565 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005566 * Handle particular types of records
5567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005569 {
Simon Butcher99000142016-10-13 17:21:01 +01005570 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5571 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005572 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005574 }
5575
Hanno Beckere678eaa2018-08-21 14:57:46 +01005576 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005577 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005578 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005579 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5581 ssl->in_msglen ) );
5582 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005583 }
5584
Hanno Beckere678eaa2018-08-21 14:57:46 +01005585 if( ssl->in_msg[0] != 1 )
5586 {
5587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5588 ssl->in_msg[0] ) );
5589 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5590 }
5591
5592#if defined(MBEDTLS_SSL_PROTO_DTLS)
5593 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5594 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5595 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5596 {
5597 if( ssl->handshake == NULL )
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5600 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5601 }
5602
5603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5604 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5605 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005606#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005607 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005610 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005611 if( ssl->in_msglen != 2 )
5612 {
5613 /* Note: Standard allows for more than one 2 byte alert
5614 to be packed in a single message, but Mbed TLS doesn't
5615 currently support this. */
5616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5617 ssl->in_msglen ) );
5618 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005622 ssl->in_msg[0], ssl->in_msg[1] ) );
5623
5624 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005625 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005630 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 }
5633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5635 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5638 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005640
5641#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5642 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5643 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5644 {
Hanno Becker90333da2017-10-10 11:27:13 +01005645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005646 /* Will be handled when trying to parse ServerHello */
5647 return( 0 );
5648 }
5649#endif
5650
5651#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5652 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5653 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5654 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5655 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5656 {
5657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5658 /* Will be handled in mbedtls_ssl_parse_certificate() */
5659 return( 0 );
5660 }
5661#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5662
5663 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005664 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 }
5666
Hanno Beckerc76c6192017-06-06 10:03:17 +01005667#if defined(MBEDTLS_SSL_PROTO_DTLS)
5668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5669 ssl->handshake != NULL &&
5670 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5671 {
5672 ssl_handshake_wrapup_free_hs_transform( ssl );
5673 }
5674#endif
5675
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 return( 0 );
5677}
5678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005680{
5681 int ret;
5682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5684 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5685 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005686 {
5687 return( ret );
5688 }
5689
5690 return( 0 );
5691}
5692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005694 unsigned char level,
5695 unsigned char message )
5696{
5697 int ret;
5698
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005699 if( ssl == NULL || ssl->conf == NULL )
5700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005706 ssl->out_msglen = 2;
5707 ssl->out_msg[0] = level;
5708 ssl->out_msg[1] = message;
5709
Hanno Becker67bc7c32018-08-06 11:33:50 +01005710 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005713 return( ret );
5714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005716
5717 return( 0 );
5718}
5719
Hanno Beckerb9d44792019-02-08 07:19:04 +00005720#if defined(MBEDTLS_X509_CRT_PARSE_C)
5721static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5722{
5723#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5724 if( session->peer_cert != NULL )
5725 {
5726 mbedtls_x509_crt_free( session->peer_cert );
5727 mbedtls_free( session->peer_cert );
5728 session->peer_cert = NULL;
5729 }
5730#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5731 if( session->peer_cert_digest != NULL )
5732 {
5733 /* Zeroization is not necessary. */
5734 mbedtls_free( session->peer_cert_digest );
5735 session->peer_cert_digest = NULL;
5736 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5737 session->peer_cert_digest_len = 0;
5738 }
5739#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5740}
5741#endif /* MBEDTLS_X509_CRT_PARSE_C */
5742
Paul Bakker5121ce52009-01-03 21:22:43 +00005743/*
5744 * Handshake functions
5745 */
Hanno Becker21489932019-02-05 13:20:55 +00005746#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005747/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005749{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005750 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5751 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005754
Hanno Becker7177a882019-02-05 13:36:46 +00005755 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005758 ssl->state++;
5759 return( 0 );
5760 }
5761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005764}
5765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005767{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005768 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5769 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005772
Hanno Becker7177a882019-02-05 13:36:46 +00005773 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005776 ssl->state++;
5777 return( 0 );
5778 }
5779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005782}
Gilles Peskinef9828522017-05-03 12:28:43 +02005783
Hanno Becker21489932019-02-05 13:20:55 +00005784#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005785/* Some certificate support -> implement write and parse */
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005790 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005792 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5793 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005796
Hanno Becker7177a882019-02-05 13:36:46 +00005797 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005800 ssl->state++;
5801 return( 0 );
5802 }
5803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005805 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 {
5807 if( ssl->client_auth == 0 )
5808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810 ssl->state++;
5811 return( 0 );
5812 }
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005815 /*
5816 * If using SSLv3 and got no cert, send an Alert message
5817 * (otherwise an empty Certificate message will be sent).
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5820 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005821 {
5822 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5824 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5825 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 goto write_msg;
5829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832#endif /* MBEDTLS_SSL_CLI_C */
5833#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5839 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005840 }
5841 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005845
5846 /*
5847 * 0 . 0 handshake type
5848 * 1 . 3 handshake length
5849 * 4 . 6 length of all certs
5850 * 7 . 9 length of cert. 1
5851 * 10 . n-1 peer certificate
5852 * n . n+2 length of cert. 2
5853 * n+3 . ... upper level cert, etc.
5854 */
5855 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005857
Paul Bakker29087132010-03-21 21:03:34 +00005858 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 {
5860 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005861 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005864 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
5868 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5869 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5870 ssl->out_msg[i + 2] = (unsigned char)( n );
5871
5872 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5873 i += n; crt = crt->next;
5874 }
5875
5876 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5877 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5878 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5879
5880 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5882 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005883
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005884#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005885write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005886#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005887
5888 ssl->state++;
5889
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005890 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 return( ret );
5894 }
5895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897
Paul Bakkered27a042013-04-18 22:46:23 +02005898 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005899}
5900
Hanno Becker84879e32019-01-31 07:44:03 +00005901#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005902
5903#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005904static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5905 unsigned char *crt_buf,
5906 size_t crt_buf_len )
5907{
5908 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5909
5910 if( peer_crt == NULL )
5911 return( -1 );
5912
5913 if( peer_crt->raw.len != crt_buf_len )
5914 return( -1 );
5915
Hanno Becker46f34d02019-02-08 14:00:04 +00005916 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005917}
Hanno Becker177475a2019-02-05 17:02:46 +00005918#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5919static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5920 unsigned char *crt_buf,
5921 size_t crt_buf_len )
5922{
5923 int ret;
5924 unsigned char const * const peer_cert_digest =
5925 ssl->session->peer_cert_digest;
5926 mbedtls_md_type_t const peer_cert_digest_type =
5927 ssl->session->peer_cert_digest_type;
5928 mbedtls_md_info_t const * const digest_info =
5929 mbedtls_md_info_from_type( peer_cert_digest_type );
5930 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5931 size_t digest_len;
5932
5933 if( peer_cert_digest == NULL || digest_info == NULL )
5934 return( -1 );
5935
5936 digest_len = mbedtls_md_get_size( digest_info );
5937 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5938 return( -1 );
5939
5940 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5941 if( ret != 0 )
5942 return( -1 );
5943
5944 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5945}
5946#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005947#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005948
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005949/*
5950 * Once the certificate message is read, parse it into a cert chain and
5951 * perform basic checks, but leave actual verification to the caller
5952 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005953static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5954 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005955{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005956 int ret;
5957#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5958 int crt_cnt=0;
5959#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005960 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005961 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005966 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5967 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 }
5970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5972 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005975 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5976 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005978 }
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005981
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005984 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005985 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005986
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005987 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005991 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5992 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005994 }
5995
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005996 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5997 i += 3;
5998
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005999 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 while( i < ssl->in_hslen )
6001 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006002 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006003 if ( i + 3 > ssl->in_hslen ) {
6004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006005 mbedtls_ssl_send_alert_message( ssl,
6006 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6007 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006008 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6009 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006010 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6011 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 if( ssl->in_msg[i] != 0 )
6013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006015 mbedtls_ssl_send_alert_message( ssl,
6016 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6017 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006019 }
6020
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006021 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6023 | (unsigned int) ssl->in_msg[i + 2];
6024 i += 3;
6025
6026 if( n < 128 || i + n > ssl->in_hslen )
6027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006029 mbedtls_ssl_send_alert_message( ssl,
6030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6031 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 }
6034
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006035 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006036#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6037 if( crt_cnt++ == 0 &&
6038 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6039 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006040 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006041 /* During client-side renegotiation, check that the server's
6042 * end-CRTs hasn't changed compared to the initial handshake,
6043 * mitigating the triple handshake attack. On success, reuse
6044 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6046 if( ssl_check_peer_crt_unchanged( ssl,
6047 &ssl->in_msg[i],
6048 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006049 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6051 mbedtls_ssl_send_alert_message( ssl,
6052 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6053 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6054 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006055 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006056
6057 /* Now we can safely free the original chain. */
6058 ssl_clear_peer_cert( ssl->session );
6059 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006060#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6061
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006062 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006063#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006064 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006065#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006066 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006067 * it in-place from the input buffer instead of making a copy. */
6068 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6069#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006070 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006072 case 0: /*ok*/
6073 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6074 /* Ignore certificate with an unknown algorithm: maybe a
6075 prior certificate was already trusted. */
6076 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006077
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006078 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6079 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6080 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006081
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006082 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6083 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6084 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006085
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006086 default:
6087 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6088 crt_parse_der_failed:
6089 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6090 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6091 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 }
6093
6094 i += n;
6095 }
6096
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006097 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006098 return( 0 );
6099}
6100
Hanno Becker4a55f632019-02-05 12:49:06 +00006101#if defined(MBEDTLS_SSL_SRV_C)
6102static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6103{
6104 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6105 return( -1 );
6106
6107#if defined(MBEDTLS_SSL_PROTO_SSL3)
6108 /*
6109 * Check if the client sent an empty certificate
6110 */
6111 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6112 {
6113 if( ssl->in_msglen == 2 &&
6114 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6115 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6116 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6117 {
6118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6119 return( 0 );
6120 }
6121
6122 return( -1 );
6123 }
6124#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6125
6126#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6127 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6128 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6129 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6130 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6131 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6132 {
6133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6134 return( 0 );
6135 }
6136
6137 return( -1 );
6138#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6139 MBEDTLS_SSL_PROTO_TLS1_2 */
6140}
6141#endif /* MBEDTLS_SSL_SRV_C */
6142
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006143/* Check if a certificate message is expected.
6144 * Return either
6145 * - SSL_CERTIFICATE_EXPECTED, or
6146 * - SSL_CERTIFICATE_SKIP
6147 * indicating whether a Certificate message is expected or not.
6148 */
6149#define SSL_CERTIFICATE_EXPECTED 0
6150#define SSL_CERTIFICATE_SKIP 1
6151static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6152 int authmode )
6153{
6154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006155 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006156
6157 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6158 return( SSL_CERTIFICATE_SKIP );
6159
6160#if defined(MBEDTLS_SSL_SRV_C)
6161 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6162 {
6163 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6164 return( SSL_CERTIFICATE_SKIP );
6165
6166 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6167 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006168 ssl->session_negotiate->verify_result =
6169 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6170 return( SSL_CERTIFICATE_SKIP );
6171 }
6172 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006173#else
6174 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006175#endif /* MBEDTLS_SSL_SRV_C */
6176
6177 return( SSL_CERTIFICATE_EXPECTED );
6178}
6179
Hanno Becker68636192019-02-05 14:36:34 +00006180static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6181 int authmode,
6182 mbedtls_x509_crt *chain,
6183 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006184{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006185 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006187 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006188 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006189
Hanno Becker8927c832019-04-03 12:52:50 +01006190 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6191 void *p_vrfy;
6192
Hanno Becker68636192019-02-05 14:36:34 +00006193 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6194 return( 0 );
6195
Hanno Becker8927c832019-04-03 12:52:50 +01006196 if( ssl->f_vrfy != NULL )
6197 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006199 f_vrfy = ssl->f_vrfy;
6200 p_vrfy = ssl->p_vrfy;
6201 }
6202 else
6203 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006205 f_vrfy = ssl->conf->f_vrfy;
6206 p_vrfy = ssl->conf->p_vrfy;
6207 }
6208
Hanno Becker68636192019-02-05 14:36:34 +00006209 /*
6210 * Main check: verify certificate
6211 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6213 if( ssl->conf->f_ca_cb != NULL )
6214 {
6215 ((void) rs_ctx);
6216 have_ca_chain = 1;
6217
6218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006219 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006220 chain,
6221 ssl->conf->f_ca_cb,
6222 ssl->conf->p_ca_cb,
6223 ssl->conf->cert_profile,
6224 ssl->hostname,
6225 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006226 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006227 }
6228 else
6229#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6230 {
6231 mbedtls_x509_crt *ca_chain;
6232 mbedtls_x509_crl *ca_crl;
6233
6234#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6235 if( ssl->handshake->sni_ca_chain != NULL )
6236 {
6237 ca_chain = ssl->handshake->sni_ca_chain;
6238 ca_crl = ssl->handshake->sni_ca_crl;
6239 }
6240 else
6241#endif
6242 {
6243 ca_chain = ssl->conf->ca_chain;
6244 ca_crl = ssl->conf->ca_crl;
6245 }
6246
6247 if( ca_chain != NULL )
6248 have_ca_chain = 1;
6249
6250 ret = mbedtls_x509_crt_verify_restartable(
6251 chain,
6252 ca_chain, ca_crl,
6253 ssl->conf->cert_profile,
6254 ssl->hostname,
6255 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006256 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006257 }
Hanno Becker68636192019-02-05 14:36:34 +00006258
6259 if( ret != 0 )
6260 {
6261 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6262 }
6263
6264#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6265 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6266 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6267#endif
6268
6269 /*
6270 * Secondary checks: always done, but change 'ret' only if it was 0
6271 */
6272
6273#if defined(MBEDTLS_ECP_C)
6274 {
6275 const mbedtls_pk_context *pk = &chain->pk;
6276
6277 /* If certificate uses an EC key, make sure the curve is OK */
6278 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6279 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6280 {
6281 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6282
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6284 if( ret == 0 )
6285 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6286 }
6287 }
6288#endif /* MBEDTLS_ECP_C */
6289
6290 if( mbedtls_ssl_check_cert_usage( chain,
6291 ciphersuite_info,
6292 ! ssl->conf->endpoint,
6293 &ssl->session_negotiate->verify_result ) != 0 )
6294 {
6295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6296 if( ret == 0 )
6297 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6298 }
6299
6300 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6301 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6302 * with details encoded in the verification flags. All other kinds
6303 * of error codes, including those from the user provided f_vrfy
6304 * functions, are treated as fatal and lead to a failure of
6305 * ssl_parse_certificate even if verification was optional. */
6306 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6307 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6308 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6309 {
6310 ret = 0;
6311 }
6312
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006313 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006314 {
6315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6316 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6317 }
6318
6319 if( ret != 0 )
6320 {
6321 uint8_t alert;
6322
6323 /* The certificate may have been rejected for several reasons.
6324 Pick one and send the corresponding alert. Which alert to send
6325 may be a subject of debate in some cases. */
6326 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6327 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6328 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6329 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6330 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6331 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6332 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6333 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6334 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6335 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6336 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6337 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6338 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6339 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6340 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6341 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6342 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6343 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6344 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6345 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6346 else
6347 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6348 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6349 alert );
6350 }
6351
6352#if defined(MBEDTLS_DEBUG_C)
6353 if( ssl->session_negotiate->verify_result != 0 )
6354 {
6355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6356 ssl->session_negotiate->verify_result ) );
6357 }
6358 else
6359 {
6360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6361 }
6362#endif /* MBEDTLS_DEBUG_C */
6363
6364 return( ret );
6365}
6366
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006367#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6368static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6369 unsigned char *start, size_t len )
6370{
6371 int ret;
6372 /* Remember digest of the peer's end-CRT. */
6373 ssl->session_negotiate->peer_cert_digest =
6374 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6375 if( ssl->session_negotiate->peer_cert_digest == NULL )
6376 {
6377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6378 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6379 mbedtls_ssl_send_alert_message( ssl,
6380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6381 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6382
6383 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6384 }
6385
6386 ret = mbedtls_md( mbedtls_md_info_from_type(
6387 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6388 start, len,
6389 ssl->session_negotiate->peer_cert_digest );
6390
6391 ssl->session_negotiate->peer_cert_digest_type =
6392 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6393 ssl->session_negotiate->peer_cert_digest_len =
6394 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6395
6396 return( ret );
6397}
6398
6399static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6400 unsigned char *start, size_t len )
6401{
6402 unsigned char *end = start + len;
6403 int ret;
6404
6405 /* Make a copy of the peer's raw public key. */
6406 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6407 ret = mbedtls_pk_parse_subpubkey( &start, end,
6408 &ssl->handshake->peer_pubkey );
6409 if( ret != 0 )
6410 {
6411 /* We should have parsed the public key before. */
6412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6413 }
6414
6415 return( 0 );
6416}
6417#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6418
Hanno Becker68636192019-02-05 14:36:34 +00006419int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6420{
6421 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006422 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006423#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6424 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6425 ? ssl->handshake->sni_authmode
6426 : ssl->conf->authmode;
6427#else
6428 const int authmode = ssl->conf->authmode;
6429#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006430 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006431 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006432
6433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6434
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006435 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6436 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006437 {
6438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006439 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006440 }
6441
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006442#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6443 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006444 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006445 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006446 chain = ssl->handshake->ecrs_peer_cert;
6447 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006448 goto crt_verify;
6449 }
6450#endif
6451
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006452 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006453 {
6454 /* mbedtls_ssl_read_record may have sent an alert already. We
6455 let it decide whether to alert. */
6456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006457 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006458 }
6459
Hanno Becker4a55f632019-02-05 12:49:06 +00006460#if defined(MBEDTLS_SSL_SRV_C)
6461 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6462 {
6463 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006464
6465 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006466 ret = 0;
6467 else
6468 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006469
Hanno Becker6bdfab22019-02-05 13:11:17 +00006470 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006471 }
6472#endif /* MBEDTLS_SSL_SRV_C */
6473
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006474 /* Clear existing peer CRT structure in case we tried to
6475 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006476 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006477
6478 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6479 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6482 sizeof( mbedtls_x509_crt ) ) );
6483 mbedtls_ssl_send_alert_message( ssl,
6484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006486
Hanno Becker3dad3112019-02-05 17:19:52 +00006487 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6488 goto exit;
6489 }
6490 mbedtls_x509_crt_init( chain );
6491
6492 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006493 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006494 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006495
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006496#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6497 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006498 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006499
6500crt_verify:
6501 if( ssl->handshake->ecrs_enabled)
6502 rs_ctx = &ssl->handshake->ecrs_ctx;
6503#endif
6504
Hanno Becker68636192019-02-05 14:36:34 +00006505 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006506 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006507 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006508 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006509
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006510#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006511 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006512 unsigned char *crt_start, *pk_start;
6513 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006514
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006515 /* We parse the CRT chain without copying, so
6516 * these pointers point into the input buffer,
6517 * and are hence still valid after freeing the
6518 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006519
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006520 crt_start = chain->raw.p;
6521 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006522
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006523 pk_start = chain->pk_raw.p;
6524 pk_len = chain->pk_raw.len;
6525
6526 /* Free the CRT structures before computing
6527 * digest and copying the peer's public key. */
6528 mbedtls_x509_crt_free( chain );
6529 mbedtls_free( chain );
6530 chain = NULL;
6531
6532 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006533 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006534 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006535
6536 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6537 if( ret != 0 )
6538 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006539 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006540#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6541 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006542 ssl->session_negotiate->peer_cert = chain;
6543 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006544#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006547
Hanno Becker6bdfab22019-02-05 13:11:17 +00006548exit:
6549
Hanno Becker3dad3112019-02-05 17:19:52 +00006550 if( ret == 0 )
6551 ssl->state++;
6552
6553#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6554 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6555 {
6556 ssl->handshake->ecrs_peer_cert = chain;
6557 chain = NULL;
6558 }
6559#endif
6560
6561 if( chain != NULL )
6562 {
6563 mbedtls_x509_crt_free( chain );
6564 mbedtls_free( chain );
6565 }
6566
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 return( ret );
6568}
Hanno Becker21489932019-02-05 13:20:55 +00006569#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006572{
6573 int ret;
6574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006578 ssl->out_msglen = 1;
6579 ssl->out_msg[0] = 1;
6580
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 ssl->state++;
6582
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006583 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 return( ret );
6587 }
6588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006590
6591 return( 0 );
6592}
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006595{
6596 int ret;
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Hanno Becker327c93b2018-08-15 13:56:18 +01006600 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 return( ret );
6604 }
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6610 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 }
6613
Hanno Beckere678eaa2018-08-21 14:57:46 +01006614 /* CCS records are only accepted if they have length 1 and content '1',
6615 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006616
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006617 /*
6618 * Switch to our negotiated transform and session parameters for inbound
6619 * data.
6620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006622 ssl->transform_in = ssl->transform_negotiate;
6623 ssl->session_in = ssl->session_negotiate;
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006629 ssl_dtls_replay_reset( ssl );
6630#endif
6631
6632 /* Increment epoch */
6633 if( ++ssl->in_epoch == 0 )
6634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006636 /* This is highly unlikely to happen for legitimate reasons, so
6637 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006639 }
6640 }
6641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006643 memset( ssl->in_ctr, 0, 8 );
6644
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006645 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6648 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006653 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6654 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006656 }
6657 }
6658#endif
6659
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 ssl->state++;
6661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006663
6664 return( 0 );
6665}
6666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6668 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006669{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006670 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6673 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6674 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006675 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006676 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6679#if defined(MBEDTLS_SHA512_C)
6680 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006681 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6682 else
6683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SHA256_C)
6685 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006686 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006687 else
6688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006692 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006693 }
Paul Bakker380da532012-04-18 16:10:25 +00006694}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6699 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006700 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6701 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6704#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006706 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006707 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6708#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006709 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006710#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006713#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006714 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006715 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006716#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006717 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006718#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006721}
6722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006724 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6727 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006728 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6729 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6732#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006733#if defined(MBEDTLS_USE_PSA_CRYPTO)
6734 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6735#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006736 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006737#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006740#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006741 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006742#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006743 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006744#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006747}
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6750 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6751static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006752 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006753{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006754 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6755 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006756}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006757#endif
Paul Bakker380da532012-04-18 16:10:25 +00006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6760#if defined(MBEDTLS_SHA256_C)
6761static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006762 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006763{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006764#if defined(MBEDTLS_USE_PSA_CRYPTO)
6765 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6766#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006767 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006768#endif
Paul Bakker380da532012-04-18 16:10:25 +00006769}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006770#endif
Paul Bakker380da532012-04-18 16:10:25 +00006771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772#if defined(MBEDTLS_SHA512_C)
6773static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006774 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006775{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006776#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006777 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006778#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006779 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006780#endif
Paul Bakker380da532012-04-18 16:10:25 +00006781}
Paul Bakker769075d2012-11-24 11:26:46 +01006782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006786static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006788{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006789 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006790 mbedtls_md5_context md5;
6791 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006792
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 unsigned char padbuf[48];
6794 unsigned char md5sum[16];
6795 unsigned char sha1sum[20];
6796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006798 if( !session )
6799 session = ssl->session;
6800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006802
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006803 mbedtls_md5_init( &md5 );
6804 mbedtls_sha1_init( &sha1 );
6805
6806 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6807 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
6809 /*
6810 * SSLv3:
6811 * hash =
6812 * MD5( master + pad2 +
6813 * MD5( handshake + sender + master + pad1 ) )
6814 * + SHA1( master + pad2 +
6815 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006816 */
6817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006819 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6820 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006824 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6825 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006826#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006829 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006830
Paul Bakker1ef83d62012-04-11 12:09:53 +00006831 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006833 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6834 mbedtls_md5_update_ret( &md5, session->master, 48 );
6835 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6836 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006838 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6839 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6840 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6841 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Paul Bakker1ef83d62012-04-11 12:09:53 +00006843 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006845 mbedtls_md5_starts_ret( &md5 );
6846 mbedtls_md5_update_ret( &md5, session->master, 48 );
6847 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6848 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6849 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006851 mbedtls_sha1_starts_ret( &sha1 );
6852 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6853 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6854 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6855 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006859 mbedtls_md5_free( &md5 );
6860 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006861
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006862 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6863 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6864 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006871static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006873{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006874 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006875 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006876 mbedtls_md5_context md5;
6877 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006878 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006881 if( !session )
6882 session = ssl->session;
6883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006885
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006886 mbedtls_md5_init( &md5 );
6887 mbedtls_sha1_init( &sha1 );
6888
6889 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6890 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006891
Paul Bakker1ef83d62012-04-11 12:09:53 +00006892 /*
6893 * TLSv1:
6894 * hash = PRF( master, finished_label,
6895 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6896 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006899 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6900 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006901#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006904 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6905 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006906#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006909 ? "client finished"
6910 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006912 mbedtls_md5_finish_ret( &md5, padbuf );
6913 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006915 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006916 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006920 mbedtls_md5_free( &md5 );
6921 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006923 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6930#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006931static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933{
6934 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006935 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006937#if defined(MBEDTLS_USE_PSA_CRYPTO)
6938 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006939 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006940 psa_status_t status;
6941#else
6942 mbedtls_sha256_context sha256;
6943#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006946 if( !session )
6947 session = ssl->session;
6948
Andrzej Kurekeb342242019-01-29 09:14:33 -05006949 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6950 ? "client finished"
6951 : "server finished";
6952
6953#if defined(MBEDTLS_USE_PSA_CRYPTO)
6954 sha256_psa = psa_hash_operation_init();
6955
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6957
6958 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6959 if( status != PSA_SUCCESS )
6960 {
6961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6962 return;
6963 }
6964
6965 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6966 if( status != PSA_SUCCESS )
6967 {
6968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6969 return;
6970 }
6971 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6972#else
6973
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006974 mbedtls_sha256_init( &sha256 );
6975
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006977
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006978 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006979
6980 /*
6981 * TLSv1.2:
6982 * hash = PRF( master, finished_label,
6983 * Hash( handshake ) )[0.11]
6984 */
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if !defined(MBEDTLS_SHA256_ALT)
6987 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006988 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006989#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006990
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006991 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006992 mbedtls_sha256_free( &sha256 );
6993#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006994
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006995 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006996 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006999
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007000 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007007static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007009{
7010 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007011 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007012 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007013#if defined(MBEDTLS_USE_PSA_CRYPTO)
7014 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007015 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007016 psa_status_t status;
7017#else
7018 mbedtls_sha512_context sha512;
7019#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007022 if( !session )
7023 session = ssl->session;
7024
Andrzej Kurekeb342242019-01-29 09:14:33 -05007025 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7026 ? "client finished"
7027 : "server finished";
7028
7029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007030 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007031
7032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7033
Andrzej Kurek972fba52019-01-30 03:29:12 -05007034 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007035 if( status != PSA_SUCCESS )
7036 {
7037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7038 return;
7039 }
7040
Andrzej Kurek972fba52019-01-30 03:29:12 -05007041 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007042 if( status != PSA_SUCCESS )
7043 {
7044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7045 return;
7046 }
7047 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7048#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007049 mbedtls_sha512_init( &sha512 );
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007052
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007053 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007054
7055 /*
7056 * TLSv1.2:
7057 * hash = PRF( master, finished_label,
7058 * Hash( handshake ) )[0.11]
7059 */
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007062 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7063 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007064#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007065
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007066 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007067 mbedtls_sha512_free( &sha512 );
7068#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007069
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007070 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007071 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007075 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#endif /* MBEDTLS_SHA512_C */
7080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007085
7086 /*
7087 * Free our handshake params
7088 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007089 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007091 ssl->handshake = NULL;
7092
7093 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007094 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007095 */
7096 if( ssl->transform )
7097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 mbedtls_ssl_transform_free( ssl->transform );
7099 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007100 }
7101 ssl->transform = ssl->transform_negotiate;
7102 ssl->transform_negotiate = NULL;
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007105}
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007108{
7109 int resume = ssl->handshake->resume;
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_RENEGOTIATION)
7114 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007117 ssl->renego_records_seen = 0;
7118 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007119#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007120
7121 /*
7122 * Free the previous session and switch in the current one
7123 */
Paul Bakker0a597072012-09-25 21:55:46 +00007124 if( ssl->session )
7125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007127 /* RFC 7366 3.1: keep the EtM state */
7128 ssl->session_negotiate->encrypt_then_mac =
7129 ssl->session->encrypt_then_mac;
7130#endif
7131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 mbedtls_ssl_session_free( ssl->session );
7133 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007134 }
7135 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007136 ssl->session_negotiate = NULL;
7137
Paul Bakker0a597072012-09-25 21:55:46 +00007138 /*
7139 * Add cache entry
7140 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007141 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007142 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007143 resume == 0 )
7144 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007145 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007147 }
Paul Bakker0a597072012-09-25 21:55:46 +00007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007151 ssl->handshake->flight != NULL )
7152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007153 /* Cancel handshake timer */
7154 ssl_set_timer( ssl, 0 );
7155
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007156 /* Keep last flight around in case we need to resend it:
7157 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007159 }
7160 else
7161#endif
7162 ssl_handshake_wrapup_free_hs_transform( ssl );
7163
Paul Bakker48916f92012-09-16 19:57:18 +00007164 ssl->state++;
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007167}
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007171 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007175 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007177 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007179 /*
7180 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7181 * may define some other value. Currently (early 2016), no defined
7182 * ciphersuite does this (and this is unlikely to change as activity has
7183 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007188 ssl->verify_data_len = hash_len;
7189 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007190#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007191
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7194 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 /*
7197 * In case of session resuming, invert the client and server
7198 * ChangeCipherSpec messages order.
7199 */
Paul Bakker0a597072012-09-25 21:55:46 +00007200 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007203 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007207 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007210 }
7211 else
7212 ssl->state++;
7213
Paul Bakker48916f92012-09-16 19:57:18 +00007214 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007215 * Switch to our negotiated transform and session parameters for outbound
7216 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007222 {
7223 unsigned char i;
7224
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007225 /* Remember current epoch settings for resending */
7226 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007227 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007228
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007229 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007230 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007231
7232 /* Increment epoch */
7233 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007234 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007235 break;
7236
7237 /* The loop goes to its end iff the counter is wrapping */
7238 if( i == 0 )
7239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7241 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007242 }
7243 }
7244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007246 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007247
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007248 ssl->transform_out = ssl->transform_negotiate;
7249 ssl->session_out = ssl->session_negotiate;
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7252 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7257 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007258 }
7259 }
7260#endif
7261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007263 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007265#endif
7266
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007267 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007268 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007269 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 return( ret );
7271 }
7272
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007273#if defined(MBEDTLS_SSL_PROTO_DTLS)
7274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7275 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7276 {
7277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7278 return( ret );
7279 }
7280#endif
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283
7284 return( 0 );
7285}
7286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007288#define SSL_MAX_HASH_LEN 36
7289#else
7290#define SSL_MAX_HASH_LEN 12
7291#endif
7292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007294{
Paul Bakker23986e52011-04-24 08:57:21 +00007295 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007296 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007297 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007300
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007301 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007302
Hanno Becker327c93b2018-08-15 13:56:18 +01007303 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007306 return( ret );
7307 }
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007312 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7313 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007315 }
7316
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007317 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_PROTO_SSL3)
7319 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007320 hash_len = 36;
7321 else
7322#endif
7323 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7326 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7330 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007332 }
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007335 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 }
7342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007344 ssl->verify_data_len = hash_len;
7345 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007346#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007347
Paul Bakker0a597072012-09-25 21:55:46 +00007348 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007351 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007357#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 }
7359 else
7360 ssl->state++;
7361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007365#endif
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
7369 return( 0 );
7370}
7371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7377 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7378 mbedtls_md5_init( &handshake->fin_md5 );
7379 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007380 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7381 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7384#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007385#if defined(MBEDTLS_USE_PSA_CRYPTO)
7386 handshake->fin_sha256_psa = psa_hash_operation_init();
7387 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7388#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007390 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007394#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007395 handshake->fin_sha384_psa = psa_hash_operation_init();
7396 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007397#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007399 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007400#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007403
7404 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007405
7406#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7407 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7408 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7409#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_DHM_C)
7412 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_ECDH_C)
7415 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007416#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007417#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007418 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007419#if defined(MBEDTLS_SSL_CLI_C)
7420 handshake->ecjpake_cache = NULL;
7421 handshake->ecjpake_cache_len = 0;
7422#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007423#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007424
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007425#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007426 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007427#endif
7428
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007429#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7430 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7431#endif
Hanno Becker75173122019-02-06 16:18:31 +00007432
7433#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7434 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7435 mbedtls_pk_init( &handshake->peer_pubkey );
7436#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437}
7438
Hanno Beckera18d1322018-01-03 14:27:32 +00007439void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7444 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007445
Hanno Beckerd56ed242018-01-03 15:32:51 +00007446#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 mbedtls_md_init( &transform->md_ctx_enc );
7448 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007449#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007450}
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007455}
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007458{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007459 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007460 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007462 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007464 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007465 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007466
7467 /*
7468 * Either the pointers are now NULL or cleared properly and can be freed.
7469 * Now allocate missing structures.
7470 */
7471 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007472 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007473 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007474 }
Paul Bakker48916f92012-09-16 19:57:18 +00007475
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007476 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007477 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007478 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007479 }
Paul Bakker48916f92012-09-16 19:57:18 +00007480
Paul Bakker82788fb2014-10-20 13:59:19 +02007481 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007483 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007484 }
Paul Bakker48916f92012-09-16 19:57:18 +00007485
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007486 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007487 if( ssl->handshake == NULL ||
7488 ssl->transform_negotiate == NULL ||
7489 ssl->session_negotiate == NULL )
7490 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 mbedtls_free( ssl->handshake );
7494 mbedtls_free( ssl->transform_negotiate );
7495 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007496
7497 ssl->handshake = NULL;
7498 ssl->transform_negotiate = NULL;
7499 ssl->session_negotiate = NULL;
7500
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007502 }
7503
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007504 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007506 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007507 ssl_handshake_params_init( ssl->handshake );
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7511 {
7512 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007513
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007514 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7515 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7516 else
7517 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007518
7519 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007520 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007521#endif
7522
Paul Bakker48916f92012-09-16 19:57:18 +00007523 return( 0 );
7524}
7525
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007526#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007527/* Dummy cookie callbacks for defaults */
7528static int ssl_cookie_write_dummy( void *ctx,
7529 unsigned char **p, unsigned char *end,
7530 const unsigned char *cli_id, size_t cli_id_len )
7531{
7532 ((void) ctx);
7533 ((void) p);
7534 ((void) end);
7535 ((void) cli_id);
7536 ((void) cli_id_len);
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007539}
7540
7541static int ssl_cookie_check_dummy( void *ctx,
7542 const unsigned char *cookie, size_t cookie_len,
7543 const unsigned char *cli_id, size_t cli_id_len )
7544{
7545 ((void) ctx);
7546 ((void) cookie);
7547 ((void) cookie_len);
7548 ((void) cli_id);
7549 ((void) cli_id_len);
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007552}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007553#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007554
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007555/* Once ssl->out_hdr as the address of the beginning of the
7556 * next outgoing record is set, deduce the other pointers.
7557 *
7558 * Note: For TLS, we save the implicit record sequence number
7559 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7560 * and the caller has to make sure there's space for this.
7561 */
7562
7563static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7564 mbedtls_ssl_transform *transform )
7565{
7566#if defined(MBEDTLS_SSL_PROTO_DTLS)
7567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7568 {
7569 ssl->out_ctr = ssl->out_hdr + 3;
7570 ssl->out_len = ssl->out_hdr + 11;
7571 ssl->out_iv = ssl->out_hdr + 13;
7572 }
7573 else
7574#endif
7575 {
7576 ssl->out_ctr = ssl->out_hdr - 8;
7577 ssl->out_len = ssl->out_hdr + 3;
7578 ssl->out_iv = ssl->out_hdr + 5;
7579 }
7580
7581 /* Adjust out_msg to make space for explicit IV, if used. */
7582 if( transform != NULL &&
7583 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7584 {
7585 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7586 }
7587 else
7588 ssl->out_msg = ssl->out_iv;
7589}
7590
7591/* Once ssl->in_hdr as the address of the beginning of the
7592 * next incoming record is set, deduce the other pointers.
7593 *
7594 * Note: For TLS, we save the implicit record sequence number
7595 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7596 * and the caller has to make sure there's space for this.
7597 */
7598
7599static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7600 mbedtls_ssl_transform *transform )
7601{
7602#if defined(MBEDTLS_SSL_PROTO_DTLS)
7603 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7604 {
7605 ssl->in_ctr = ssl->in_hdr + 3;
7606 ssl->in_len = ssl->in_hdr + 11;
7607 ssl->in_iv = ssl->in_hdr + 13;
7608 }
7609 else
7610#endif
7611 {
7612 ssl->in_ctr = ssl->in_hdr - 8;
7613 ssl->in_len = ssl->in_hdr + 3;
7614 ssl->in_iv = ssl->in_hdr + 5;
7615 }
7616
7617 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7618 if( transform != NULL &&
7619 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7620 {
7621 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7622 }
7623 else
7624 ssl->in_msg = ssl->in_iv;
7625}
7626
Paul Bakker5121ce52009-01-03 21:22:43 +00007627/*
7628 * Initialize an SSL context
7629 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007630void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7631{
7632 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7633}
7634
7635/*
7636 * Setup an SSL context
7637 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007638
7639static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7640{
7641 /* Set the incoming and outgoing record pointers. */
7642#if defined(MBEDTLS_SSL_PROTO_DTLS)
7643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7644 {
7645 ssl->out_hdr = ssl->out_buf;
7646 ssl->in_hdr = ssl->in_buf;
7647 }
7648 else
7649#endif /* MBEDTLS_SSL_PROTO_DTLS */
7650 {
7651 ssl->out_hdr = ssl->out_buf + 8;
7652 ssl->in_hdr = ssl->in_buf + 8;
7653 }
7654
7655 /* Derive other internal pointers. */
7656 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7657 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7658}
7659
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007660int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007661 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007662{
Paul Bakker48916f92012-09-16 19:57:18 +00007663 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007664
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007665 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007666
7667 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007668 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007669 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007670
7671 /* Set to NULL in case of an error condition */
7672 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007673
Angus Grattond8213d02016-05-25 20:56:48 +10007674 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7675 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007676 {
Angus Grattond8213d02016-05-25 20:56:48 +10007677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007678 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007679 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007680 }
7681
7682 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7683 if( ssl->out_buf == NULL )
7684 {
7685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007686 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007687 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007688 }
7689
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007690 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007691
Paul Bakker48916f92012-09-16 19:57:18 +00007692 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007693 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007694
7695 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007696
7697error:
7698 mbedtls_free( ssl->in_buf );
7699 mbedtls_free( ssl->out_buf );
7700
7701 ssl->conf = NULL;
7702
7703 ssl->in_buf = NULL;
7704 ssl->out_buf = NULL;
7705
7706 ssl->in_hdr = NULL;
7707 ssl->in_ctr = NULL;
7708 ssl->in_len = NULL;
7709 ssl->in_iv = NULL;
7710 ssl->in_msg = NULL;
7711
7712 ssl->out_hdr = NULL;
7713 ssl->out_ctr = NULL;
7714 ssl->out_len = NULL;
7715 ssl->out_iv = NULL;
7716 ssl->out_msg = NULL;
7717
k-stachowiak9f7798e2018-07-31 16:52:32 +02007718 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007719}
7720
7721/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007722 * Reset an initialized and used SSL context for re-use while retaining
7723 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007724 *
7725 * If partial is non-zero, keep data in the input buffer and client ID.
7726 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007727 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007728static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007729{
Paul Bakker48916f92012-09-16 19:57:18 +00007730 int ret;
7731
Hanno Becker7e772132018-08-10 12:38:21 +01007732#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7733 !defined(MBEDTLS_SSL_SRV_C)
7734 ((void) partial);
7735#endif
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007738
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007739 /* Cancel any possibly running timer */
7740 ssl_set_timer( ssl, 0 );
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_RENEGOTIATION)
7743 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007744 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007745
7746 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7748 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007749#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007751
Paul Bakker7eb013f2011-10-06 12:37:39 +00007752 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007753 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007754
7755 ssl->in_msgtype = 0;
7756 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007758 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007759 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007762 ssl_dtls_replay_reset( ssl );
7763#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007764
7765 ssl->in_hslen = 0;
7766 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007767
7768 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007769
7770 ssl->out_msgtype = 0;
7771 ssl->out_msglen = 0;
7772 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7774 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007775 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007776#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007777
Hanno Becker19859472018-08-06 09:40:20 +01007778 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7779
Paul Bakker48916f92012-09-16 19:57:18 +00007780 ssl->transform_in = NULL;
7781 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007782
Hanno Becker78640902018-08-13 16:35:15 +01007783 ssl->session_in = NULL;
7784 ssl->session_out = NULL;
7785
Angus Grattond8213d02016-05-25 20:56:48 +10007786 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007787
7788#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007789 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007790#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7791 {
7792 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007793 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007794 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7797 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7800 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7803 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007804 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007805 }
7806#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007807
Paul Bakker48916f92012-09-16 19:57:18 +00007808 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 mbedtls_ssl_transform_free( ssl->transform );
7811 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007812 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007813 }
Paul Bakker48916f92012-09-16 19:57:18 +00007814
Paul Bakkerc0463502013-02-14 11:19:38 +01007815 if( ssl->session )
7816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 mbedtls_ssl_session_free( ssl->session );
7818 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007819 ssl->session = NULL;
7820 }
7821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007823 ssl->alpn_chosen = NULL;
7824#endif
7825
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007826#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007827#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007828 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007829#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007830 {
7831 mbedtls_free( ssl->cli_id );
7832 ssl->cli_id = NULL;
7833 ssl->cli_id_len = 0;
7834 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007835#endif
7836
Paul Bakker48916f92012-09-16 19:57:18 +00007837 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7838 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007839
7840 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007841}
7842
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007843/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007844 * Reset an initialized and used SSL context for re-use while retaining
7845 * all application-set variables, function pointers and data.
7846 */
7847int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7848{
7849 return( ssl_session_reset_int( ssl, 0 ) );
7850}
7851
7852/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007853 * SSL set accessors
7854 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007855void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007856{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007857 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007858}
7859
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007860void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007861{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007862 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007863}
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007866void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007868 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007869}
7870#endif
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007873void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007874{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007875 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007876}
7877#endif
7878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007880
Hanno Becker1841b0a2018-08-24 11:13:57 +01007881void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7882 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007883{
7884 ssl->disable_datagram_packing = !allow_packing;
7885}
7886
7887void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7888 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->hs_timeout_min = min;
7891 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007892}
7893#endif
7894
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007895void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007897 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007898}
7899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007901void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007902 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007903 void *p_vrfy )
7904{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007905 conf->f_vrfy = f_vrfy;
7906 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007909
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007910void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007911 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 void *p_rng )
7913{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007914 conf->f_rng = f_rng;
7915 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007916}
7917
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007918void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007919 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007920 void *p_dbg )
7921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->f_dbg = f_dbg;
7923 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007924}
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007927 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007928 mbedtls_ssl_send_t *f_send,
7929 mbedtls_ssl_recv_t *f_recv,
7930 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007931{
7932 ssl->p_bio = p_bio;
7933 ssl->f_send = f_send;
7934 ssl->f_recv = f_recv;
7935 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007936}
7937
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007938#if defined(MBEDTLS_SSL_PROTO_DTLS)
7939void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7940{
7941 ssl->mtu = mtu;
7942}
7943#endif
7944
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007945void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007946{
7947 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007948}
7949
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007950void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7951 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007952 mbedtls_ssl_set_timer_t *f_set_timer,
7953 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007954{
7955 ssl->p_timer = p_timer;
7956 ssl->f_set_timer = f_set_timer;
7957 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007958
7959 /* Make sure we start with no timer running */
7960 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007961}
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007964void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007965 void *p_cache,
7966 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7967 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007968{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007969 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007970 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007971 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_CLI_C)
7976int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007977{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007978 int ret;
7979
7980 if( ssl == NULL ||
7981 session == NULL ||
7982 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007983 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007986 }
7987
Hanno Becker52055ae2019-02-06 14:30:46 +00007988 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7989 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007990 return( ret );
7991
Paul Bakker0a597072012-09-25 21:55:46 +00007992 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007993
7994 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007995}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007997
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007998void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007999 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008001 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8002 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8003 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8004 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008005}
8006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008007void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008008 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008009 int major, int minor )
8010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008012 return;
8013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008015 return;
8016
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008017 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008018}
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008021void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008022 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008023{
8024 conf->cert_profile = profile;
8025}
8026
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008027/* Append a new keycert entry to a (possibly empty) list */
8028static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8029 mbedtls_x509_crt *cert,
8030 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008031{
niisato8ee24222018-06-25 19:05:48 +09008032 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008033
niisato8ee24222018-06-25 19:05:48 +09008034 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8035 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008036 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008037
niisato8ee24222018-06-25 19:05:48 +09008038 new_cert->cert = cert;
8039 new_cert->key = key;
8040 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008041
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008042 /* Update head is the list was null, else add to the end */
8043 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008044 {
niisato8ee24222018-06-25 19:05:48 +09008045 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008046 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008047 else
8048 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008049 mbedtls_ssl_key_cert *cur = *head;
8050 while( cur->next != NULL )
8051 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008052 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008053 }
8054
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008055 return( 0 );
8056}
8057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008058int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008059 mbedtls_x509_crt *own_cert,
8060 mbedtls_pk_context *pk_key )
8061{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008062 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008063}
8064
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008065void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008066 mbedtls_x509_crt *ca_chain,
8067 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008068{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008069 conf->ca_chain = ca_chain;
8070 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008071
8072#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8073 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8074 * cannot be used together. */
8075 conf->f_ca_cb = NULL;
8076 conf->p_ca_cb = NULL;
8077#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008078}
Hanno Becker5adaad92019-03-27 16:54:37 +00008079
8080#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8081void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008082 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008083 void *p_ca_cb )
8084{
8085 conf->f_ca_cb = f_ca_cb;
8086 conf->p_ca_cb = p_ca_cb;
8087
8088 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8089 * cannot be used together. */
8090 conf->ca_chain = NULL;
8091 conf->ca_crl = NULL;
8092}
8093#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008095
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008096#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8097int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8098 mbedtls_x509_crt *own_cert,
8099 mbedtls_pk_context *pk_key )
8100{
8101 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8102 own_cert, pk_key ) );
8103}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008104
8105void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8106 mbedtls_x509_crt *ca_chain,
8107 mbedtls_x509_crl *ca_crl )
8108{
8109 ssl->handshake->sni_ca_chain = ca_chain;
8110 ssl->handshake->sni_ca_crl = ca_crl;
8111}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008112
8113void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8114 int authmode )
8115{
8116 ssl->handshake->sni_authmode = authmode;
8117}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008118#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8119
Hanno Becker8927c832019-04-03 12:52:50 +01008120#if defined(MBEDTLS_X509_CRT_PARSE_C)
8121void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8122 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8123 void *p_vrfy )
8124{
8125 ssl->f_vrfy = f_vrfy;
8126 ssl->p_vrfy = p_vrfy;
8127}
8128#endif
8129
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008130#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008131/*
8132 * Set EC J-PAKE password for current handshake
8133 */
8134int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8135 const unsigned char *pw,
8136 size_t pw_len )
8137{
8138 mbedtls_ecjpake_role role;
8139
Janos Follath8eb64132016-06-03 15:40:57 +01008140 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8142
8143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8144 role = MBEDTLS_ECJPAKE_SERVER;
8145 else
8146 role = MBEDTLS_ECJPAKE_CLIENT;
8147
8148 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8149 role,
8150 MBEDTLS_MD_SHA256,
8151 MBEDTLS_ECP_DP_SECP256R1,
8152 pw, pw_len ) );
8153}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008154#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008157
8158static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8159{
8160 /* Remove reference to existing PSK, if any. */
8161#if defined(MBEDTLS_USE_PSA_CRYPTO)
8162 if( conf->psk_opaque != 0 )
8163 {
8164 /* The maintenance of the PSK key slot is the
8165 * user's responsibility. */
8166 conf->psk_opaque = 0;
8167 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008168 /* This and the following branch should never
8169 * be taken simultaenously as we maintain the
8170 * invariant that raw and opaque PSKs are never
8171 * configured simultaneously. As a safeguard,
8172 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008173#endif /* MBEDTLS_USE_PSA_CRYPTO */
8174 if( conf->psk != NULL )
8175 {
8176 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8177
8178 mbedtls_free( conf->psk );
8179 conf->psk = NULL;
8180 conf->psk_len = 0;
8181 }
8182
8183 /* Remove reference to PSK identity, if any. */
8184 if( conf->psk_identity != NULL )
8185 {
8186 mbedtls_free( conf->psk_identity );
8187 conf->psk_identity = NULL;
8188 conf->psk_identity_len = 0;
8189 }
8190}
8191
Hanno Becker7390c712018-11-15 13:33:04 +00008192/* This function assumes that PSK identity in the SSL config is unset.
8193 * It checks that the provided identity is well-formed and attempts
8194 * to make a copy of it in the SSL config.
8195 * On failure, the PSK identity in the config remains unset. */
8196static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8197 unsigned char const *psk_identity,
8198 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008199{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008200 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008201 if( psk_identity == NULL ||
8202 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008203 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008204 {
8205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8206 }
8207
Hanno Becker7390c712018-11-15 13:33:04 +00008208 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8209 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008210 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008211
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008212 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008213 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008214
8215 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008216}
8217
Hanno Becker7390c712018-11-15 13:33:04 +00008218int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8219 const unsigned char *psk, size_t psk_len,
8220 const unsigned char *psk_identity, size_t psk_identity_len )
8221{
8222 int ret;
8223 /* Remove opaque/raw PSK + PSK Identity */
8224 ssl_conf_remove_psk( conf );
8225
8226 /* Check and set raw PSK */
8227 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8229 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8230 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8231 conf->psk_len = psk_len;
8232 memcpy( conf->psk, psk, conf->psk_len );
8233
8234 /* Check and set PSK Identity */
8235 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8236 if( ret != 0 )
8237 ssl_conf_remove_psk( conf );
8238
8239 return( ret );
8240}
8241
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008242static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8243{
8244#if defined(MBEDTLS_USE_PSA_CRYPTO)
8245 if( ssl->handshake->psk_opaque != 0 )
8246 {
8247 ssl->handshake->psk_opaque = 0;
8248 }
8249 else
8250#endif /* MBEDTLS_USE_PSA_CRYPTO */
8251 if( ssl->handshake->psk != NULL )
8252 {
8253 mbedtls_platform_zeroize( ssl->handshake->psk,
8254 ssl->handshake->psk_len );
8255 mbedtls_free( ssl->handshake->psk );
8256 ssl->handshake->psk_len = 0;
8257 }
8258}
8259
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008260int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8261 const unsigned char *psk, size_t psk_len )
8262{
8263 if( psk == NULL || ssl->handshake == NULL )
8264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8265
8266 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8268
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008269 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008270
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008271 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008272 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008273
8274 ssl->handshake->psk_len = psk_len;
8275 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8276
8277 return( 0 );
8278}
8279
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008280#if defined(MBEDTLS_USE_PSA_CRYPTO)
8281int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008282 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008283 const unsigned char *psk_identity,
8284 size_t psk_identity_len )
8285{
Hanno Becker7390c712018-11-15 13:33:04 +00008286 int ret;
8287 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008288 ssl_conf_remove_psk( conf );
8289
Hanno Becker7390c712018-11-15 13:33:04 +00008290 /* Check and set opaque PSK */
8291 if( psk_slot == 0 )
8292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008293 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008294
8295 /* Check and set PSK Identity */
8296 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8297 psk_identity_len );
8298 if( ret != 0 )
8299 ssl_conf_remove_psk( conf );
8300
8301 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008302}
8303
8304int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008305 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008306{
8307 if( psk_slot == 0 || ssl->handshake == NULL )
8308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8309
8310 ssl_remove_psk( ssl );
8311 ssl->handshake->psk_opaque = psk_slot;
8312 return( 0 );
8313}
8314#endif /* MBEDTLS_USE_PSA_CRYPTO */
8315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008316void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008318 size_t),
8319 void *p_psk )
8320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008321 conf->f_psk = f_psk;
8322 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008325
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008326#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008327
8328#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008329int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008330{
8331 int ret;
8332
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008333 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8334 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8335 {
8336 mbedtls_mpi_free( &conf->dhm_P );
8337 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008340
8341 return( 0 );
8342}
Hanno Becker470a8c42017-10-04 15:28:46 +01008343#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008344
Hanno Beckera90658f2017-10-04 15:29:08 +01008345int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8346 const unsigned char *dhm_P, size_t P_len,
8347 const unsigned char *dhm_G, size_t G_len )
8348{
8349 int ret;
8350
8351 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8352 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8353 {
8354 mbedtls_mpi_free( &conf->dhm_P );
8355 mbedtls_mpi_free( &conf->dhm_G );
8356 return( ret );
8357 }
8358
8359 return( 0 );
8360}
Paul Bakker5121ce52009-01-03 21:22:43 +00008361
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008362int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008363{
8364 int ret;
8365
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008366 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8367 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8368 {
8369 mbedtls_mpi_free( &conf->dhm_P );
8370 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008371 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008372 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008373
8374 return( 0 );
8375}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008376#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008377
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008378#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8379/*
8380 * Set the minimum length for Diffie-Hellman parameters
8381 */
8382void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8383 unsigned int bitlen )
8384{
8385 conf->dhm_min_bitlen = bitlen;
8386}
8387#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8388
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008389#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008390/*
8391 * Set allowed/preferred hashes for handshake signatures
8392 */
8393void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8394 const int *hashes )
8395{
8396 conf->sig_hashes = hashes;
8397}
Hanno Becker947194e2017-04-07 13:25:49 +01008398#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008399
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008400#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008401/*
8402 * Set the allowed elliptic curves
8403 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008404void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008408}
Hanno Becker947194e2017-04-07 13:25:49 +01008409#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008410
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008411#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008413{
Hanno Becker947194e2017-04-07 13:25:49 +01008414 /* Initialize to suppress unnecessary compiler warning */
8415 size_t hostname_len = 0;
8416
8417 /* Check if new hostname is valid before
8418 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008419 if( hostname != NULL )
8420 {
8421 hostname_len = strlen( hostname );
8422
8423 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8425 }
8426
8427 /* Now it's clear that we will overwrite the old hostname,
8428 * so we can free it safely */
8429
8430 if( ssl->hostname != NULL )
8431 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008432 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008433 mbedtls_free( ssl->hostname );
8434 }
8435
8436 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008437
Paul Bakker5121ce52009-01-03 21:22:43 +00008438 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008439 {
8440 ssl->hostname = NULL;
8441 }
8442 else
8443 {
8444 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008445 if( ssl->hostname == NULL )
8446 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008447
Hanno Becker947194e2017-04-07 13:25:49 +01008448 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008449
Hanno Becker947194e2017-04-07 13:25:49 +01008450 ssl->hostname[hostname_len] = '\0';
8451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008452
8453 return( 0 );
8454}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008455#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008456
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008457#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008460 const unsigned char *, size_t),
8461 void *p_sni )
8462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008463 conf->f_sni = f_sni;
8464 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008469int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008470{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008471 size_t cur_len, tot_len;
8472 const char **p;
8473
8474 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008475 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8476 * MUST NOT be truncated."
8477 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008478 */
8479 tot_len = 0;
8480 for( p = protos; *p != NULL; p++ )
8481 {
8482 cur_len = strlen( *p );
8483 tot_len += cur_len;
8484
8485 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008487 }
8488
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008489 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008490
8491 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008492}
8493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008495{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008496 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008499
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008500void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->max_major_ver = major;
8503 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008504}
8505
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008506void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008507{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008508 conf->min_major_ver = major;
8509 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008510}
8511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008513void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008514{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008515 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008516}
8517#endif
8518
Janos Follath088ce432017-04-10 12:42:31 +01008519#if defined(MBEDTLS_SSL_SRV_C)
8520void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8521 char cert_req_ca_list )
8522{
8523 conf->cert_req_ca_list = cert_req_ca_list;
8524}
8525#endif
8526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008528void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008529{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008530 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008531}
8532#endif
8533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008535void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008536{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008537 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008538}
8539#endif
8540
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008541#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008542void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008545}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008546#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008549int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008552 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008555 }
8556
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008557 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008558
8559 return( 0 );
8560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008564void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008566 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008571void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008572{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008573 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008574}
8575#endif
8576
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008577void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008579 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008580}
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008583void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008586}
8587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008588void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008590 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008591}
8592
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008593void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008594 const unsigned char period[8] )
8595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008601#if defined(MBEDTLS_SSL_CLI_C)
8602void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008603{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008604 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008605}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008606#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008607
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008608#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008609void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8610 mbedtls_ssl_ticket_write_t *f_ticket_write,
8611 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8612 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008613{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008614 conf->f_ticket_write = f_ticket_write;
8615 conf->f_ticket_parse = f_ticket_parse;
8616 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008617}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008620
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008621#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8622void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8623 mbedtls_ssl_export_keys_t *f_export_keys,
8624 void *p_export_keys )
8625{
8626 conf->f_export_keys = f_export_keys;
8627 conf->p_export_keys = p_export_keys;
8628}
8629#endif
8630
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008631#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008632void mbedtls_ssl_conf_async_private_cb(
8633 mbedtls_ssl_config *conf,
8634 mbedtls_ssl_async_sign_t *f_async_sign,
8635 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8636 mbedtls_ssl_async_resume_t *f_async_resume,
8637 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008638 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008639{
8640 conf->f_async_sign_start = f_async_sign;
8641 conf->f_async_decrypt_start = f_async_decrypt;
8642 conf->f_async_resume = f_async_resume;
8643 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008644 conf->p_async_config_data = async_config_data;
8645}
8646
Gilles Peskine8f97af72018-04-26 11:46:10 +02008647void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8648{
8649 return( conf->p_async_config_data );
8650}
8651
Gilles Peskine1febfef2018-04-30 11:54:39 +02008652void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008653{
8654 if( ssl->handshake == NULL )
8655 return( NULL );
8656 else
8657 return( ssl->handshake->user_async_ctx );
8658}
8659
Gilles Peskine1febfef2018-04-30 11:54:39 +02008660void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008661 void *ctx )
8662{
8663 if( ssl->handshake != NULL )
8664 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008665}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008666#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008667
Paul Bakker5121ce52009-01-03 21:22:43 +00008668/*
8669 * SSL get accessors
8670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008672{
8673 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8674}
8675
Hanno Becker8b170a02017-10-10 11:51:19 +01008676int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8677{
8678 /*
8679 * Case A: We're currently holding back
8680 * a message for further processing.
8681 */
8682
8683 if( ssl->keep_current_message == 1 )
8684 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008686 return( 1 );
8687 }
8688
8689 /*
8690 * Case B: Further records are pending in the current datagram.
8691 */
8692
8693#if defined(MBEDTLS_SSL_PROTO_DTLS)
8694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8695 ssl->in_left > ssl->next_record_offset )
8696 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008698 return( 1 );
8699 }
8700#endif /* MBEDTLS_SSL_PROTO_DTLS */
8701
8702 /*
8703 * Case C: A handshake message is being processed.
8704 */
8705
Hanno Becker8b170a02017-10-10 11:51:19 +01008706 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8707 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008709 return( 1 );
8710 }
8711
8712 /*
8713 * Case D: An application data message is being processed
8714 */
8715 if( ssl->in_offt != NULL )
8716 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008718 return( 1 );
8719 }
8720
8721 /*
8722 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008723 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008724 * we implement support for multiple alerts in single records.
8725 */
8726
8727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8728 return( 0 );
8729}
8730
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008731uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008732{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008733 if( ssl->session != NULL )
8734 return( ssl->session->verify_result );
8735
8736 if( ssl->session_negotiate != NULL )
8737 return( ssl->session_negotiate->verify_result );
8738
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008739 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008740}
8741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008743{
Paul Bakker926c8e42013-03-06 10:23:34 +01008744 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008745 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008748}
8749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008754 {
8755 switch( ssl->minor_ver )
8756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008758 return( "DTLSv1.0" );
8759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008761 return( "DTLSv1.2" );
8762
8763 default:
8764 return( "unknown (DTLS)" );
8765 }
8766 }
8767#endif
8768
Paul Bakker43ca69c2011-01-15 17:35:19 +00008769 switch( ssl->minor_ver )
8770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008772 return( "SSLv3.0" );
8773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008775 return( "TLSv1.0" );
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008778 return( "TLSv1.1" );
8779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008781 return( "TLSv1.2" );
8782
Paul Bakker43ca69c2011-01-15 17:35:19 +00008783 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008784 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008785 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008786}
8787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008789{
Hanno Becker3136ede2018-08-17 15:28:19 +01008790 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008792 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008793
Hanno Becker78640902018-08-13 16:35:15 +01008794 if( transform == NULL )
8795 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#if defined(MBEDTLS_ZLIB_SUPPORT)
8798 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8799 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008800#endif
8801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 case MBEDTLS_MODE_GCM:
8805 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008806 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008808 transform_expansion = transform->minlen;
8809 break;
8810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008812
8813 block_size = mbedtls_cipher_get_block_size(
8814 &transform->cipher_ctx_enc );
8815
Hanno Becker3136ede2018-08-17 15:28:19 +01008816 /* Expansion due to the addition of the MAC. */
8817 transform_expansion += transform->maclen;
8818
8819 /* Expansion due to the addition of CBC padding;
8820 * Theoretically up to 256 bytes, but we never use
8821 * more than the block size of the underlying cipher. */
8822 transform_expansion += block_size;
8823
8824 /* For TLS 1.1 or higher, an explicit IV is added
8825 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008826#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8827 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008828 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008829#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008830
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008831 break;
8832
8833 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008836 }
8837
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008838 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008839}
8840
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008841#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8842size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8843{
8844 size_t max_len;
8845
8846 /*
8847 * Assume mfl_code is correct since it was checked when set
8848 */
Angus Grattond8213d02016-05-25 20:56:48 +10008849 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008850
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008851 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008852 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008853 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008854 {
Angus Grattond8213d02016-05-25 20:56:48 +10008855 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008856 }
8857
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008858 /* During a handshake, use the value being negotiated */
8859 if( ssl->session_negotiate != NULL &&
8860 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8861 {
8862 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8863 }
8864
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008865 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008866}
8867#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8868
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008869#if defined(MBEDTLS_SSL_PROTO_DTLS)
8870static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8871{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008872 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8874 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8875 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8876 return ( 0 );
8877
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008878 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8879 return( ssl->mtu );
8880
8881 if( ssl->mtu == 0 )
8882 return( ssl->handshake->mtu );
8883
8884 return( ssl->mtu < ssl->handshake->mtu ?
8885 ssl->mtu : ssl->handshake->mtu );
8886}
8887#endif /* MBEDTLS_SSL_PROTO_DTLS */
8888
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008889int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8890{
8891 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8892
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008893#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8894 !defined(MBEDTLS_SSL_PROTO_DTLS)
8895 (void) ssl;
8896#endif
8897
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008898#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8899 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8900
8901 if( max_len > mfl )
8902 max_len = mfl;
8903#endif
8904
8905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008906 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008907 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008908 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008909 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8910 const size_t overhead = (size_t) ret;
8911
8912 if( ret < 0 )
8913 return( ret );
8914
8915 if( mtu <= overhead )
8916 {
8917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8918 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8919 }
8920
8921 if( max_len > mtu - overhead )
8922 max_len = mtu - overhead;
8923 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008925
Hanno Becker0defedb2018-08-10 12:35:02 +01008926#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8927 !defined(MBEDTLS_SSL_PROTO_DTLS)
8928 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008929#endif
8930
8931 return( (int) max_len );
8932}
8933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934#if defined(MBEDTLS_X509_CRT_PARSE_C)
8935const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008936{
8937 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008938 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008939
Hanno Beckere6824572019-02-07 13:18:46 +00008940#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008941 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008942#else
8943 return( NULL );
8944#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008949int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8950 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008951{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008952 if( ssl == NULL ||
8953 dst == NULL ||
8954 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008955 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008958 }
8959
Hanno Becker52055ae2019-02-06 14:30:46 +00008960 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008963
Paul Bakker5121ce52009-01-03 21:22:43 +00008964/*
Paul Bakker1961b702013-01-25 14:49:24 +01008965 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008970
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008971 if( ssl == NULL || ssl->conf == NULL )
8972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008981#endif
8982
Paul Bakker1961b702013-01-25 14:49:24 +01008983 return( ret );
8984}
8985
8986/*
8987 * Perform the SSL handshake
8988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008990{
8991 int ret = 0;
8992
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008993 if( ssl == NULL || ssl->conf == NULL )
8994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009001
9002 if( ret != 0 )
9003 break;
9004 }
9005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009007
9008 return( ret );
9009}
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_RENEGOTIATION)
9012#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009013/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009014 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009017{
9018 int ret;
9019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009021
9022 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9024 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009025
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009026 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009027 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009029 return( ret );
9030 }
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009033
9034 return( 0 );
9035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009037
9038/*
9039 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 * - any side: calling mbedtls_ssl_renegotiate(),
9041 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9042 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009043 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009044 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009048{
9049 int ret;
9050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009052
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009053 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9054 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009055
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009056 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9057 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009061 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009062 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009063 ssl->handshake->out_msg_seq = 1;
9064 else
9065 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009066 }
9067#endif
9068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9070 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009075 return( ret );
9076 }
9077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009079
9080 return( 0 );
9081}
9082
9083/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009084 * Renegotiate current connection on client,
9085 * or request renegotiation on server
9086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009090
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009091 if( ssl == NULL || ssl->conf == NULL )
9092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009095 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009096 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009102
9103 /* Did we already try/start sending HelloRequest? */
9104 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009106
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009107 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009112 /*
9113 * On client, either start the renegotiation process or,
9114 * if already in progress, continue the handshake
9115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009120
9121 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009124 return( ret );
9125 }
9126 }
9127 else
9128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009132 return( ret );
9133 }
9134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009136
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009137 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009138}
9139
9140/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009141 * Check record counters and renegotiate if they're above the limit.
9142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009144{
Andres AG2196c7f2016-12-15 17:01:16 +00009145 size_t ep_len = ssl_ep_len( ssl );
9146 int in_ctr_cmp;
9147 int out_ctr_cmp;
9148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9150 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009151 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009152 {
9153 return( 0 );
9154 }
9155
Andres AG2196c7f2016-12-15 17:01:16 +00009156 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9157 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009158 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009159 ssl->conf->renego_period + ep_len, 8 - ep_len );
9160
9161 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009162 {
9163 return( 0 );
9164 }
9165
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009170
9171/*
9172 * Receive application data decrypted from the SSL layer
9173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009175{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009176 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009177 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009178
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009179 if( ssl == NULL || ssl->conf == NULL )
9180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009188 return( ret );
9189
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009190 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009192 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009193 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009194 return( ret );
9195 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009196 }
9197#endif
9198
Hanno Becker4a810fb2017-05-24 16:27:30 +01009199 /*
9200 * Check if renegotiation is necessary and/or handshake is
9201 * in process. If yes, perform/continue, and fall through
9202 * if an unexpected packet is received while the client
9203 * is waiting for the ServerHello.
9204 *
9205 * (There is no equivalent to the last condition on
9206 * the server-side as it is not treated as within
9207 * a handshake while waiting for the ClientHello
9208 * after a renegotiation request.)
9209 */
9210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009212 ret = ssl_check_ctr_renegotiate( ssl );
9213 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9214 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009217 return( ret );
9218 }
9219#endif
9220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009224 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9225 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009228 return( ret );
9229 }
9230 }
9231
Hanno Beckere41158b2017-10-23 13:30:32 +01009232 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009233 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009234 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009235 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009236 if( ssl->f_get_timer != NULL &&
9237 ssl->f_get_timer( ssl->p_timer ) == -1 )
9238 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009239 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009240 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009241
Hanno Becker327c93b2018-08-15 13:56:18 +01009242 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009243 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009244 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9245 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009246
Hanno Becker4a810fb2017-05-24 16:27:30 +01009247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9248 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009249 }
9250
9251 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009253 {
9254 /*
9255 * OpenSSL sends empty messages to randomize the IV
9256 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009257 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009260 return( 0 );
9261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009263 return( ret );
9264 }
9265 }
9266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009270
Hanno Becker4a810fb2017-05-24 16:27:30 +01009271 /*
9272 * - For client-side, expect SERVER_HELLO_REQUEST.
9273 * - For server-side, expect CLIENT_HELLO.
9274 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9275 */
9276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009278 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009280 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009283
9284 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009286 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009287 {
9288 continue;
9289 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009292 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009293#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009294
Hanno Becker4a810fb2017-05-24 16:27:30 +01009295#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009296 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009300
9301 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009304 {
9305 continue;
9306 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009309 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009310#endif /* MBEDTLS_SSL_SRV_C */
9311
Hanno Becker21df7f92017-10-17 11:03:26 +01009312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009313 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009314 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9315 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9316 ssl->conf->allow_legacy_renegotiation ==
9317 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9318 {
9319 /*
9320 * Accept renegotiation request
9321 */
Paul Bakker48916f92012-09-16 19:57:18 +00009322
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009323 /* DTLS clients need to know renego is server-initiated */
9324#if defined(MBEDTLS_SSL_PROTO_DTLS)
9325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9326 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9327 {
9328 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9329 }
9330#endif
9331 ret = ssl_start_renegotiation( ssl );
9332 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9333 ret != 0 )
9334 {
9335 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9336 return( ret );
9337 }
9338 }
9339 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009340#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009341 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009342 /*
9343 * Refuse renegotiation
9344 */
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_SSL_PROTO_SSL3)
9349 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009350 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009351 /* SSLv3 does not have a "no_renegotiation" warning, so
9352 we send a fatal alert and abort the connection. */
9353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9354 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9355 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009356 }
9357 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9359#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9360 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9361 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9364 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9365 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009366 {
9367 return( ret );
9368 }
Paul Bakker48916f92012-09-16 19:57:18 +00009369 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009370 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9372 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9375 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009376 }
Paul Bakker48916f92012-09-16 19:57:18 +00009377 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009378
Hanno Becker90333da2017-10-10 11:27:13 +01009379 /* At this point, we don't know whether the renegotiation has been
9380 * completed or not. The cases to consider are the following:
9381 * 1) The renegotiation is complete. In this case, no new record
9382 * has been read yet.
9383 * 2) The renegotiation is incomplete because the client received
9384 * an application data record while awaiting the ServerHello.
9385 * 3) The renegotiation is incomplete because the client received
9386 * a non-handshake, non-application data message while awaiting
9387 * the ServerHello.
9388 * In each of these case, looping will be the proper action:
9389 * - For 1), the next iteration will read a new record and check
9390 * if it's application data.
9391 * - For 2), the loop condition isn't satisfied as application data
9392 * is present, hence continue is the same as break
9393 * - For 3), the loop condition is satisfied and read_record
9394 * will re-deliver the message that was held back by the client
9395 * when expecting the ServerHello.
9396 */
9397 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009398 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009399#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009401 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009402 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009403 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009404 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009407 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009409 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009410 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009411 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9415 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009418 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009419 }
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9424 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009425 }
9426
9427 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009428
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009429 /* We're going to return something now, cancel timer,
9430 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009432 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009433
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009435 /* If we requested renego but received AppData, resend HelloRequest.
9436 * Do it now, after setting in_offt, to avoid taking this branch
9437 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009441 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009442 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009445 return( ret );
9446 }
9447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009449#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009450 }
9451
9452 n = ( len < ssl->in_msglen )
9453 ? len : ssl->in_msglen;
9454
9455 memcpy( buf, ssl->in_offt, n );
9456 ssl->in_msglen -= n;
9457
9458 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009459 {
9460 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009461 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009462 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009464 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009465 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009466 /* more data available */
9467 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009471
Paul Bakker23986e52011-04-24 08:57:21 +00009472 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009473}
9474
9475/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009476 * Send application data to be encrypted by the SSL layer, taking care of max
9477 * fragment length and buffer size.
9478 *
9479 * According to RFC 5246 Section 6.2.1:
9480 *
9481 * Zero-length fragments of Application data MAY be sent as they are
9482 * potentially useful as a traffic analysis countermeasure.
9483 *
9484 * Therefore, it is possible that the input message length is 0 and the
9485 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009486 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009487static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009488 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009489{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009490 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9491 const size_t max_len = (size_t) ret;
9492
9493 if( ret < 0 )
9494 {
9495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9496 return( ret );
9497 }
9498
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009499 if( len > max_len )
9500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009505 "maximum fragment length: %d > %d",
9506 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009508 }
9509 else
9510#endif
9511 len = max_len;
9512 }
Paul Bakker887bd502011-06-08 13:10:54 +00009513
Paul Bakker5121ce52009-01-03 21:22:43 +00009514 if( ssl->out_left != 0 )
9515 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009516 /*
9517 * The user has previously tried to send the data and
9518 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9519 * written. In this case, we expect the high-level write function
9520 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 return( ret );
9526 }
9527 }
Paul Bakker887bd502011-06-08 13:10:54 +00009528 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009529 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009530 /*
9531 * The user is trying to send a message the first time, so we need to
9532 * copy the data into the internal buffers and setup the data structure
9533 * to keep track of partial writes
9534 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009535 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009537 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009538
Hanno Becker67bc7c32018-08-06 11:33:50 +01009539 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009542 return( ret );
9543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009544 }
9545
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009546 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009547}
9548
9549/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009550 * Write application data, doing 1/n-1 splitting if necessary.
9551 *
9552 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009553 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009554 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009557static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009558 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009559{
9560 int ret;
9561
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009562 if( ssl->conf->cbc_record_splitting ==
9563 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009564 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9566 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9567 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009568 {
9569 return( ssl_write_real( ssl, buf, len ) );
9570 }
9571
9572 if( ssl->split_done == 0 )
9573 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009574 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009575 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009576 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009577 }
9578
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009579 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9580 return( ret );
9581 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009582
9583 return( ret + 1 );
9584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009586
9587/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009588 * Write application data (public-facing wrapper)
9589 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009590int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009591{
9592 int ret;
9593
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009595
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009596 if( ssl == NULL || ssl->conf == NULL )
9597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9598
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009599#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009600 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9601 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009603 return( ret );
9604 }
9605#endif
9606
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009607 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009608 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009609 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009610 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009612 return( ret );
9613 }
9614 }
9615
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009616#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009617 ret = ssl_write_split( ssl, buf, len );
9618#else
9619 ret = ssl_write_real( ssl, buf, len );
9620#endif
9621
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009623
9624 return( ret );
9625}
9626
9627/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 * Notify the peer that the connection is being closed
9629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009631{
9632 int ret;
9633
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009634 if( ssl == NULL || ssl->conf == NULL )
9635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009638
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009639 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9645 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9646 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009649 return( ret );
9650 }
9651 }
9652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009654
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009656}
9657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009659{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009660 if( transform == NULL )
9661 return;
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009664 deflateEnd( &transform->ctx_deflate );
9665 inflateEnd( &transform->ctx_inflate );
9666#endif
9667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9669 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009670
Hanno Beckerd56ed242018-01-03 15:32:51 +00009671#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 mbedtls_md_free( &transform->md_ctx_enc );
9673 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009674#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009675
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009676 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009677}
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_X509_CRT_PARSE_C)
9680static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009683
9684 while( cur != NULL )
9685 {
9686 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009688 cur = next;
9689 }
9690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009692
Hanno Becker0271f962018-08-16 13:23:47 +01009693#if defined(MBEDTLS_SSL_PROTO_DTLS)
9694
9695static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9696{
9697 unsigned offset;
9698 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9699
9700 if( hs == NULL )
9701 return;
9702
Hanno Becker283f5ef2018-08-24 09:34:47 +01009703 ssl_free_buffered_record( ssl );
9704
Hanno Becker0271f962018-08-16 13:23:47 +01009705 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009706 ssl_buffering_free_slot( ssl, offset );
9707}
9708
9709static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9710 uint8_t slot )
9711{
9712 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9713 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009714
9715 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9716 return;
9717
Hanno Beckere605b192018-08-21 15:59:07 +01009718 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009719 {
Hanno Beckere605b192018-08-21 15:59:07 +01009720 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009721 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009722 mbedtls_free( hs_buf->data );
9723 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009724 }
9725}
9726
9727#endif /* MBEDTLS_SSL_PROTO_DTLS */
9728
Gilles Peskine9b562d52018-04-25 20:32:43 +02009729void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009730{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009731 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9732
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009733 if( handshake == NULL )
9734 return;
9735
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009736#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9737 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9738 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009739 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009740 handshake->async_in_progress = 0;
9741 }
9742#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9743
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009744#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9745 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9746 mbedtls_md5_free( &handshake->fin_md5 );
9747 mbedtls_sha1_free( &handshake->fin_sha1 );
9748#endif
9749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9750#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009751#if defined(MBEDTLS_USE_PSA_CRYPTO)
9752 psa_hash_abort( &handshake->fin_sha256_psa );
9753#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009754 mbedtls_sha256_free( &handshake->fin_sha256 );
9755#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009756#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009757#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009758#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009759 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009760#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009761 mbedtls_sha512_free( &handshake->fin_sha512 );
9762#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009763#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009764#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_DHM_C)
9767 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_ECDH_C)
9770 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009771#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009772#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009773 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009774#if defined(MBEDTLS_SSL_CLI_C)
9775 mbedtls_free( handshake->ecjpake_cache );
9776 handshake->ecjpake_cache = NULL;
9777 handshake->ecjpake_cache_len = 0;
9778#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009779#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009780
Janos Follath4ae5c292016-02-10 11:27:43 +00009781#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9782 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009783 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009785#endif
9786
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009787#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9788 if( handshake->psk != NULL )
9789 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009790 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009791 mbedtls_free( handshake->psk );
9792 }
9793#endif
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9796 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009797 /*
9798 * Free only the linked list wrapper, not the keys themselves
9799 * since the belong to the SNI callback
9800 */
9801 if( handshake->sni_key_cert != NULL )
9802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009804
9805 while( cur != NULL )
9806 {
9807 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009809 cur = next;
9810 }
9811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009813
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009814#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009815 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009816 if( handshake->ecrs_peer_cert != NULL )
9817 {
9818 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9819 mbedtls_free( handshake->ecrs_peer_cert );
9820 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009821#endif
9822
Hanno Becker75173122019-02-06 16:18:31 +00009823#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9824 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9825 mbedtls_pk_free( &handshake->peer_pubkey );
9826#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#if defined(MBEDTLS_SSL_PROTO_DTLS)
9829 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009830 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009831 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009832#endif
9833
Hanno Becker4a63ed42019-01-08 11:39:35 +00009834#if defined(MBEDTLS_ECDH_C) && \
9835 defined(MBEDTLS_USE_PSA_CRYPTO)
9836 psa_destroy_key( handshake->ecdh_psa_privkey );
9837#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9838
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009839 mbedtls_platform_zeroize( handshake,
9840 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009841}
9842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009844{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009845 if( session == NULL )
9846 return;
9847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009849 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009850#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009851
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009852#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009854#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009855
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009856 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009857}
9858
Paul Bakker5121ce52009-01-03 21:22:43 +00009859/*
9860 * Free an SSL context
9861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009863{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009864 if( ssl == NULL )
9865 return;
9866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009868
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009869 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009870 {
Angus Grattond8213d02016-05-25 20:56:48 +10009871 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009873 }
9874
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009875 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009876 {
Angus Grattond8213d02016-05-25 20:56:48 +10009877 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009879 }
9880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009882 if( ssl->compress_buf != NULL )
9883 {
Angus Grattond8213d02016-05-25 20:56:48 +10009884 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009886 }
9887#endif
9888
Paul Bakker48916f92012-09-16 19:57:18 +00009889 if( ssl->transform )
9890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 mbedtls_ssl_transform_free( ssl->transform );
9892 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009893 }
9894
9895 if( ssl->handshake )
9896 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009897 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9899 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901 mbedtls_free( ssl->handshake );
9902 mbedtls_free( ssl->transform_negotiate );
9903 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009904 }
9905
Paul Bakkerc0463502013-02-14 11:19:38 +01009906 if( ssl->session )
9907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908 mbedtls_ssl_session_free( ssl->session );
9909 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009910 }
9911
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009912#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009913 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009914 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009915 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009917 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9921 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9924 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009925 }
9926#endif
9927
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009928#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009930#endif
9931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009933
Paul Bakker86f04f42013-02-14 11:20:09 +01009934 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009935 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009936}
9937
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009938/*
9939 * Initialze mbedtls_ssl_config
9940 */
9941void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9942{
9943 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9944}
9945
Simon Butcherc97b6972015-12-27 23:48:17 +00009946#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009947static int ssl_preset_default_hashes[] = {
9948#if defined(MBEDTLS_SHA512_C)
9949 MBEDTLS_MD_SHA512,
9950 MBEDTLS_MD_SHA384,
9951#endif
9952#if defined(MBEDTLS_SHA256_C)
9953 MBEDTLS_MD_SHA256,
9954 MBEDTLS_MD_SHA224,
9955#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009956#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009957 MBEDTLS_MD_SHA1,
9958#endif
9959 MBEDTLS_MD_NONE
9960};
Simon Butcherc97b6972015-12-27 23:48:17 +00009961#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009962
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009963static int ssl_preset_suiteb_ciphersuites[] = {
9964 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9965 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9966 0
9967};
9968
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009969#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009970static int ssl_preset_suiteb_hashes[] = {
9971 MBEDTLS_MD_SHA256,
9972 MBEDTLS_MD_SHA384,
9973 MBEDTLS_MD_NONE
9974};
9975#endif
9976
9977#if defined(MBEDTLS_ECP_C)
9978static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9979 MBEDTLS_ECP_DP_SECP256R1,
9980 MBEDTLS_ECP_DP_SECP384R1,
9981 MBEDTLS_ECP_DP_NONE
9982};
9983#endif
9984
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009985/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009986 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009987 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009988int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009989 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009990{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009991#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009992 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009993#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009994
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009995 /* Use the functions here so that they are covered in tests,
9996 * but otherwise access member directly for efficiency */
9997 mbedtls_ssl_conf_endpoint( conf, endpoint );
9998 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009999
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010000 /*
10001 * Things that are common to all presets
10002 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010003#if defined(MBEDTLS_SSL_CLI_C)
10004 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10005 {
10006 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10007#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10008 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10009#endif
10010 }
10011#endif
10012
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010013#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010014 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010015#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010016
10017#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10018 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10019#endif
10020
10021#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10022 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10023#endif
10024
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010025#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10026 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10027#endif
10028
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010029#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010030 conf->f_cookie_write = ssl_cookie_write_dummy;
10031 conf->f_cookie_check = ssl_cookie_check_dummy;
10032#endif
10033
10034#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10035 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10036#endif
10037
Janos Follath088ce432017-04-10 12:42:31 +010010038#if defined(MBEDTLS_SSL_SRV_C)
10039 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10040#endif
10041
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010042#if defined(MBEDTLS_SSL_PROTO_DTLS)
10043 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10044 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10045#endif
10046
10047#if defined(MBEDTLS_SSL_RENEGOTIATION)
10048 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010049 memset( conf->renego_period, 0x00, 2 );
10050 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010051#endif
10052
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010053#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10054 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10055 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010056 const unsigned char dhm_p[] =
10057 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10058 const unsigned char dhm_g[] =
10059 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10060
Hanno Beckera90658f2017-10-04 15:29:08 +010010061 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10062 dhm_p, sizeof( dhm_p ),
10063 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010064 {
10065 return( ret );
10066 }
10067 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010068#endif
10069
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010070 /*
10071 * Preset-specific defaults
10072 */
10073 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010074 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010075 /*
10076 * NSA Suite B
10077 */
10078 case MBEDTLS_SSL_PRESET_SUITEB:
10079 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10080 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10081 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10082 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10083
10084 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10085 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10086 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10087 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10088 ssl_preset_suiteb_ciphersuites;
10089
10090#if defined(MBEDTLS_X509_CRT_PARSE_C)
10091 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010092#endif
10093
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010094#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010095 conf->sig_hashes = ssl_preset_suiteb_hashes;
10096#endif
10097
10098#if defined(MBEDTLS_ECP_C)
10099 conf->curve_list = ssl_preset_suiteb_curves;
10100#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010101 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010102
10103 /*
10104 * Default
10105 */
10106 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010107 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10108 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10109 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10110 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10111 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10112 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10113 MBEDTLS_SSL_MIN_MINOR_VERSION :
10114 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010115 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10116 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10117
10118#if defined(MBEDTLS_SSL_PROTO_DTLS)
10119 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10120 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10121#endif
10122
10123 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10124 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10125 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10126 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10127 mbedtls_ssl_list_ciphersuites();
10128
10129#if defined(MBEDTLS_X509_CRT_PARSE_C)
10130 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10131#endif
10132
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010133#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010134 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010135#endif
10136
10137#if defined(MBEDTLS_ECP_C)
10138 conf->curve_list = mbedtls_ecp_grp_id_list();
10139#endif
10140
10141#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10142 conf->dhm_min_bitlen = 1024;
10143#endif
10144 }
10145
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010146 return( 0 );
10147}
10148
10149/*
10150 * Free mbedtls_ssl_config
10151 */
10152void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10153{
10154#if defined(MBEDTLS_DHM_C)
10155 mbedtls_mpi_free( &conf->dhm_P );
10156 mbedtls_mpi_free( &conf->dhm_G );
10157#endif
10158
10159#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10160 if( conf->psk != NULL )
10161 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010162 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010163 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010164 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010165 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010166 }
10167
10168 if( conf->psk_identity != NULL )
10169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010170 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010171 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010172 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010173 conf->psk_identity_len = 0;
10174 }
10175#endif
10176
10177#if defined(MBEDTLS_X509_CRT_PARSE_C)
10178 ssl_key_cert_free( conf->key_cert );
10179#endif
10180
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010181 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010182}
10183
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010184#if defined(MBEDTLS_PK_C) && \
10185 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010186/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191#if defined(MBEDTLS_RSA_C)
10192 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10193 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195#if defined(MBEDTLS_ECDSA_C)
10196 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10197 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010200}
10201
Hanno Becker7e5437a2017-04-28 17:15:26 +010010202unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10203{
10204 switch( type ) {
10205 case MBEDTLS_PK_RSA:
10206 return( MBEDTLS_SSL_SIG_RSA );
10207 case MBEDTLS_PK_ECDSA:
10208 case MBEDTLS_PK_ECKEY:
10209 return( MBEDTLS_SSL_SIG_ECDSA );
10210 default:
10211 return( MBEDTLS_SSL_SIG_ANON );
10212 }
10213}
10214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010216{
10217 switch( sig )
10218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#if defined(MBEDTLS_RSA_C)
10220 case MBEDTLS_SSL_SIG_RSA:
10221 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#if defined(MBEDTLS_ECDSA_C)
10224 case MBEDTLS_SSL_SIG_ECDSA:
10225 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010226#endif
10227 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010229 }
10230}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010231#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010232
Hanno Becker7e5437a2017-04-28 17:15:26 +010010233#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10234 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10235
10236/* Find an entry in a signature-hash set matching a given hash algorithm. */
10237mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10238 mbedtls_pk_type_t sig_alg )
10239{
10240 switch( sig_alg )
10241 {
10242 case MBEDTLS_PK_RSA:
10243 return( set->rsa );
10244 case MBEDTLS_PK_ECDSA:
10245 return( set->ecdsa );
10246 default:
10247 return( MBEDTLS_MD_NONE );
10248 }
10249}
10250
10251/* Add a signature-hash-pair to a signature-hash set */
10252void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10253 mbedtls_pk_type_t sig_alg,
10254 mbedtls_md_type_t md_alg )
10255{
10256 switch( sig_alg )
10257 {
10258 case MBEDTLS_PK_RSA:
10259 if( set->rsa == MBEDTLS_MD_NONE )
10260 set->rsa = md_alg;
10261 break;
10262
10263 case MBEDTLS_PK_ECDSA:
10264 if( set->ecdsa == MBEDTLS_MD_NONE )
10265 set->ecdsa = md_alg;
10266 break;
10267
10268 default:
10269 break;
10270 }
10271}
10272
10273/* Allow exactly one hash algorithm for each signature. */
10274void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10275 mbedtls_md_type_t md_alg )
10276{
10277 set->rsa = md_alg;
10278 set->ecdsa = md_alg;
10279}
10280
10281#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10282 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10283
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010284/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010285 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010288{
10289 switch( hash )
10290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291#if defined(MBEDTLS_MD5_C)
10292 case MBEDTLS_SSL_HASH_MD5:
10293 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295#if defined(MBEDTLS_SHA1_C)
10296 case MBEDTLS_SSL_HASH_SHA1:
10297 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299#if defined(MBEDTLS_SHA256_C)
10300 case MBEDTLS_SSL_HASH_SHA224:
10301 return( MBEDTLS_MD_SHA224 );
10302 case MBEDTLS_SSL_HASH_SHA256:
10303 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305#if defined(MBEDTLS_SHA512_C)
10306 case MBEDTLS_SSL_HASH_SHA384:
10307 return( MBEDTLS_MD_SHA384 );
10308 case MBEDTLS_SSL_HASH_SHA512:
10309 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010310#endif
10311 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010313 }
10314}
10315
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010316/*
10317 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10318 */
10319unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10320{
10321 switch( md )
10322 {
10323#if defined(MBEDTLS_MD5_C)
10324 case MBEDTLS_MD_MD5:
10325 return( MBEDTLS_SSL_HASH_MD5 );
10326#endif
10327#if defined(MBEDTLS_SHA1_C)
10328 case MBEDTLS_MD_SHA1:
10329 return( MBEDTLS_SSL_HASH_SHA1 );
10330#endif
10331#if defined(MBEDTLS_SHA256_C)
10332 case MBEDTLS_MD_SHA224:
10333 return( MBEDTLS_SSL_HASH_SHA224 );
10334 case MBEDTLS_MD_SHA256:
10335 return( MBEDTLS_SSL_HASH_SHA256 );
10336#endif
10337#if defined(MBEDTLS_SHA512_C)
10338 case MBEDTLS_MD_SHA384:
10339 return( MBEDTLS_SSL_HASH_SHA384 );
10340 case MBEDTLS_MD_SHA512:
10341 return( MBEDTLS_SSL_HASH_SHA512 );
10342#endif
10343 default:
10344 return( MBEDTLS_SSL_HASH_NONE );
10345 }
10346}
10347
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010348#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010349/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010350 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010351 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010352 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010353int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010356
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010357 if( ssl->conf->curve_list == NULL )
10358 return( -1 );
10359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010360 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010361 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010362 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010363
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010364 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010365}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010366#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010369/*
10370 * Check if a hash proposed by the peer is in our list.
10371 * Return 0 if we're willing to use it, -1 otherwise.
10372 */
10373int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10374 mbedtls_md_type_t md )
10375{
10376 const int *cur;
10377
10378 if( ssl->conf->sig_hashes == NULL )
10379 return( -1 );
10380
10381 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10382 if( *cur == (int) md )
10383 return( 0 );
10384
10385 return( -1 );
10386}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010387#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389#if defined(MBEDTLS_X509_CRT_PARSE_C)
10390int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10391 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010392 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010393 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010394{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010395 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010397 int usage = 0;
10398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010400 const char *ext_oid;
10401 size_t ext_len;
10402#endif
10403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10405 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010406 ((void) cert);
10407 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010408 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010409#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10412 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010413 {
10414 /* Server part of the key exchange */
10415 switch( ciphersuite->key_exchange )
10416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 case MBEDTLS_KEY_EXCHANGE_RSA:
10418 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010419 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010420 break;
10421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10423 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10424 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10425 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010426 break;
10427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10429 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010430 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010431 break;
10432
10433 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 case MBEDTLS_KEY_EXCHANGE_NONE:
10435 case MBEDTLS_KEY_EXCHANGE_PSK:
10436 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10437 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010438 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010439 usage = 0;
10440 }
10441 }
10442 else
10443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10445 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010446 }
10447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010449 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010450 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010451 ret = -1;
10452 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010453#else
10454 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10458 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10461 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010462 }
10463 else
10464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10466 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010467 }
10468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010470 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010471 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010472 ret = -1;
10473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010475
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010476 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010479
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010480/*
10481 * Convert version numbers to/from wire format
10482 * and, for DTLS, to/from TLS equivalent.
10483 *
10484 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010485 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010486 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10487 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010490 unsigned char ver[2] )
10491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492#if defined(MBEDTLS_SSL_PROTO_DTLS)
10493 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010496 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10497
10498 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10499 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10500 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010501 else
10502#else
10503 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010504#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010505 {
10506 ver[0] = (unsigned char) major;
10507 ver[1] = (unsigned char) minor;
10508 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010509}
10510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010512 const unsigned char ver[2] )
10513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#if defined(MBEDTLS_SSL_PROTO_DTLS)
10515 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010516 {
10517 *major = 255 - ver[0] + 2;
10518 *minor = 255 - ver[1] + 1;
10519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010521 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10522 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010523 else
10524#else
10525 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010526#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010527 {
10528 *major = ver[0];
10529 *minor = ver[1];
10530 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010531}
10532
Simon Butcher99000142016-10-13 17:21:01 +010010533int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10534{
10535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10536 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10537 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10538
10539 switch( md )
10540 {
10541#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10542#if defined(MBEDTLS_MD5_C)
10543 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010544 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010545#endif
10546#if defined(MBEDTLS_SHA1_C)
10547 case MBEDTLS_SSL_HASH_SHA1:
10548 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10549 break;
10550#endif
10551#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10552#if defined(MBEDTLS_SHA512_C)
10553 case MBEDTLS_SSL_HASH_SHA384:
10554 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10555 break;
10556#endif
10557#if defined(MBEDTLS_SHA256_C)
10558 case MBEDTLS_SSL_HASH_SHA256:
10559 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10560 break;
10561#endif
10562 default:
10563 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10564 }
10565
10566 return 0;
10567#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10568 (void) ssl;
10569 (void) md;
10570
10571 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10573}
10574
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010575#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10576 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10577int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10578 unsigned char *output,
10579 unsigned char *data, size_t data_len )
10580{
10581 int ret = 0;
10582 mbedtls_md5_context mbedtls_md5;
10583 mbedtls_sha1_context mbedtls_sha1;
10584
10585 mbedtls_md5_init( &mbedtls_md5 );
10586 mbedtls_sha1_init( &mbedtls_sha1 );
10587
10588 /*
10589 * digitally-signed struct {
10590 * opaque md5_hash[16];
10591 * opaque sha_hash[20];
10592 * };
10593 *
10594 * md5_hash
10595 * MD5(ClientHello.random + ServerHello.random
10596 * + ServerParams);
10597 * sha_hash
10598 * SHA(ClientHello.random + ServerHello.random
10599 * + ServerParams);
10600 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010601 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010602 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010604 goto exit;
10605 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010606 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010607 ssl->handshake->randbytes, 64 ) ) != 0 )
10608 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010610 goto exit;
10611 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010612 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010613 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010615 goto exit;
10616 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010617 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010618 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010620 goto exit;
10621 }
10622
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010623 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010624 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010626 goto exit;
10627 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010628 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010629 ssl->handshake->randbytes, 64 ) ) != 0 )
10630 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010632 goto exit;
10633 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010634 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010635 data_len ) ) != 0 )
10636 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010638 goto exit;
10639 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010640 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010641 output + 16 ) ) != 0 )
10642 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010644 goto exit;
10645 }
10646
10647exit:
10648 mbedtls_md5_free( &mbedtls_md5 );
10649 mbedtls_sha1_free( &mbedtls_sha1 );
10650
10651 if( ret != 0 )
10652 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10653 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10654
10655 return( ret );
10656
10657}
10658#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10659 MBEDTLS_SSL_PROTO_TLS1_1 */
10660
10661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10662 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010663
10664#if defined(MBEDTLS_USE_PSA_CRYPTO)
10665int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10666 unsigned char *hash, size_t *hashlen,
10667 unsigned char *data, size_t data_len,
10668 mbedtls_md_type_t md_alg )
10669{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010670 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010671 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010672 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10673
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010675
10676 if( ( status = psa_hash_setup( &hash_operation,
10677 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010678 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010679 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010680 goto exit;
10681 }
10682
Andrzej Kurek814feff2019-01-14 04:35:19 -050010683 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10684 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010685 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010686 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010687 goto exit;
10688 }
10689
Andrzej Kurek814feff2019-01-14 04:35:19 -050010690 if( ( status = psa_hash_update( &hash_operation,
10691 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010692 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010693 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010694 goto exit;
10695 }
10696
Andrzej Kurek814feff2019-01-14 04:35:19 -050010697 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10698 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010699 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010700 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010701 goto exit;
10702 }
10703
10704exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010705 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010706 {
10707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10708 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010709 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010710 {
10711 case PSA_ERROR_NOT_SUPPORTED:
10712 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010713 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010714 case PSA_ERROR_BUFFER_TOO_SMALL:
10715 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10716 case PSA_ERROR_INSUFFICIENT_MEMORY:
10717 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10718 default:
10719 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10720 }
10721 }
10722 return( 0 );
10723}
10724
10725#else
10726
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010727int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010728 unsigned char *hash, size_t *hashlen,
10729 unsigned char *data, size_t data_len,
10730 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010731{
10732 int ret = 0;
10733 mbedtls_md_context_t ctx;
10734 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010735 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010736
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010738
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010739 mbedtls_md_init( &ctx );
10740
10741 /*
10742 * digitally-signed struct {
10743 * opaque client_random[32];
10744 * opaque server_random[32];
10745 * ServerDHParams params;
10746 * };
10747 */
10748 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10749 {
10750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10751 goto exit;
10752 }
10753 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10754 {
10755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10756 goto exit;
10757 }
10758 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10759 {
10760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10761 goto exit;
10762 }
10763 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10764 {
10765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10766 goto exit;
10767 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010768 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010769 {
10770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10771 goto exit;
10772 }
10773
10774exit:
10775 mbedtls_md_free( &ctx );
10776
10777 if( ret != 0 )
10778 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10779 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10780
10781 return( ret );
10782}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010783#endif /* MBEDTLS_USE_PSA_CRYPTO */
10784
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010785#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10786 MBEDTLS_SSL_PROTO_TLS1_2 */
10787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788#endif /* MBEDTLS_SSL_TLS_C */