blob: 26ba4e9761251baeb8e4c2c10b47940a3b69182e [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
907 if( md_type == MBEDTLS_MD_SHA384 )
908 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
909 else
910 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
911
912 status = psa_key_derivation( &generator, psk, alg,
913 salt, salt_len,
914 (unsigned char const *) lbl,
915 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000916 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100917 if( status != PSA_SUCCESS )
918 {
919 psa_generator_abort( &generator );
920 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
921 }
922
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000923 status = psa_generator_read( &generator, session->master,
924 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100925 if( status != PSA_SUCCESS )
926 {
927 psa_generator_abort( &generator );
928 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
929 }
930
931 status = psa_generator_abort( &generator );
932 if( status != PSA_SUCCESS )
933 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100934 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 else
936#endif
937 {
938 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
939 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000940 session->master,
941 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100942 if( ret != 0 )
943 {
944 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
945 return( ret );
946 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
949 handshake->premaster,
950 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100951
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952 mbedtls_platform_zeroize( handshake->premaster,
953 sizeof(handshake->premaster) );
954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
957 /*
958 * Swap the client and server random values.
959 */
Paul Bakker48916f92012-09-16 19:57:18 +0000960 memcpy( tmp, handshake->randbytes, 64 );
961 memcpy( handshake->randbytes, tmp + 32, 32 );
962 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500963 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965 /*
966 * SSLv3:
967 * key block =
968 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
969 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
970 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
971 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
972 * ...
973 *
974 * TLSv1:
975 * key block = PRF( master, "key expansion", randbytes )
976 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100977 ret = handshake->tls_prf( session->master, 48, "key expansion",
978 handshake->randbytes, 64, keyblk, 256 );
979 if( ret != 0 )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100982 return( ret );
983 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
986 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
987 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
988 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
989 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500991 mbedtls_platform_zeroize( handshake->randbytes,
992 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 /*
995 * Determine the appropriate key, IV and MAC length.
996 */
Paul Bakker68884e32013-01-07 18:20:04 +0100997
Hanno Becker88aaf652017-12-27 08:17:40 +0000998 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200999
Hanno Becker8031d062018-01-03 15:32:31 +00001000#if defined(MBEDTLS_GCM_C) || \
1001 defined(MBEDTLS_CCM_C) || \
1002 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001004 cipher_info->mode == MBEDTLS_MODE_CCM ||
1005 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001007 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001008
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001009 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001010 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001011 transform->taglen =
1012 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001013
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001014 /* All modes haves 96-bit IVs;
1015 * GCM and CCM has 4 implicit and 8 explicit bytes
1016 * ChachaPoly has all 12 bytes implicit
1017 */
Paul Bakker68884e32013-01-07 18:20:04 +01001018 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001019 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1020 transform->fixed_ivlen = 12;
1021 else
1022 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001024 /* Minimum length of encrypted record */
1025 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001027 }
1028 else
Hanno Becker8031d062018-01-03 15:32:31 +00001029#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1030#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1031 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1032 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001033 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1036 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001042 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001043 mac_key_len = mbedtls_md_get_size( md_info );
1044 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001047 /*
1048 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1049 * (rfc 6066 page 13 or rfc 2104 section 4),
1050 * so we only need to adjust the length here.
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001055
1056#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1057 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001058 * HMAC implementation which also truncates the key
1059 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001060 mac_key_len = transform->maclen;
1061#endif
1062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001064
1065 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001066 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001068 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 transform->minlen = transform->maclen;
1071 else
Paul Bakker68884e32013-01-07 18:20:04 +01001072 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001073 /*
1074 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001075 * 1. if EtM is in use: one block plus MAC
1076 * otherwise: * first multiple of blocklen greater than maclen
1077 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1080 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001081 {
1082 transform->minlen = transform->maclen
1083 + cipher_info->block_size;
1084 }
1085 else
1086#endif
1087 {
1088 transform->minlen = transform->maclen
1089 + cipher_info->block_size
1090 - transform->maclen % cipher_info->block_size;
1091 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1094 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1095 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001096 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001097 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1100 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1101 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 {
1103 transform->minlen += transform->ivlen;
1104 }
1105 else
1106#endif
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001110 }
Paul Bakker68884e32013-01-07 18:20:04 +01001111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
Hanno Becker8031d062018-01-03 15:32:31 +00001113 else
1114#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1115 {
1116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Hanno Becker88aaf652017-12-27 08:17:40 +00001120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1121 (unsigned) keylen,
1122 (unsigned) transform->minlen,
1123 (unsigned) transform->ivlen,
1124 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 /*
1127 * Finally setup the cipher contexts, IVs and MAC secrets.
1128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001130 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001132 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001133 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Paul Bakker68884e32013-01-07 18:20:04 +01001135 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001138 /*
1139 * This is not used in TLS v1.1.
1140 */
Paul Bakker48916f92012-09-16 19:57:18 +00001141 iv_copy_len = ( transform->fixed_ivlen ) ?
1142 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001143 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1144 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 }
1147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_SSL_CLI_C */
1149#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001150 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001152 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001153 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Hanno Becker81c7b182017-11-09 18:39:33 +00001155 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001156 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001158 /*
1159 * This is not used in TLS v1.1.
1160 */
Paul Bakker48916f92012-09-16 19:57:18 +00001161 iv_copy_len = ( transform->fixed_ivlen ) ?
1162 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001163 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1164 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001165 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Hanno Beckerd56ed242018-01-03 15:32:51 +00001174#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#if defined(MBEDTLS_SSL_PROTO_SSL3)
1176 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001177 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001178 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
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" ) );
1205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001206 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001207#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1210 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001211 {
1212 int ret = 0;
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001215
Hanno Becker88aaf652017-12-27 08:17:40 +00001216 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001217 transform->iv_enc, transform->iv_dec,
1218 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001219 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001220 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1223 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001224 }
1225 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001226#else
1227 ((void) mac_dec);
1228 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001230
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001231#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1232 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001233 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001234 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1235 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001236 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001237 iv_copy_len );
1238 }
1239#endif
1240
Hanno Beckerf704bef2018-11-16 15:21:18 +00001241#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001242
1243 /* Only use PSA-based ciphers for TLS-1.2.
1244 * That's relevant at least for TLS-1.0, where
1245 * we assume that mbedtls_cipher_crypt() updates
1246 * the structure field for the IV, which the PSA-based
1247 * implementation currently doesn't. */
1248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1249 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001250 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001251 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1252 cipher_info, taglen );
1253 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1254 {
1255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1256 return( ret );
1257 }
1258
1259 if( ret == 0 )
1260 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001262 psa_fallthrough = 0;
1263 }
1264 else
1265 {
1266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1267 psa_fallthrough = 1;
1268 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001269 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001270 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001271 psa_fallthrough = 1;
1272#else
1273 psa_fallthrough = 1;
1274#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001275
Hanno Beckercb1cc802018-11-17 22:27:38 +00001276 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001277#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001278 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001279 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001282 return( ret );
1283 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001284
Hanno Beckerf704bef2018-11-16 15:21:18 +00001285#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001286 /* Only use PSA-based ciphers for TLS-1.2.
1287 * That's relevant at least for TLS-1.0, where
1288 * we assume that mbedtls_cipher_crypt() updates
1289 * the structure field for the IV, which the PSA-based
1290 * implementation currently doesn't. */
1291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1292 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001293 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001294 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1295 cipher_info, taglen );
1296 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1297 {
1298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1299 return( ret );
1300 }
1301
1302 if( ret == 0 )
1303 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001305 psa_fallthrough = 0;
1306 }
1307 else
1308 {
1309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1310 psa_fallthrough = 1;
1311 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001312 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001313 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001314 psa_fallthrough = 1;
1315#else
1316 psa_fallthrough = 1;
1317#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001318
Hanno Beckercb1cc802018-11-17 22:27:38 +00001319 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001320#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001321 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001322 cipher_info ) ) != 0 )
1323 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001325 return( ret );
1326 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001329 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001333 return( ret );
1334 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001337 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001341 return( ret );
1342 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_CIPHER_MODE_CBC)
1345 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1348 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001351 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001352 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1355 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001358 return( ret );
1359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001363 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001366 // Initialize compression
1367 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001369 {
Paul Bakker16770332013-10-11 09:59:44 +02001370 if( ssl->compress_buf == NULL )
1371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001373 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001374 if( ssl->compress_buf == NULL )
1375 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001377 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001379 }
1380 }
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001383
Paul Bakker48916f92012-09-16 19:57:18 +00001384 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1385 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001386
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001387 if( deflateInit( &transform->ctx_deflate,
1388 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001389 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1392 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001393 }
1394 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
1399 return( 0 );
1400}
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_SSL_PROTO_SSL3)
1403void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001405 mbedtls_md5_context md5;
1406 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 unsigned char pad_1[48];
1408 unsigned char pad_2[48];
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001412 mbedtls_md5_init( &md5 );
1413 mbedtls_sha1_init( &sha1 );
1414
1415 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1416 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Paul Bakker380da532012-04-18 16:10:25 +00001418 memset( pad_1, 0x36, 48 );
1419 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1423 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001425 mbedtls_md5_starts_ret( &md5 );
1426 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1427 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1428 mbedtls_md5_update_ret( &md5, hash, 16 );
1429 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1433 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001434
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001435 mbedtls_sha1_starts_ret( &sha1 );
1436 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1437 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1438 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1439 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001444 mbedtls_md5_free( &md5 );
1445 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001446
Paul Bakker380da532012-04-18 16:10:25 +00001447 return;
1448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1452void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001453{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_md5_context md5;
1455 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001459 mbedtls_md5_init( &md5 );
1460 mbedtls_sha1_init( &sha1 );
1461
1462 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1463 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Andrzej Kurekeb342242019-01-29 09:14:33 -05001465 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001466 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001471 mbedtls_md5_free( &md5 );
1472 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001473
Paul Bakker380da532012-04-18 16:10:25 +00001474 return;
1475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1479#if defined(MBEDTLS_SHA256_C)
1480void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001481{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001482#if defined(MBEDTLS_USE_PSA_CRYPTO)
1483 size_t hash_size;
1484 psa_status_t status;
1485 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1486
1487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1488 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1489 if( status != PSA_SUCCESS )
1490 {
1491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1492 return;
1493 }
1494
1495 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1496 if( status != PSA_SUCCESS )
1497 {
1498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1499 return;
1500 }
1501 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1503#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001504 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001506 mbedtls_sha256_init( &sha256 );
1507
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001509
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001510 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001511 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001517#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001518 return;
1519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_SHA512_C)
1523void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001524{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001525#if defined(MBEDTLS_USE_PSA_CRYPTO)
1526 size_t hash_size;
1527 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001528 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001529
1530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001531 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001532 if( status != PSA_SUCCESS )
1533 {
1534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1535 return;
1536 }
1537
Andrzej Kurek972fba52019-01-30 03:29:12 -05001538 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001539 if( status != PSA_SUCCESS )
1540 {
1541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1542 return;
1543 }
1544 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1546#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001547 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001548
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001549 mbedtls_sha512_init( &sha512 );
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001552
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001553 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001554 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001558
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001559 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001560#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 return;
1562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_SHA512_C */
1564#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1567int 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 +02001568{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 unsigned char *p = ssl->handshake->premaster;
1570 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001571 const unsigned char *psk = ssl->conf->psk;
1572 size_t psk_len = ssl->conf->psk_len;
1573
1574 /* If the psk callback was called, use its result */
1575 if( ssl->handshake->psk != NULL )
1576 {
1577 psk = ssl->handshake->psk;
1578 psk_len = ssl->handshake->psk_len;
1579 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580
1581 /*
1582 * PMS = struct {
1583 * opaque other_secret<0..2^16-1>;
1584 * opaque psk<0..2^16-1>;
1585 * };
1586 * with "other_secret" depending on the particular key exchange
1587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1589 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001591 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001593
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001594 *(p++) = (unsigned char)( psk_len >> 8 );
1595 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001596
1597 if( end < p || (size_t)( end - p ) < psk_len )
1598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1599
1600 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001601 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 }
1603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1605#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1606 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001607 {
1608 /*
1609 * other_secret already set by the ClientKeyExchange message,
1610 * and is 48 bytes long
1611 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001612 if( end - p < 2 )
1613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1614
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001615 *p++ = 0;
1616 *p++ = 48;
1617 p += 48;
1618 }
1619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1621#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1622 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001624 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001625 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001627 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001629 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001630 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 return( ret );
1634 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001635 *(p++) = (unsigned char)( len >> 8 );
1636 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 p += len;
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 }
1641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1643#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1644 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001646 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001647 size_t zlen;
1648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001650 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001651 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654 return( ret );
1655 }
1656
1657 *(p++) = (unsigned char)( zlen >> 8 );
1658 *(p++) = (unsigned char)( zlen );
1659 p += zlen;
1660
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001661 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1662 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001663 }
1664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1668 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001669 }
1670
1671 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001672 if( end - p < 2 )
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001674
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001675 *(p++) = (unsigned char)( psk_len >> 8 );
1676 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001677
1678 if( end < p || (size_t)( end - p ) < psk_len )
1679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1680
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001681 memcpy( p, psk, psk_len );
1682 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001683
1684 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1685
1686 return( 0 );
1687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001691/*
1692 * SSLv3.0 MAC functions
1693 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001694#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695static void ssl_mac( mbedtls_md_context_t *md_ctx,
1696 const unsigned char *secret,
1697 const unsigned char *buf, size_t len,
1698 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001699 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700{
1701 unsigned char header[11];
1702 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001703 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1705 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001706
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001707 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001709 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001710 else
Paul Bakker68884e32013-01-07 18:20:04 +01001711 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
1713 memcpy( header, ctr, 8 );
1714 header[ 8] = (unsigned char) type;
1715 header[ 9] = (unsigned char)( len >> 8 );
1716 header[10] = (unsigned char)( len );
1717
Paul Bakker68884e32013-01-07 18:20:04 +01001718 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_md_starts( md_ctx );
1720 mbedtls_md_update( md_ctx, secret, md_size );
1721 mbedtls_md_update( md_ctx, padding, padlen );
1722 mbedtls_md_update( md_ctx, header, 11 );
1723 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001724 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Paul Bakker68884e32013-01-07 18:20:04 +01001726 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_md_starts( md_ctx );
1728 mbedtls_md_update( md_ctx, secret, md_size );
1729 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001730 mbedtls_md_update( md_ctx, out, md_size );
1731 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001734
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001735/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001736 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001737#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001738 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1739 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1740 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1741/* This function makes sure every byte in the memory region is accessed
1742 * (in ascending addresses order) */
1743static void ssl_read_memory( unsigned char *p, size_t len )
1744{
1745 unsigned char acc = 0;
1746 volatile unsigned char force;
1747
1748 for( ; len != 0; p++, len-- )
1749 acc ^= *p;
1750
1751 force = acc;
1752 (void) force;
1753}
1754#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1755
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001756/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001758 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001759
1760static void ssl_extract_add_data_from_record( unsigned char* add_data,
1761 mbedtls_record *rec )
1762{
1763 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1764 add_data[8] = rec->type;
1765 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1766 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1767 add_data[12] = rec->data_len & 0xFF;
1768}
1769
Hanno Beckera18d1322018-01-03 14:27:32 +00001770int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1771 mbedtls_ssl_transform *transform,
1772 mbedtls_record *rec,
1773 int (*f_rng)(void *, unsigned char *, size_t),
1774 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001777 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001778 unsigned char * data;
1779 unsigned char add_data[13];
1780 size_t post_avail;
1781
1782 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001783#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001784 ((void) ssl);
1785#endif
1786
1787 /* The PRNG is used for dynamic IV generation that's used
1788 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1789#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1790 ( defined(MBEDTLS_AES_C) || \
1791 defined(MBEDTLS_ARIA_C) || \
1792 defined(MBEDTLS_CAMELLIA_C) ) && \
1793 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1794 ((void) f_rng);
1795 ((void) p_rng);
1796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001800 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1804 }
1805 if( rec == NULL ||
1806 rec->buf == NULL ||
1807 rec->buf_len < rec->data_offset ||
1808 rec->buf_len - rec->data_offset < rec->data_len )
1809 {
1810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001812 }
1813
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001814 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1815 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001817 data, rec->data_len );
1818
1819 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1820
1821 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1822 {
1823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1824 (unsigned) rec->data_len,
1825 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1827 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001828
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001830 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 */
Hanno Becker52344c22018-01-03 15:24:20 +00001832#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( mode == MBEDTLS_MODE_STREAM ||
1834 ( mode == MBEDTLS_MODE_CBC
1835#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001836 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001837#endif
1838 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001840 if( post_avail < transform->maclen )
1841 {
1842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1843 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001847 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001849 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001850 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1851 data, rec->data_len, rec->ctr, rec->type, mac );
1852 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 }
1854 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1857 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001858 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001859 {
Hanno Becker992b6872017-11-09 18:57:39 +00001860 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1861
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001862 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001863
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001864 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1865 sizeof( add_data ) );
1866 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1867 data, rec->data_len );
1868 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1869 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1870
1871 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001872 }
1873 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001874#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001878 }
1879
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001880 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1881 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001882
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001883 rec->data_len += transform->maclen;
1884 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001885 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001886 }
Hanno Becker52344c22018-01-03 15:24:20 +00001887#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001889 /*
1890 * Encrypt
1891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1893 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001895 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001896 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001898 "including %d bytes of padding",
1899 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001901 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1902 transform->iv_enc, transform->ivlen,
1903 data, rec->data_len,
1904 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001907 return( ret );
1908 }
1909
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001910 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 }
Paul Bakker68884e32013-01-07 18:20:04 +01001916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001918
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001919#if defined(MBEDTLS_GCM_C) || \
1920 defined(MBEDTLS_CCM_C) || \
1921 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001923 mode == MBEDTLS_MODE_CCM ||
1924 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001925 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001926 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001928 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001929
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001930 /* Check that there's space for both the authentication tag
1931 * and the explicit IV before and after the record content. */
1932 if( post_avail < transform->taglen ||
1933 rec->data_offset < explicit_iv_len )
1934 {
1935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1936 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1937 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001938
Paul Bakker68884e32013-01-07 18:20:04 +01001939 /*
1940 * Generate IV
1941 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001942 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1943 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001944 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001945 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001946 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1947 explicit_iv_len );
1948 /* Prefix record content with explicit IV. */
1949 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001950 }
1951 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1952 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001953 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001954 unsigned char i;
1955
1956 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1957
1958 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001959 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001960 }
1961 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001962 {
1963 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001966 }
1967
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001968 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1969 iv, transform->ivlen );
1970 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001971 data - explicit_iv_len, explicit_iv_len );
1972 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1973 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001975 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001976 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001977
Paul Bakker68884e32013-01-07 18:20:04 +01001978 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001979 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001980 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001981
1982 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001983 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001984 iv, transform->ivlen,
1985 add_data, 13, /* add data */
1986 data, rec->data_len, /* source */
1987 data, &rec->data_len, /* destination */
1988 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001991 return( ret );
1992 }
1993
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001994 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1995 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001996
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001997 rec->data_len += transform->taglen + explicit_iv_len;
1998 rec->data_offset -= explicit_iv_len;
1999 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002000 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2004#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002005 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002008 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002009 size_t padlen, i;
2010 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002012 /* Currently we're always using minimal padding
2013 * (up to 255 bytes would be allowed). */
2014 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2015 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 padlen = 0;
2017
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002018 /* Check there's enough space in the buffer for the padding. */
2019 if( post_avail < padlen + 1 )
2020 {
2021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2022 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2023 }
2024
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002026 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002028 rec->data_len += padlen + 1;
2029 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002032 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002033 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2034 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002035 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002036 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002038 if( f_rng == NULL )
2039 {
2040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2042 }
2043
2044 if( rec->data_offset < transform->ivlen )
2045 {
2046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2047 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2048 }
2049
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002050 /*
2051 * Generate IV
2052 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002054 if( ret != 0 )
2055 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002056
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002057 memcpy( data - transform->ivlen, transform->iv_enc,
2058 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002059
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002064 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002065 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002066 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002068 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2069 transform->iv_enc,
2070 transform->ivlen,
2071 data, rec->data_len,
2072 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002075 return( ret );
2076 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002077
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002078 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002082 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002086 {
2087 /*
2088 * Save IV in SSL3 and TLS1
2089 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002090 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2091 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002093 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002094#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002095 {
2096 data -= transform->ivlen;
2097 rec->data_offset -= transform->ivlen;
2098 rec->data_len += transform->ivlen;
2099 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002102 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002103 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002104 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2105
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002106 /*
2107 * MAC(MAC_write_key, seq_num +
2108 * TLSCipherText.type +
2109 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002110 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002111 * IV + // except for TLS 1.0
2112 * ENC(content + padding + padding_length));
2113 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002114
2115 if( post_avail < transform->maclen)
2116 {
2117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2118 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2119 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2123 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002124
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002126
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002127 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2128 sizeof( add_data ) );
2129 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2130 data, rec->data_len );
2131 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2132 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002133
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002135
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 rec->data_len += transform->maclen;
2137 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002138 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002144 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002149
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002150 /* Make extra sure authentication was performed, exactly once */
2151 if( auth_done != 1 )
2152 {
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é-Gonnard352143f2015-01-13 10:59:51 +01002155 }
2156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
2159 return( 0 );
2160}
2161
Hanno Beckera18d1322018-01-03 14:27:32 +00002162int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2163 mbedtls_ssl_transform *transform,
2164 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002165{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002166 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002168 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002169#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002170 size_t padlen = 0, correct = 1;
2171#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002172 unsigned char* data;
2173 unsigned char add_data[13];
2174
Hanno Beckera18d1322018-01-03 14:27:32 +00002175#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002176 ((void) ssl);
2177#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002180 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002181 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2184 }
2185 if( rec == NULL ||
2186 rec->buf == NULL ||
2187 rec->buf_len < rec->data_offset ||
2188 rec->buf_len - rec->data_offset < rec->data_len )
2189 {
2190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002192 }
2193
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002194 data = rec->buf + rec->data_offset;
2195 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2198 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002199 {
2200 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002201 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2202 transform->iv_dec,
2203 transform->ivlen,
2204 data, rec->data_len,
2205 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002208 return( ret );
2209 }
2210
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002211 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 }
Paul Bakker68884e32013-01-07 18:20:04 +01002217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002219#if defined(MBEDTLS_GCM_C) || \
2220 defined(MBEDTLS_CCM_C) || \
2221 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002223 mode == MBEDTLS_MODE_CCM ||
2224 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002225 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002226 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002227 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002228
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002229 /*
2230 * Compute and update sizes
2231 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002232 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002235 "+ taglen (%d)", rec->data_len,
2236 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002238 }
Paul Bakker68884e32013-01-07 18:20:04 +01002239
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002240 /*
2241 * Prepare IV
2242 */
2243 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2244 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002245 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002246 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002247 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002248
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002249 }
2250 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2251 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002252 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002253 unsigned char i;
2254
2255 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2256
2257 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002258 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002259 }
2260 else
2261 {
2262 /* Reminder if we ever add an AEAD mode with a different size */
2263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2265 }
2266
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002267 data += explicit_iv_len;
2268 rec->data_offset += explicit_iv_len;
2269 rec->data_len -= explicit_iv_len + transform->taglen;
2270
2271 ssl_extract_add_data_from_record( add_data, rec );
2272 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2273 add_data, 13 );
2274
2275 memcpy( transform->iv_dec + transform->fixed_ivlen,
2276 data - explicit_iv_len, explicit_iv_len );
2277
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002278 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002279 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002280 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002281
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002282
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002283 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002284 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002285 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002286 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2287 iv, transform->ivlen,
2288 add_data, 13,
2289 data, rec->data_len,
2290 data, &olen,
2291 data + rec->data_len,
2292 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2297 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002298
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002299 return( ret );
2300 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002301 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002302
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002303 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002307 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2311#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002312 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002315 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002316
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 /*
Paul Bakker45829992013-01-03 14:52:21 +01002318 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002321 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2322 {
2323 /* The ciphertext is prefixed with the CBC IV. */
2324 minlen += transform->ivlen;
2325 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002326#endif
Paul Bakker45829992013-01-03 14:52:21 +01002327
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002328 /* Size considerations:
2329 *
2330 * - The CBC cipher text must not be empty and hence
2331 * at least of size transform->ivlen.
2332 *
2333 * Together with the potential IV-prefix, this explains
2334 * the first of the two checks below.
2335 *
2336 * - The record must contain a MAC, either in plain or
2337 * encrypted, depending on whether Encrypt-then-MAC
2338 * is used or not.
2339 * - If it is, the message contains the IV-prefix,
2340 * the CBC ciphertext, and the MAC.
2341 * - If it is not, the padded plaintext, and hence
2342 * the CBC ciphertext, has at least length maclen + 1
2343 * because there is at least the padding length byte.
2344 *
2345 * As the CBC ciphertext is not empty, both cases give the
2346 * lower bound minlen + maclen + 1 on the record size, which
2347 * we test for in the second check below.
2348 */
2349 if( rec->data_len < minlen + transform->ivlen ||
2350 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002353 "+ 1 ) ( + expl IV )", rec->data_len,
2354 transform->ivlen,
2355 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002357 }
2358
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002359 /*
2360 * Authenticate before decrypt if enabled
2361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002363 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002364 {
Hanno Becker992b6872017-11-09 18:57:39 +00002365 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002368
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002369 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2370 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002371
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002372 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002373
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002374 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2375 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2376 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2377 data, rec->data_len );
2378 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2379 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002380
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2382 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002383 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002384 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002385
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002386 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2387 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002391 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002392 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002393 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002395
2396 /*
2397 * Check length sanity
2398 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002399 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002402 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002404 }
2405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002408 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002409 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002410 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002412 /* This is safe because data_len >= minlen + maclen + 1 initially,
2413 * and at this point we have at most subtracted maclen (note that
2414 * minlen == transform->ivlen here). */
2415 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002417 data += transform->ivlen;
2418 rec->data_offset += transform->ivlen;
2419 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002423 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2424 transform->iv_dec, transform->ivlen,
2425 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002428 return( ret );
2429 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002430
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002431 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002435 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002438 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002439 {
2440 /*
2441 * Save IV in SSL3 and TLS1
2442 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002443 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2444 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002448 /* Safe since data_len >= minlen + maclen + 1, so after having
2449 * subtracted at most minlen and maclen up to this point,
2450 * data_len > 0. */
2451 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002452
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002453 if( auth_done == 1 )
2454 {
2455 correct *= ( rec->data_len >= padlen + 1 );
2456 padlen *= ( rec->data_len >= padlen + 1 );
2457 }
2458 else
Paul Bakker45829992013-01-03 14:52:21 +01002459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002461 if( rec->data_len < transform->maclen + padlen + 1 )
2462 {
2463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2464 rec->data_len,
2465 transform->maclen,
2466 padlen + 1 ) );
2467 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002468#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002469
2470 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2471 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002474 padlen++;
2475
2476 /* Regardless of the validity of the padding,
2477 * we have data_len >= padlen here. */
2478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002480 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002482 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_DEBUG_ALL)
2485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002486 "should be no more than %d",
2487 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002488#endif
Paul Bakker45829992013-01-03 14:52:21 +01002489 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 }
2491 }
2492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2495 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002496 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002498 /* The padding check involves a series of up to 256
2499 * consecutive memory reads at the end of the record
2500 * plaintext buffer. In order to hide the length and
2501 * validity of the padding, always perform exactly
2502 * `min(256,plaintext_len)` reads (but take into account
2503 * only the last `padlen` bytes for the padding check). */
2504 size_t pad_count = 0;
2505 size_t real_count = 0;
2506 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002507
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002508 /* Index of first padding byte; it has been ensured above
2509 * that the subtraction is safe. */
2510 size_t const padding_idx = rec->data_len - padlen;
2511 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2512 size_t const start_idx = rec->data_len - num_checks;
2513 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002514
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002515 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002516 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002517 real_count |= ( idx >= padding_idx );
2518 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002519 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002520 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002523 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002525#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002526 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002528 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2530 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002534 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002535
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 /* If the padding was found to be invalid, padlen == 0
2537 * and the subtraction is safe. If the padding was found valid,
2538 * padlen hasn't been changed and the previous assertion
2539 * data_len >= padlen still holds. */
2540 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002542 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002544 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002550#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
2555 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002556 * Authenticate if not done yet.
2557 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 */
Hanno Becker52344c22018-01-03 15:24:20 +00002559#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002560 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561 {
Hanno Becker992b6872017-11-09 18:57:39 +00002562 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002563
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 /* If the initial value of padlen was such that
2565 * data_len < maclen + padlen + 1, then padlen
2566 * got reset to 1, and the initial check
2567 * data_len >= minlen + maclen + 1
2568 * guarantees that at this point we still
2569 * have at least data_len >= maclen.
2570 *
2571 * If the initial value of padlen was such that
2572 * data_len >= maclen + padlen + 1, then we have
2573 * subtracted either padlen + 1 (if the padding was correct)
2574 * or 0 (if the padding was incorrect) since then,
2575 * hence data_len >= maclen in any case.
2576 */
2577 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002579 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002582 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002583 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 ssl_mac( &transform->md_ctx_dec,
2585 transform->mac_dec,
2586 data, rec->data_len,
2587 rec->ctr, rec->type,
2588 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002589 }
2590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2592#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2593 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 {
2596 /*
2597 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002598 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002599 *
2600 * Known timing attacks:
2601 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2602 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002603 * To compensate for different timings for the MAC calculation
2604 * depending on how much padding was removed (which is determined
2605 * by padlen), process extra_run more blocks through the hash
2606 * function.
2607 *
2608 * The formula in the paper is
2609 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2610 * where L1 is the size of the header plus the decrypted message
2611 * plus CBC padding and L2 is the size of the header plus the
2612 * decrypted message. This is for an underlying hash function
2613 * with 64-byte blocks.
2614 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2615 * correctly. We round down instead of up, so -56 is the correct
2616 * value for our calculations instead of -55.
2617 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002618 * Repeat the formula rather than defining a block_size variable.
2619 * This avoids requiring division by a variable at runtime
2620 * (which would be marginally less efficient and would require
2621 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002622 */
2623 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002624 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002625
2626 /*
2627 * The next two sizes are the minimum and maximum values of
2628 * in_msglen over all padlen values.
2629 *
2630 * They're independent of padlen, since we previously did
2631 * in_msglen -= padlen.
2632 *
2633 * Note that max_len + maclen is never more than the buffer
2634 * length, as we previously did in_msglen -= maclen too.
2635 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002636 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002637 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2638
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 memset( tmp, 0, sizeof( tmp ) );
2640
2641 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002642 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002643#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2644 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002645 case MBEDTLS_MD_MD5:
2646 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002647 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002648 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2650 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002651 break;
2652#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002653#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002654 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002655 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2657 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002658 break;
2659#endif
2660 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2663 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002664
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002665 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002666
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2668 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2669 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002670 /* Make sure we access everything even when padlen > 0. This
2671 * makes the synchronisation requirements for just-in-time
2672 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 ssl_read_memory( data + rec->data_len, padlen );
2674 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002675
2676 /* Call mbedtls_md_process at least once due to cache attacks
2677 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002678 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002680
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002682
2683 /* Make sure we access all the memory that could contain the MAC,
2684 * before we check it in the next code block. This makes the
2685 * synchronisation requirements for just-in-time Prime+Probe
2686 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 ssl_read_memory( data + min_len,
2688 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002689 }
2690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2692 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002698#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2700 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002701#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2704 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_SSL_DEBUG_ALL)
2707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002708#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 correct = 0;
2710 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002711 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002713
2714 /*
2715 * Finally check the correct flag
2716 */
2717 if( correct == 0 )
2718 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002719#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002720
2721 /* Make extra sure authentication was performed, exactly once */
2722 if( auth_done != 1 )
2723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
2730 return( 0 );
2731}
2732
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002733#undef MAC_NONE
2734#undef MAC_PLAINTEXT
2735#undef MAC_CIPHERTEXT
2736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002738/*
2739 * Compression/decompression functions
2740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002742{
2743 int ret;
2744 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002745 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002747 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002750
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002751 if( len_pre == 0 )
2752 return( 0 );
2753
Paul Bakker2770fbd2012-07-03 13:30:23 +00002754 memcpy( msg_pre, ssl->out_msg, len_pre );
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757 ssl->out_msglen ) );
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002760 ssl->out_msg, ssl->out_msglen );
2761
Paul Bakker48916f92012-09-16 19:57:18 +00002762 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2763 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2764 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002765 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002768 if( ret != Z_OK )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2771 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772 }
2773
Angus Grattond8213d02016-05-25 20:56:48 +10002774 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002775 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002778 ssl->out_msglen ) );
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 ssl->out_msg, ssl->out_msglen );
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784
2785 return( 0 );
2786}
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789{
2790 int ret;
2791 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002792 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002793 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002794 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002797
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002798 if( len_pre == 0 )
2799 return( 0 );
2800
Paul Bakker2770fbd2012-07-03 13:30:23 +00002801 memcpy( msg_pre, ssl->in_msg, len_pre );
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804 ssl->in_msglen ) );
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002807 ssl->in_msg, ssl->in_msglen );
2808
Paul Bakker48916f92012-09-16 19:57:18 +00002809 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2810 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2811 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002812 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002813 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814
Paul Bakker48916f92012-09-16 19:57:18 +00002815 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002816 if( ret != Z_OK )
2817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2819 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002820 }
2821
Angus Grattond8213d02016-05-25 20:56:48 +10002822 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002823 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002826 ssl->in_msglen ) );
2827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002829 ssl->in_msg, ssl->in_msglen );
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002832
2833 return( 0 );
2834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2838static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_SSL_PROTO_DTLS)
2841static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002842{
2843 /* If renegotiation is not enforced, retransmit until we would reach max
2844 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002845 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002846 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002847 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002848 unsigned char doublings = 1;
2849
2850 while( ratio != 0 )
2851 {
2852 ++doublings;
2853 ratio >>= 1;
2854 }
2855
2856 if( ++ssl->renego_records_seen > doublings )
2857 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002859 return( 0 );
2860 }
2861 }
2862
2863 return( ssl_write_hello_request( ssl ) );
2864}
2865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002867
Paul Bakker5121ce52009-01-03 21:22:43 +00002868/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002869 * Fill the input message buffer by appending data to it.
2870 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002871 *
2872 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2873 * available (from this read and/or a previous one). Otherwise, an error code
2874 * is returned (possibly EOF or WANT_READ).
2875 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002876 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2877 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2878 * since we always read a whole datagram at once.
2879 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002880 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002881 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002884{
Paul Bakker23986e52011-04-24 08:57:21 +00002885 int ret;
2886 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002890 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002893 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002895 }
2896
Angus Grattond8213d02016-05-25 20:56:48 +10002897 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002901 }
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002906 uint32_t timeout;
2907
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002908 /* Just to be sure */
2909 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2910 {
2911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2912 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2914 }
2915
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002916 /*
2917 * The point is, we need to always read a full datagram at once, so we
2918 * sometimes read more then requested, and handle the additional data.
2919 * It could be the rest of the current record (while fetching the
2920 * header) and/or some other records in the same datagram.
2921 */
2922
2923 /*
2924 * Move to the next record in the already read datagram if applicable
2925 */
2926 if( ssl->next_record_offset != 0 )
2927 {
2928 if( ssl->in_left < ssl->next_record_offset )
2929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002932 }
2933
2934 ssl->in_left -= ssl->next_record_offset;
2935
2936 if( ssl->in_left != 0 )
2937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002939 ssl->next_record_offset ) );
2940 memmove( ssl->in_hdr,
2941 ssl->in_hdr + ssl->next_record_offset,
2942 ssl->in_left );
2943 }
2944
2945 ssl->next_record_offset = 0;
2946 }
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002950
2951 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002952 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002953 */
2954 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002957 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002958 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002959
2960 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002961 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002962 * are not at the beginning of a new record, the caller did something
2963 * wrong.
2964 */
2965 if( ssl->in_left != 0 )
2966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002969 }
2970
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971 /*
2972 * Don't even try to read if time's out already.
2973 * This avoids by-passing the timer when repeatedly receiving messages
2974 * that will end up being dropped.
2975 */
2976 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002977 {
2978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002979 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002980 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002981 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002982 {
Angus Grattond8213d02016-05-25 20:56:48 +10002983 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002986 timeout = ssl->handshake->retransmit_timeout;
2987 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002991
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002992 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002993 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2994 timeout );
2995 else
2996 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002999
3000 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003002 }
3003
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003004 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003007 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003010 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003011 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003014 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003015 }
3016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003020 return( ret );
3021 }
3022
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003023 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003026 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003028 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003029 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003032 return( ret );
3033 }
3034
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003035 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003036 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003038 }
3039
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 if( ret < 0 )
3041 return( ret );
3042
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003043 ssl->in_left = ret;
3044 }
3045 else
3046#endif
3047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003049 ssl->in_left, nb_want ) );
3050
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003051 while( ssl->in_left < nb_want )
3052 {
3053 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003054
3055 if( ssl_check_timer( ssl ) != 0 )
3056 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3057 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003058 {
3059 if( ssl->f_recv_timeout != NULL )
3060 {
3061 ret = ssl->f_recv_timeout( ssl->p_bio,
3062 ssl->in_hdr + ssl->in_left, len,
3063 ssl->conf->read_timeout );
3064 }
3065 else
3066 {
3067 ret = ssl->f_recv( ssl->p_bio,
3068 ssl->in_hdr + ssl->in_left, len );
3069 }
3070 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003073 ssl->in_left, nb_want ) );
3074 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003075
3076 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003078
3079 if( ret < 0 )
3080 return( ret );
3081
mohammad160352aecb92018-03-28 23:41:40 -07003082 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003083 {
Darryl Green11999bb2018-03-13 15:22:58 +00003084 MBEDTLS_SSL_DEBUG_MSG( 1,
3085 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003086 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3088 }
3089
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003090 ssl->in_left += ret;
3091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 }
3093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
3096 return( 0 );
3097}
3098
3099/*
3100 * Flush any data not yet written
3101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003103{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003104 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003105 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003109 if( ssl->f_send == NULL )
3110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003112 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003114 }
3115
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003116 /* Avoid incrementing counter if data is flushed */
3117 if( ssl->out_left == 0 )
3118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003120 return( 0 );
3121 }
3122
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 while( ssl->out_left > 0 )
3124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3126 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
Hanno Becker2b1e3542018-08-06 11:19:13 +01003128 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003129 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
3133 if( ret <= 0 )
3134 return( ret );
3135
mohammad160352aecb92018-03-28 23:41:40 -07003136 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003137 {
Darryl Green11999bb2018-03-13 15:22:58 +00003138 MBEDTLS_SSL_DEBUG_MSG( 1,
3139 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003140 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3142 }
3143
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 ssl->out_left -= ret;
3145 }
3146
Hanno Becker2b1e3542018-08-06 11:19:13 +01003147#if defined(MBEDTLS_SSL_PROTO_DTLS)
3148 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003149 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003150 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003151 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003152 else
3153#endif
3154 {
3155 ssl->out_hdr = ssl->out_buf + 8;
3156 }
3157 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
3161 return( 0 );
3162}
3163
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003164/*
3165 * Functions to handle the DTLS retransmission state machine
3166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003168/*
3169 * Append current handshake message to current outgoing flight
3170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3175 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3176 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177
3178 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003179 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003183 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184 }
3185
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003186 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == 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", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 mbedtls_free( msg );
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
3193 /* Copy current handshake message with headers */
3194 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3195 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003197 msg->next = NULL;
3198
3199 /* Append to the current flight */
3200 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003201 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202 else
3203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003205 while( cur->next != NULL )
3206 cur = cur->next;
3207 cur->next = msg;
3208 }
3209
Hanno Becker3b235902018-08-06 09:54:53 +01003210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003211 return( 0 );
3212}
3213
3214/*
3215 * Free the current flight of handshake messages
3216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 mbedtls_ssl_flight_item *cur = flight;
3220 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003221
3222 while( cur != NULL )
3223 {
3224 next = cur->next;
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 mbedtls_free( cur->p );
3227 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003228
3229 cur = next;
3230 }
3231}
3232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3234static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003235#endif
3236
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003237/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003238 * Swap transform_out and out_ctr with the alternative ones
3239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003243 unsigned char tmp_out_ctr[8];
3244
3245 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003248 return;
3249 }
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003253 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003254 tmp_transform = ssl->transform_out;
3255 ssl->transform_out = ssl->handshake->alt_transform_out;
3256 ssl->handshake->alt_transform_out = tmp_transform;
3257
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003258 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003259 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3260 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003262
3263 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003264 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3267 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3272 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003273 }
3274 }
3275#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003276}
3277
3278/*
3279 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003280 */
3281int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3282{
3283 int ret = 0;
3284
3285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3286
3287 ret = mbedtls_ssl_flight_transmit( ssl );
3288
3289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3290
3291 return( ret );
3292}
3293
3294/*
3295 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003296 *
3297 * Need to remember the current message in case flush_output returns
3298 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003299 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003300 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003301int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003303 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003307 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003309
3310 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003311 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003312 ssl_swap_epochs( ssl );
3313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003315 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316
3317 while( ssl->handshake->cur_msg != NULL )
3318 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003320 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003321
Hanno Beckere1dcb032018-08-17 16:47:58 +01003322 int const is_finished =
3323 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3324 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3325
Hanno Becker04da1892018-08-14 13:22:10 +01003326 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3327 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3328
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003329 /* Swap epochs before sending Finished: we can't do it after
3330 * sending ChangeCipherSpec, in case write returns WANT_READ.
3331 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003332 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003333 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003335 ssl_swap_epochs( ssl );
3336 }
3337
Hanno Becker67bc7c32018-08-06 11:33:50 +01003338 ret = ssl_get_remaining_payload_in_datagram( ssl );
3339 if( ret < 0 )
3340 return( ret );
3341 max_frag_len = (size_t) ret;
3342
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003343 /* CCS is copied as is, while HS messages may need fragmentation */
3344 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3345 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003346 if( max_frag_len == 0 )
3347 {
3348 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3349 return( ret );
3350
3351 continue;
3352 }
3353
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003354 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003355 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003356 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003358 /* Update position inside current message */
3359 ssl->handshake->cur_msg_p += cur->len;
3360 }
3361 else
3362 {
3363 const unsigned char * const p = ssl->handshake->cur_msg_p;
3364 const size_t hs_len = cur->len - 12;
3365 const size_t frag_off = p - ( cur->p + 12 );
3366 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003367 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
Hanno Beckere1dcb032018-08-17 16:47:58 +01003369 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003370 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003371 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003372 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
Hanno Becker67bc7c32018-08-06 11:33:50 +01003374 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3375 return( ret );
3376
3377 continue;
3378 }
3379 max_hs_frag_len = max_frag_len - 12;
3380
3381 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3382 max_hs_frag_len : rem_len;
3383
3384 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003385 {
3386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003387 (unsigned) cur_hs_frag_len,
3388 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003389 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003390
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003391 /* Messages are stored with handshake headers as if not fragmented,
3392 * copy beginning of headers then fill fragmentation fields.
3393 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3394 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003395
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003396 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3397 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3398 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3399
Hanno Becker67bc7c32018-08-06 11:33:50 +01003400 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3401 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3402 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003403
3404 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3405
Hanno Becker3f7b9732018-08-28 09:53:25 +01003406 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003407 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3408 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003409 ssl->out_msgtype = cur->type;
3410
3411 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003412 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003413 }
3414
3415 /* If done with the current message move to the next one if any */
3416 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3417 {
3418 if( cur->next != NULL )
3419 {
3420 ssl->handshake->cur_msg = cur->next;
3421 ssl->handshake->cur_msg_p = cur->next->p + 12;
3422 }
3423 else
3424 {
3425 ssl->handshake->cur_msg = NULL;
3426 ssl->handshake->cur_msg_p = NULL;
3427 }
3428 }
3429
3430 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003431 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003434 return( ret );
3435 }
3436 }
3437
Hanno Becker67bc7c32018-08-06 11:33:50 +01003438 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3439 return( ret );
3440
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003441 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3443 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003444 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003447 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3448 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003449
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
3452 return( 0 );
3453}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454
3455/*
3456 * To be called when the last message of an incoming flight is received.
3457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459{
3460 /* We won't need to resend that one any more */
3461 ssl_flight_free( ssl->handshake->flight );
3462 ssl->handshake->flight = NULL;
3463 ssl->handshake->cur_msg = NULL;
3464
3465 /* The next incoming flight will start with this msg_seq */
3466 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3467
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003468 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003469 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003470
Hanno Becker0271f962018-08-16 13:23:47 +01003471 /* Clear future message buffering structure. */
3472 ssl_buffering_free( ssl );
3473
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003474 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003475 ssl_set_timer( ssl, 0 );
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3478 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003481 }
3482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003484}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003485
3486/*
3487 * To be called when the last message of an outgoing flight is send.
3488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003490{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003491 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003492 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3495 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003498 }
3499 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Paul Bakker5121ce52009-01-03 21:22:43 +00003504/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507
3508/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003509 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003510 *
3511 * - fill in handshake headers
3512 * - update handshake checksum
3513 * - DTLS: save message for resending
3514 * - then pass to the record layer
3515 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003516 * DTLS: except for HelloRequest, messages are only queued, and will only be
3517 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003518 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003519 * Inputs:
3520 * - ssl->out_msglen: 4 + actual handshake message len
3521 * (4 is the size of handshake headers for TLS)
3522 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3523 * - ssl->out_msg + 4: the handshake message body
3524 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003525 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003526 * - ssl->out_msglen: the length of the record contents
3527 * (including handshake headers but excluding record headers)
3528 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003529 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003530int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003531{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003532 int ret;
3533 const size_t hs_len = ssl->out_msglen - 4;
3534 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3537
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003538 /*
3539 * Sanity checks
3540 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003541 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003542 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3543 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003544 /* In SSLv3, the client might send a NoCertificate alert. */
3545#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3546 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3547 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3548 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3549#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3550 {
3551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3553 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003556 /* Whenever we send anything different from a
3557 * HelloRequest we should be in a handshake - double check. */
3558 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3559 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003560 ssl->handshake == NULL )
3561 {
3562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003568 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003574#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003575
Hanno Beckerb50a2532018-08-06 11:52:54 +01003576 /* Double-check that we did not exceed the bounds
3577 * of the outgoing record buffer.
3578 * This should never fail as the various message
3579 * writing functions must obey the bounds of the
3580 * outgoing record buffer, but better be safe.
3581 *
3582 * Note: We deliberately do not check for the MTU or MFL here.
3583 */
3584 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3585 {
3586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3587 "size %u, maximum %u",
3588 (unsigned) ssl->out_msglen,
3589 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3591 }
3592
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003593 /*
3594 * Fill handshake headers
3595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003598 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3599 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3600 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003602 /*
3603 * DTLS has additional fields in the Handshake layer,
3604 * between the length field and the actual payload:
3605 * uint16 message_seq;
3606 * uint24 fragment_offset;
3607 * uint24 fragment_length;
3608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003611 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003612 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003613 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003614 {
3615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3616 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003617 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003618 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3620 }
3621
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003622 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003623 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003624
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003625 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003626 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003627 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003628 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3629 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3630 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003631 }
3632 else
3633 {
3634 ssl->out_msg[4] = 0;
3635 ssl->out_msg[5] = 0;
3636 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003637
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003638 /* Handshake hashes are computed without fragmentation,
3639 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003640 memset( ssl->out_msg + 6, 0x00, 3 );
3641 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003644
Hanno Becker0207e532018-08-28 10:28:28 +01003645 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003646 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3647 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 }
3649
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003650 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003653 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3654 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003655 {
3656 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659 return( ret );
3660 }
3661 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003663#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003664 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003665 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003666 {
3667 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3668 return( ret );
3669 }
3670 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003671
3672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3673
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003674 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003675}
3676
3677/*
3678 * Record layer functions
3679 */
3680
3681/*
3682 * Write current record.
3683 *
3684 * Uses:
3685 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3686 * - ssl->out_msglen: length of the record content (excl headers)
3687 * - ssl->out_msg: record content
3688 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003689int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003690{
3691 int ret, done = 0;
3692 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003693 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003694
3695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003698 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003700 {
3701 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003704 return( ret );
3705 }
3706
3707 len = ssl->out_msglen;
3708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3712 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 ret = mbedtls_ssl_hw_record_write( ssl );
3717 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3720 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003721 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003722
3723 if( ret == 0 )
3724 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003727 if( !done )
3728 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003729 unsigned i;
3730 size_t protected_record_size;
3731
Paul Bakker05ef8352012-05-08 09:17:57 +00003732 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003734 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003735
Hanno Becker19859472018-08-06 09:40:20 +01003736 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003737 ssl->out_len[0] = (unsigned char)( len >> 8 );
3738 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003741 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003742 mbedtls_record rec;
3743
3744 rec.buf = ssl->out_iv;
3745 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3746 ( ssl->out_iv - ssl->out_buf );
3747 rec.data_len = ssl->out_msglen;
3748 rec.data_offset = ssl->out_msg - rec.buf;
3749
3750 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3751 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3752 ssl->conf->transport, rec.ver );
3753 rec.type = ssl->out_msgtype;
3754
Hanno Beckera18d1322018-01-03 14:27:32 +00003755 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003756 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003759 return( ret );
3760 }
3761
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003762 if( rec.data_offset != 0 )
3763 {
3764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3766 }
3767
Hanno Becker78f839d2019-03-14 12:56:23 +00003768 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003769 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3770 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003771 }
3772
Hanno Becker2b1e3542018-08-06 11:19:13 +01003773 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3774
3775#if defined(MBEDTLS_SSL_PROTO_DTLS)
3776 /* In case of DTLS, double-check that we don't exceed
3777 * the remaining space in the datagram. */
3778 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3779 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003780 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003781 if( ret < 0 )
3782 return( ret );
3783
3784 if( protected_record_size > (size_t) ret )
3785 {
3786 /* Should never happen */
3787 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3788 }
3789 }
3790#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003793 "version = [%d:%d], msglen = %d",
3794 ssl->out_hdr[0], ssl->out_hdr[1],
3795 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003798 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003799
3800 ssl->out_left += protected_record_size;
3801 ssl->out_hdr += protected_record_size;
3802 ssl_update_out_pointers( ssl, ssl->transform_out );
3803
Hanno Becker04484622018-08-06 09:49:38 +01003804 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3805 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3806 break;
3807
3808 /* The loop goes to its end iff the counter is wrapping */
3809 if( i == ssl_ep_len( ssl ) )
3810 {
3811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3812 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 }
3815
Hanno Becker67bc7c32018-08-06 11:33:50 +01003816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3818 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003820 size_t remaining;
3821 ret = ssl_get_remaining_payload_in_datagram( ssl );
3822 if( ret < 0 )
3823 {
3824 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3825 ret );
3826 return( ret );
3827 }
3828
3829 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003830 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003831 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003832 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003833 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003834 else
3835 {
Hanno Becker513815a2018-08-20 11:56:09 +01003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003837 }
3838 }
3839#endif /* MBEDTLS_SSL_PROTO_DTLS */
3840
3841 if( ( flush == SSL_FORCE_FLUSH ) &&
3842 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 return( ret );
3846 }
3847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
3850 return( 0 );
3851}
3852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003854
3855static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3856{
3857 if( ssl->in_msglen < ssl->in_hslen ||
3858 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3859 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3860 {
3861 return( 1 );
3862 }
3863 return( 0 );
3864}
Hanno Becker44650b72018-08-16 12:51:11 +01003865
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003866static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003867{
3868 return( ( ssl->in_msg[9] << 16 ) |
3869 ( ssl->in_msg[10] << 8 ) |
3870 ssl->in_msg[11] );
3871}
3872
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003873static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003874{
3875 return( ( ssl->in_msg[6] << 16 ) |
3876 ( ssl->in_msg[7] << 8 ) |
3877 ssl->in_msg[8] );
3878}
3879
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003880static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003881{
3882 uint32_t msg_len, frag_off, frag_len;
3883
3884 msg_len = ssl_get_hs_total_len( ssl );
3885 frag_off = ssl_get_hs_frag_off( ssl );
3886 frag_len = ssl_get_hs_frag_len( ssl );
3887
3888 if( frag_off > msg_len )
3889 return( -1 );
3890
3891 if( frag_len > msg_len - frag_off )
3892 return( -1 );
3893
3894 if( frag_len + 12 > ssl->in_msglen )
3895 return( -1 );
3896
3897 return( 0 );
3898}
3899
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003900/*
3901 * Mark bits in bitmask (used for DTLS HS reassembly)
3902 */
3903static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3904{
3905 unsigned int start_bits, end_bits;
3906
3907 start_bits = 8 - ( offset % 8 );
3908 if( start_bits != 8 )
3909 {
3910 size_t first_byte_idx = offset / 8;
3911
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003912 /* Special case */
3913 if( len <= start_bits )
3914 {
3915 for( ; len != 0; len-- )
3916 mask[first_byte_idx] |= 1 << ( start_bits - len );
3917
3918 /* Avoid potential issues with offset or len becoming invalid */
3919 return;
3920 }
3921
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003922 offset += start_bits; /* Now offset % 8 == 0 */
3923 len -= start_bits;
3924
3925 for( ; start_bits != 0; start_bits-- )
3926 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3927 }
3928
3929 end_bits = len % 8;
3930 if( end_bits != 0 )
3931 {
3932 size_t last_byte_idx = ( offset + len ) / 8;
3933
3934 len -= end_bits; /* Now len % 8 == 0 */
3935
3936 for( ; end_bits != 0; end_bits-- )
3937 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3938 }
3939
3940 memset( mask + offset / 8, 0xFF, len / 8 );
3941}
3942
3943/*
3944 * Check that bitmask is full
3945 */
3946static int ssl_bitmask_check( unsigned char *mask, size_t len )
3947{
3948 size_t i;
3949
3950 for( i = 0; i < len / 8; i++ )
3951 if( mask[i] != 0xFF )
3952 return( -1 );
3953
3954 for( i = 0; i < len % 8; i++ )
3955 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3956 return( -1 );
3957
3958 return( 0 );
3959}
3960
Hanno Becker56e205e2018-08-16 09:06:12 +01003961/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003962static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003963 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003964{
Hanno Becker56e205e2018-08-16 09:06:12 +01003965 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003966
Hanno Becker56e205e2018-08-16 09:06:12 +01003967 alloc_len = 12; /* Handshake header */
3968 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969
Hanno Beckerd07df862018-08-16 09:14:58 +01003970 if( add_bitmap )
3971 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003972
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003973 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003974}
Hanno Becker56e205e2018-08-16 09:06:12 +01003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003977
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003978static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003979{
3980 return( ( ssl->in_msg[1] << 16 ) |
3981 ( ssl->in_msg[2] << 8 ) |
3982 ssl->in_msg[3] );
3983}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003984
Simon Butcher99000142016-10-13 17:21:01 +01003985int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003990 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003992 }
3993
Hanno Becker12555c62018-08-16 12:47:53 +01003994 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003997 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003998 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004002 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004003 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004004 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004005
Hanno Becker44650b72018-08-16 12:51:11 +01004006 if( ssl_check_hs_header( ssl ) != 0 )
4007 {
4008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4009 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4010 }
4011
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004012 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004013 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4014 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4015 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4016 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004017 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004018 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4019 {
4020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4021 recv_msg_seq,
4022 ssl->handshake->in_msg_seq ) );
4023 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4024 }
4025
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004026 /* Retransmit only on last message from previous flight, to avoid
4027 * too many retransmissions.
4028 * Besides, No sane server ever retransmits HelloVerifyRequest */
4029 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004033 "message_seq = %d, start_of_flight = %d",
4034 recv_msg_seq,
4035 ssl->handshake->in_flight_start_seq ) );
4036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004040 return( ret );
4041 }
4042 }
4043 else
4044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004046 "message_seq = %d, expected = %d",
4047 recv_msg_seq,
4048 ssl->handshake->in_msg_seq ) );
4049 }
4050
Hanno Becker90333da2017-10-10 11:27:13 +01004051 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004052 }
4053 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004054
Hanno Becker6d97ef52018-08-16 13:09:04 +01004055 /* Message reassembly is handled alongside buffering of future
4056 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004057 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004058 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004059 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004062 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004063 }
4064 }
4065 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004067 /* With TLS we don't handle fragmentation (for now) */
4068 if( ssl->in_msglen < ssl->in_hslen )
4069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4071 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004072 }
4073
Simon Butcher99000142016-10-13 17:21:01 +01004074 return( 0 );
4075}
4076
4077void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4078{
Hanno Becker0271f962018-08-16 13:23:47 +01004079 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004080
Hanno Becker0271f962018-08-16 13:23:47 +01004081 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004082 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004083 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004084 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004085
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004086 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004089 ssl->handshake != NULL )
4090 {
Hanno Becker0271f962018-08-16 13:23:47 +01004091 unsigned offset;
4092 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004093
Hanno Becker0271f962018-08-16 13:23:47 +01004094 /* Increment handshake sequence number */
4095 hs->in_msg_seq++;
4096
4097 /*
4098 * Clear up handshake buffering and reassembly structure.
4099 */
4100
4101 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004102 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004103
4104 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004105 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4106 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004107 offset++, hs_buf++ )
4108 {
4109 *hs_buf = *(hs_buf + 1);
4110 }
4111
4112 /* Create a fresh last entry */
4113 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004114 }
4115#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004116}
4117
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004118/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119 * DTLS anti-replay: RFC 6347 4.1.2.6
4120 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004121 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4122 * Bit n is set iff record number in_window_top - n has been seen.
4123 *
4124 * Usually, in_window_top is the last record number seen and the lsb of
4125 * in_window is set. The only exception is the initial state (record number 0
4126 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4129static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004130{
4131 ssl->in_window_top = 0;
4132 ssl->in_window = 0;
4133}
4134
4135static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4136{
4137 return( ( (uint64_t) buf[0] << 40 ) |
4138 ( (uint64_t) buf[1] << 32 ) |
4139 ( (uint64_t) buf[2] << 24 ) |
4140 ( (uint64_t) buf[3] << 16 ) |
4141 ( (uint64_t) buf[4] << 8 ) |
4142 ( (uint64_t) buf[5] ) );
4143}
4144
4145/*
4146 * Return 0 if sequence number is acceptable, -1 otherwise
4147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004149{
4150 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4151 uint64_t bit;
4152
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004153 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004154 return( 0 );
4155
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004156 if( rec_seqnum > ssl->in_window_top )
4157 return( 0 );
4158
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004159 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004160
4161 if( bit >= 64 )
4162 return( -1 );
4163
4164 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4165 return( -1 );
4166
4167 return( 0 );
4168}
4169
4170/*
4171 * Update replay window on new validated record
4172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004174{
4175 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004177 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004178 return;
4179
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004180 if( rec_seqnum > ssl->in_window_top )
4181 {
4182 /* Update window_top and the contents of the window */
4183 uint64_t shift = rec_seqnum - ssl->in_window_top;
4184
4185 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004186 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004188 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004189 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004190 ssl->in_window |= 1;
4191 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004192
4193 ssl->in_window_top = rec_seqnum;
4194 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004195 else
4196 {
4197 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004198 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004199
4200 if( bit < 64 ) /* Always true, but be extra sure */
4201 ssl->in_window |= (uint64_t) 1 << bit;
4202 }
4203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004205
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004206#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004207/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004208static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4209
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004210/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004211 * Without any SSL context, check if a datagram looks like a ClientHello with
4212 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004213 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004214 *
4215 * - if cookie is valid, return 0
4216 * - if ClientHello looks superficially valid but cookie is not,
4217 * fill obuf and set olen, then
4218 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4219 * - otherwise return a specific error code
4220 */
4221static int ssl_check_dtls_clihlo_cookie(
4222 mbedtls_ssl_cookie_write_t *f_cookie_write,
4223 mbedtls_ssl_cookie_check_t *f_cookie_check,
4224 void *p_cookie,
4225 const unsigned char *cli_id, size_t cli_id_len,
4226 const unsigned char *in, size_t in_len,
4227 unsigned char *obuf, size_t buf_len, size_t *olen )
4228{
4229 size_t sid_len, cookie_len;
4230 unsigned char *p;
4231
4232 if( f_cookie_write == NULL || f_cookie_check == NULL )
4233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4234
4235 /*
4236 * Structure of ClientHello with record and handshake headers,
4237 * and expected values. We don't need to check a lot, more checks will be
4238 * done when actually parsing the ClientHello - skipping those checks
4239 * avoids code duplication and does not make cookie forging any easier.
4240 *
4241 * 0-0 ContentType type; copied, must be handshake
4242 * 1-2 ProtocolVersion version; copied
4243 * 3-4 uint16 epoch; copied, must be 0
4244 * 5-10 uint48 sequence_number; copied
4245 * 11-12 uint16 length; (ignored)
4246 *
4247 * 13-13 HandshakeType msg_type; (ignored)
4248 * 14-16 uint24 length; (ignored)
4249 * 17-18 uint16 message_seq; copied
4250 * 19-21 uint24 fragment_offset; copied, must be 0
4251 * 22-24 uint24 fragment_length; (ignored)
4252 *
4253 * 25-26 ProtocolVersion client_version; (ignored)
4254 * 27-58 Random random; (ignored)
4255 * 59-xx SessionID session_id; 1 byte len + sid_len content
4256 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4257 * ...
4258 *
4259 * Minimum length is 61 bytes.
4260 */
4261 if( in_len < 61 ||
4262 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4263 in[3] != 0 || in[4] != 0 ||
4264 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4265 {
4266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4267 }
4268
4269 sid_len = in[59];
4270 if( sid_len > in_len - 61 )
4271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4272
4273 cookie_len = in[60 + sid_len];
4274 if( cookie_len > in_len - 60 )
4275 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4276
4277 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4278 cli_id, cli_id_len ) == 0 )
4279 {
4280 /* Valid cookie */
4281 return( 0 );
4282 }
4283
4284 /*
4285 * If we get here, we've got an invalid cookie, let's prepare HVR.
4286 *
4287 * 0-0 ContentType type; copied
4288 * 1-2 ProtocolVersion version; copied
4289 * 3-4 uint16 epoch; copied
4290 * 5-10 uint48 sequence_number; copied
4291 * 11-12 uint16 length; olen - 13
4292 *
4293 * 13-13 HandshakeType msg_type; hello_verify_request
4294 * 14-16 uint24 length; olen - 25
4295 * 17-18 uint16 message_seq; copied
4296 * 19-21 uint24 fragment_offset; copied
4297 * 22-24 uint24 fragment_length; olen - 25
4298 *
4299 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4300 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4301 *
4302 * Minimum length is 28.
4303 */
4304 if( buf_len < 28 )
4305 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4306
4307 /* Copy most fields and adapt others */
4308 memcpy( obuf, in, 25 );
4309 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4310 obuf[25] = 0xfe;
4311 obuf[26] = 0xff;
4312
4313 /* Generate and write actual cookie */
4314 p = obuf + 28;
4315 if( f_cookie_write( p_cookie,
4316 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4317 {
4318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4319 }
4320
4321 *olen = p - obuf;
4322
4323 /* Go back and fill length fields */
4324 obuf[27] = (unsigned char)( *olen - 28 );
4325
4326 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4327 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4328 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4329
4330 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4331 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4332
4333 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4334}
4335
4336/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004337 * Handle possible client reconnect with the same UDP quadruplet
4338 * (RFC 6347 Section 4.2.8).
4339 *
4340 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4341 * that looks like a ClientHello.
4342 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004343 * - if the input looks like a ClientHello without cookies,
4344 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004345 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004346 * - if the input looks like a ClientHello with a valid cookie,
4347 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004348 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004349 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004350 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004351 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004352 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4353 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004354 */
4355static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4356{
4357 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004358 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004359
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004360 ret = ssl_check_dtls_clihlo_cookie(
4361 ssl->conf->f_cookie_write,
4362 ssl->conf->f_cookie_check,
4363 ssl->conf->p_cookie,
4364 ssl->cli_id, ssl->cli_id_len,
4365 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004366 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004367
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004368 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4369
4370 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004371 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004372 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373 * If the error is permanent we'll catch it later,
4374 * if it's not, then hopefully it'll work next time. */
4375 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4376
4377 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004378 }
4379
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004381 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004382 /* Got a valid cookie, partially reset context */
4383 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4384 {
4385 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4386 return( ret );
4387 }
4388
4389 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390 }
4391
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004392 return( ret );
4393}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004394#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004395
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004396/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004397 * ContentType type;
4398 * ProtocolVersion version;
4399 * uint16 epoch; // DTLS only
4400 * uint48 sequence_number; // DTLS only
4401 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004402 *
4403 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004404 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004405 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4406 *
4407 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004408 * 1. proceed with the record if this function returns 0
4409 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4410 * 3. return CLIENT_RECONNECT if this function return that value
4411 * 4. drop the whole datagram if this function returns anything else.
4412 * Point 2 is needed when the peer is resending, and we have already received
4413 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004416{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004417 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 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 +02004420
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004422 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004423 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004427 ssl->in_msgtype,
4428 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004430 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4432 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4433 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4434 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004437
4438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004439 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4440 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004441 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4442#endif /* MBEDTLS_SSL_PROTO_DTLS */
4443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4444 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004447 }
4448
4449 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004450 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4453 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 }
4455
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004456 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4459 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 }
4461
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004462 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004463 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004464 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004468 }
4469
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004470 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004471 * DTLS-related tests.
4472 * Check epoch before checking length constraint because
4473 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4474 * message gets duplicated before the corresponding Finished message,
4475 * the second ChangeCipherSpec should be discarded because it belongs
4476 * to an old epoch, but not because its length is shorter than
4477 * the minimum record length for packets using the new record transform.
4478 * Note that these two kinds of failures are handled differently,
4479 * as an unexpected record is silently skipped but an invalid
4480 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004481 */
4482#if defined(MBEDTLS_SSL_PROTO_DTLS)
4483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4484 {
4485 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4486
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004487 /* Check epoch (and sequence number) with DTLS */
4488 if( rec_epoch != ssl->in_epoch )
4489 {
4490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4491 "expected %d, received %d",
4492 ssl->in_epoch, rec_epoch ) );
4493
4494#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4495 /*
4496 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4497 * access the first byte of record content (handshake type), as we
4498 * have an active transform (possibly iv_len != 0), so use the
4499 * fact that the record header len is 13 instead.
4500 */
4501 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4502 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4503 rec_epoch == 0 &&
4504 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4505 ssl->in_left > 13 &&
4506 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4507 {
4508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4509 "from the same port" ) );
4510 return( ssl_handle_possible_reconnect( ssl ) );
4511 }
4512 else
4513#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004514 {
4515 /* Consider buffering the record. */
4516 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4517 {
4518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4519 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4520 }
4521
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004522 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004523 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004524 }
4525
4526#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4527 /* Replay detection only works for the current epoch */
4528 if( rec_epoch == ssl->in_epoch &&
4529 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4530 {
4531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4532 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4533 }
4534#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004535
Hanno Becker52c6dc62017-05-26 16:07:36 +01004536 /* Drop unexpected ApplicationData records,
4537 * except at the beginning of renegotiations */
4538 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4539 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4540#if defined(MBEDTLS_SSL_RENEGOTIATION)
4541 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4542 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4543#endif
4544 )
4545 {
4546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4547 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4548 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004549 }
4550#endif /* MBEDTLS_SSL_PROTO_DTLS */
4551
Hanno Becker52c6dc62017-05-26 16:07:36 +01004552
4553 /* Check length against bounds of the current transform and version */
4554 if( ssl->transform_in == NULL )
4555 {
4556 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004557 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004558 {
4559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4561 }
4562 }
4563 else
4564 {
4565 if( ssl->in_msglen < ssl->transform_in->minlen )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
4571#if defined(MBEDTLS_SSL_PROTO_SSL3)
4572 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004573 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004574 {
4575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4576 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4577 }
4578#endif
4579#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4580 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4581 /*
4582 * TLS encrypted messages can have up to 256 bytes of padding
4583 */
4584 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4585 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004586 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004587 {
4588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4589 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4590 }
4591#endif
4592 }
4593
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004594 return( 0 );
4595}
Paul Bakker5121ce52009-01-03 21:22:43 +00004596
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004597/*
4598 * If applicable, decrypt (and decompress) record content
4599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601{
4602 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4605 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4608 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 ret = mbedtls_ssl_hw_record_read( ssl );
4613 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4616 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004617 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004618
4619 if( ret == 0 )
4620 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004623 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004625 mbedtls_record rec;
4626
4627 rec.buf = ssl->in_iv;
4628 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4629 - ( ssl->in_iv - ssl->in_buf );
4630 rec.data_len = ssl->in_msglen;
4631 rec.data_offset = 0;
4632
4633 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4634 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4635 ssl->conf->transport, rec.ver );
4636 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004637 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4638 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 return( ret );
4642 }
4643
Hanno Becker29800d22018-08-07 14:30:18 +01004644 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4645 {
4646 /* Should never happen */
4647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004649
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004650 ssl->in_msglen = rec.data_len;
4651 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4652 ssl->in_len[1] = (unsigned char)( rec.data_len );
4653
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004654 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4655 ssl->in_msg, ssl->in_msglen );
4656
Angus Grattond8213d02016-05-25 20:56:48 +10004657 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4660 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004662 else if( ssl->in_msglen == 0 )
4663 {
4664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4665 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4666 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4667 {
4668 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4671 }
4672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4673
4674 ssl->nb_zero++;
4675
4676 /*
4677 * Three or more empty messages may be a DoS attack
4678 * (excessive CPU consumption).
4679 */
4680 if( ssl->nb_zero > 3 )
4681 {
4682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4683 "messages, possible DoS attack" ) );
4684 /* Q: Is that the right error code? */
4685 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4686 }
4687 }
4688 else
4689 ssl->nb_zero = 0;
4690
4691#if defined(MBEDTLS_SSL_PROTO_DTLS)
4692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4693 {
4694 ; /* in_ctr read from peer, not maintained internally */
4695 }
4696 else
4697#endif
4698 {
4699 unsigned i;
4700 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4701 if( ++ssl->in_ctr[i - 1] != 0 )
4702 break;
4703
4704 /* The loop goes to its end iff the counter is wrapping */
4705 if( i == ssl_ep_len( ssl ) )
4706 {
4707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4708 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4709 }
4710 }
4711
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 }
4713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004715 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004716 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004717 {
4718 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004721 return( ret );
4722 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004723 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004730 }
4731#endif
4732
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004733 return( 0 );
4734}
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004737
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004738/*
4739 * Read a record.
4740 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004741 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4742 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4743 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004744 */
Hanno Becker1097b342018-08-15 14:09:41 +01004745
4746/* Helper functions for mbedtls_ssl_read_record(). */
4747static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004748static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4749static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004750
Hanno Becker327c93b2018-08-15 13:56:18 +01004751int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004752 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004753{
4754 int ret;
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004757
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004758 if( ssl->keep_current_message == 0 )
4759 {
4760 do {
Simon Butcher99000142016-10-13 17:21:01 +01004761
Hanno Becker26994592018-08-15 14:14:59 +01004762 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004763 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004764 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004765
Hanno Beckere74d5562018-08-15 14:26:08 +01004766 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004767 {
Hanno Becker40f50842018-08-15 14:48:01 +01004768#if defined(MBEDTLS_SSL_PROTO_DTLS)
4769 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004770
Hanno Becker40f50842018-08-15 14:48:01 +01004771 /* We only check for buffered messages if the
4772 * current datagram is fully consumed. */
4773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004774 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004775 {
Hanno Becker40f50842018-08-15 14:48:01 +01004776 if( ssl_load_buffered_message( ssl ) == 0 )
4777 have_buffered = 1;
4778 }
4779
4780 if( have_buffered == 0 )
4781#endif /* MBEDTLS_SSL_PROTO_DTLS */
4782 {
4783 ret = ssl_get_next_record( ssl );
4784 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4785 continue;
4786
4787 if( ret != 0 )
4788 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004789 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004790 return( ret );
4791 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004792 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004793 }
4794
4795 ret = mbedtls_ssl_handle_message_type( ssl );
4796
Hanno Becker40f50842018-08-15 14:48:01 +01004797#if defined(MBEDTLS_SSL_PROTO_DTLS)
4798 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4799 {
4800 /* Buffer future message */
4801 ret = ssl_buffer_message( ssl );
4802 if( ret != 0 )
4803 return( ret );
4804
4805 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4806 }
4807#endif /* MBEDTLS_SSL_PROTO_DTLS */
4808
Hanno Becker90333da2017-10-10 11:27:13 +01004809 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4810 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004811
4812 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004813 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004814 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004815 return( ret );
4816 }
4817
Hanno Becker327c93b2018-08-15 13:56:18 +01004818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004819 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004820 {
4821 mbedtls_ssl_update_handshake_status( ssl );
4822 }
Simon Butcher99000142016-10-13 17:21:01 +01004823 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004824 else
Simon Butcher99000142016-10-13 17:21:01 +01004825 {
Hanno Becker02f59072018-08-15 14:00:24 +01004826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004827 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004828 }
4829
4830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4831
4832 return( 0 );
4833}
4834
Hanno Becker40f50842018-08-15 14:48:01 +01004835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004836static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004837{
Hanno Becker40f50842018-08-15 14:48:01 +01004838 if( ssl->in_left > ssl->next_record_offset )
4839 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004840
Hanno Becker40f50842018-08-15 14:48:01 +01004841 return( 0 );
4842}
4843
4844static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4845{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004847 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004848 int ret = 0;
4849
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004850 if( hs == NULL )
4851 return( -1 );
4852
Hanno Beckere00ae372018-08-20 09:39:42 +01004853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4854
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004855 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4856 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4857 {
4858 /* Check if we have seen a ChangeCipherSpec before.
4859 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004860 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004861 {
4862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4863 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004864 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004865 }
4866
Hanno Becker39b8bc92018-08-28 17:17:13 +01004867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004868 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4869 ssl->in_msglen = 1;
4870 ssl->in_msg[0] = 1;
4871
4872 /* As long as they are equal, the exact value doesn't matter. */
4873 ssl->in_left = 0;
4874 ssl->next_record_offset = 0;
4875
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004876 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004877 goto exit;
4878 }
Hanno Becker37f95322018-08-16 13:55:32 +01004879
Hanno Beckerb8f50142018-08-28 10:01:34 +01004880#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004881 /* Debug only */
4882 {
4883 unsigned offset;
4884 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4885 {
4886 hs_buf = &hs->buffering.hs[offset];
4887 if( hs_buf->is_valid == 1 )
4888 {
4889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4890 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004891 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004892 }
4893 }
4894 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004895#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004896
4897 /* Check if we have buffered and/or fully reassembled the
4898 * next handshake message. */
4899 hs_buf = &hs->buffering.hs[0];
4900 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4901 {
4902 /* Synthesize a record containing the buffered HS message. */
4903 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4904 ( hs_buf->data[2] << 8 ) |
4905 hs_buf->data[3];
4906
4907 /* Double-check that we haven't accidentally buffered
4908 * a message that doesn't fit into the input buffer. */
4909 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4910 {
4911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4913 }
4914
4915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4916 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4917 hs_buf->data, msg_len + 12 );
4918
4919 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4920 ssl->in_hslen = msg_len + 12;
4921 ssl->in_msglen = msg_len + 12;
4922 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4923
4924 ret = 0;
4925 goto exit;
4926 }
4927 else
4928 {
4929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4930 hs->in_msg_seq ) );
4931 }
4932
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004933 ret = -1;
4934
4935exit:
4936
4937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4938 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004939}
4940
Hanno Beckera02b0b42018-08-21 17:20:27 +01004941static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4942 size_t desired )
4943{
4944 int offset;
4945 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4947 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004948
Hanno Becker01315ea2018-08-21 17:22:17 +01004949 /* Get rid of future records epoch first, if such exist. */
4950 ssl_free_buffered_record( ssl );
4951
4952 /* Check if we have enough space available now. */
4953 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4954 hs->buffering.total_bytes_buffered ) )
4955 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004957 return( 0 );
4958 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004959
Hanno Becker4f432ad2018-08-28 10:02:32 +01004960 /* We don't have enough space to buffer the next expected handshake
4961 * message. Remove buffers used for future messages to gain space,
4962 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004963 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4964 offset >= 0; offset-- )
4965 {
4966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4967 offset ) );
4968
Hanno Beckerb309b922018-08-23 13:18:05 +01004969 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004970
4971 /* Check if we have enough space available now. */
4972 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4973 hs->buffering.total_bytes_buffered ) )
4974 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004976 return( 0 );
4977 }
4978 }
4979
4980 return( -1 );
4981}
4982
Hanno Becker40f50842018-08-15 14:48:01 +01004983static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4984{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004985 int ret = 0;
4986 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4987
4988 if( hs == NULL )
4989 return( 0 );
4990
4991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4992
4993 switch( ssl->in_msgtype )
4994 {
4995 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004997
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004998 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004999 break;
5000
5001 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005002 {
5003 unsigned recv_msg_seq_offset;
5004 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5005 mbedtls_ssl_hs_buffer *hs_buf;
5006 size_t msg_len = ssl->in_hslen - 12;
5007
5008 /* We should never receive an old handshake
5009 * message - double-check nonetheless. */
5010 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5011 {
5012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5014 }
5015
5016 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5017 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5018 {
5019 /* Silently ignore -- message too far in the future */
5020 MBEDTLS_SSL_DEBUG_MSG( 2,
5021 ( "Ignore future HS message with sequence number %u, "
5022 "buffering window %u - %u",
5023 recv_msg_seq, ssl->handshake->in_msg_seq,
5024 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5025
5026 goto exit;
5027 }
5028
5029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5030 recv_msg_seq, recv_msg_seq_offset ) );
5031
5032 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5033
5034 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005035 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005036 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005037 size_t reassembly_buf_sz;
5038
Hanno Becker37f95322018-08-16 13:55:32 +01005039 hs_buf->is_fragmented =
5040 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5041
5042 /* We copy the message back into the input buffer
5043 * after reassembly, so check that it's not too large.
5044 * This is an implementation-specific limitation
5045 * and not one from the standard, hence it is not
5046 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005047 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005048 {
5049 /* Ignore message */
5050 goto exit;
5051 }
5052
Hanno Beckere0b150f2018-08-21 15:51:03 +01005053 /* Check if we have enough space to buffer the message. */
5054 if( hs->buffering.total_bytes_buffered >
5055 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5056 {
5057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5059 }
5060
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005061 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5062 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005063
5064 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5065 hs->buffering.total_bytes_buffered ) )
5066 {
5067 if( recv_msg_seq_offset > 0 )
5068 {
5069 /* If we can't buffer a future message because
5070 * of space limitations -- ignore. */
5071 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",
5072 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5073 (unsigned) hs->buffering.total_bytes_buffered ) );
5074 goto exit;
5075 }
Hanno Beckere1801392018-08-21 16:51:05 +01005076 else
5077 {
5078 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",
5079 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5080 (unsigned) hs->buffering.total_bytes_buffered ) );
5081 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005082
Hanno Beckera02b0b42018-08-21 17:20:27 +01005083 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005084 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005085 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",
5086 (unsigned) msg_len,
5087 (unsigned) reassembly_buf_sz,
5088 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005089 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005090 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5091 goto exit;
5092 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005093 }
5094
5095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5096 msg_len ) );
5097
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005098 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5099 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005100 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005101 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005102 goto exit;
5103 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005104 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005105
5106 /* Prepare final header: copy msg_type, length and message_seq,
5107 * then add standardised fragment_offset and fragment_length */
5108 memcpy( hs_buf->data, ssl->in_msg, 6 );
5109 memset( hs_buf->data + 6, 0, 3 );
5110 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5111
5112 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005113
5114 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005115 }
5116 else
5117 {
5118 /* Make sure msg_type and length are consistent */
5119 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5120 {
5121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5122 /* Ignore */
5123 goto exit;
5124 }
5125 }
5126
Hanno Becker4422bbb2018-08-20 09:40:19 +01005127 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005128 {
5129 size_t frag_len, frag_off;
5130 unsigned char * const msg = hs_buf->data + 12;
5131
5132 /*
5133 * Check and copy current fragment
5134 */
5135
5136 /* Validation of header fields already done in
5137 * mbedtls_ssl_prepare_handshake_record(). */
5138 frag_off = ssl_get_hs_frag_off( ssl );
5139 frag_len = ssl_get_hs_frag_len( ssl );
5140
5141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5142 frag_off, frag_len ) );
5143 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5144
5145 if( hs_buf->is_fragmented )
5146 {
5147 unsigned char * const bitmask = msg + msg_len;
5148 ssl_bitmask_set( bitmask, frag_off, frag_len );
5149 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5150 msg_len ) == 0 );
5151 }
5152 else
5153 {
5154 hs_buf->is_complete = 1;
5155 }
5156
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5158 hs_buf->is_complete ? "" : "not yet " ) );
5159 }
5160
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005161 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005162 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005163
5164 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005165 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005166 break;
5167 }
5168
5169exit:
5170
5171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5172 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005173}
5174#endif /* MBEDTLS_SSL_PROTO_DTLS */
5175
Hanno Becker1097b342018-08-15 14:09:41 +01005176static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005177{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005178 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005179 * Consume last content-layer message and potentially
5180 * update in_msglen which keeps track of the contents'
5181 * consumption state.
5182 *
5183 * (1) Handshake messages:
5184 * Remove last handshake message, move content
5185 * and adapt in_msglen.
5186 *
5187 * (2) Alert messages:
5188 * Consume whole record content, in_msglen = 0.
5189 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005190 * (3) Change cipher spec:
5191 * Consume whole record content, in_msglen = 0.
5192 *
5193 * (4) Application data:
5194 * Don't do anything - the record layer provides
5195 * the application data as a stream transport
5196 * and consumes through mbedtls_ssl_read only.
5197 *
5198 */
5199
5200 /* Case (1): Handshake messages */
5201 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005202 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005203 /* Hard assertion to be sure that no application data
5204 * is in flight, as corrupting ssl->in_msglen during
5205 * ssl->in_offt != NULL is fatal. */
5206 if( ssl->in_offt != NULL )
5207 {
5208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5210 }
5211
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005212 /*
5213 * Get next Handshake message in the current record
5214 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005215
Hanno Becker4a810fb2017-05-24 16:27:30 +01005216 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005217 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005218 * current handshake content: If DTLS handshake
5219 * fragmentation is used, that's the fragment
5220 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005221 * size here is faulty and should be changed at
5222 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005223 * (2) While it doesn't seem to cause problems, one
5224 * has to be very careful not to assume that in_hslen
5225 * is always <= in_msglen in a sensible communication.
5226 * Again, it's wrong for DTLS handshake fragmentation.
5227 * The following check is therefore mandatory, and
5228 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005229 * Additionally, ssl->in_hslen might be arbitrarily out of
5230 * bounds after handling a DTLS message with an unexpected
5231 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005232 */
5233 if( ssl->in_hslen < ssl->in_msglen )
5234 {
5235 ssl->in_msglen -= ssl->in_hslen;
5236 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5237 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005238
Hanno Becker4a810fb2017-05-24 16:27:30 +01005239 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5240 ssl->in_msg, ssl->in_msglen );
5241 }
5242 else
5243 {
5244 ssl->in_msglen = 0;
5245 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005246
Hanno Becker4a810fb2017-05-24 16:27:30 +01005247 ssl->in_hslen = 0;
5248 }
5249 /* Case (4): Application data */
5250 else if( ssl->in_offt != NULL )
5251 {
5252 return( 0 );
5253 }
5254 /* Everything else (CCS & Alerts) */
5255 else
5256 {
5257 ssl->in_msglen = 0;
5258 }
5259
Hanno Becker1097b342018-08-15 14:09:41 +01005260 return( 0 );
5261}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005262
Hanno Beckere74d5562018-08-15 14:26:08 +01005263static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5264{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005265 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005266 return( 1 );
5267
5268 return( 0 );
5269}
5270
Hanno Becker5f066e72018-08-16 14:56:31 +01005271#if defined(MBEDTLS_SSL_PROTO_DTLS)
5272
5273static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5274{
5275 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5276 if( hs == NULL )
5277 return;
5278
Hanno Becker01315ea2018-08-21 17:22:17 +01005279 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005280 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005281 hs->buffering.total_bytes_buffered -=
5282 hs->buffering.future_record.len;
5283
5284 mbedtls_free( hs->buffering.future_record.data );
5285 hs->buffering.future_record.data = NULL;
5286 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005287}
5288
5289static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5290{
5291 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5292 unsigned char * rec;
5293 size_t rec_len;
5294 unsigned rec_epoch;
5295
5296 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5297 return( 0 );
5298
5299 if( hs == NULL )
5300 return( 0 );
5301
Hanno Becker5f066e72018-08-16 14:56:31 +01005302 rec = hs->buffering.future_record.data;
5303 rec_len = hs->buffering.future_record.len;
5304 rec_epoch = hs->buffering.future_record.epoch;
5305
5306 if( rec == NULL )
5307 return( 0 );
5308
Hanno Becker4cb782d2018-08-20 11:19:05 +01005309 /* Only consider loading future records if the
5310 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005311 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005312 return( 0 );
5313
Hanno Becker5f066e72018-08-16 14:56:31 +01005314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5315
5316 if( rec_epoch != ssl->in_epoch )
5317 {
5318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5319 goto exit;
5320 }
5321
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5323
5324 /* Double-check that the record is not too large */
5325 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5326 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5327 {
5328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5330 }
5331
5332 memcpy( ssl->in_hdr, rec, rec_len );
5333 ssl->in_left = rec_len;
5334 ssl->next_record_offset = 0;
5335
5336 ssl_free_buffered_record( ssl );
5337
5338exit:
5339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5340 return( 0 );
5341}
5342
5343static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5344{
5345 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5346 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005347 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005348
5349 /* Don't buffer future records outside handshakes. */
5350 if( hs == NULL )
5351 return( 0 );
5352
5353 /* Only buffer handshake records (we are only interested
5354 * in Finished messages). */
5355 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5356 return( 0 );
5357
5358 /* Don't buffer more than one future epoch record. */
5359 if( hs->buffering.future_record.data != NULL )
5360 return( 0 );
5361
Hanno Becker01315ea2018-08-21 17:22:17 +01005362 /* Don't buffer record if there's not enough buffering space remaining. */
5363 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5364 hs->buffering.total_bytes_buffered ) )
5365 {
5366 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",
5367 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5368 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005369 return( 0 );
5370 }
5371
Hanno Becker5f066e72018-08-16 14:56:31 +01005372 /* Buffer record */
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5374 ssl->in_epoch + 1 ) );
5375 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5376 rec_hdr_len + ssl->in_msglen );
5377
5378 /* ssl_parse_record_header() only considers records
5379 * of the next epoch as candidates for buffering. */
5380 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005381 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005382
5383 hs->buffering.future_record.data =
5384 mbedtls_calloc( 1, hs->buffering.future_record.len );
5385 if( hs->buffering.future_record.data == NULL )
5386 {
5387 /* If we run out of RAM trying to buffer a
5388 * record from the next epoch, just ignore. */
5389 return( 0 );
5390 }
5391
Hanno Becker01315ea2018-08-21 17:22:17 +01005392 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005393
Hanno Becker01315ea2018-08-21 17:22:17 +01005394 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005395 return( 0 );
5396}
5397
5398#endif /* MBEDTLS_SSL_PROTO_DTLS */
5399
Hanno Beckere74d5562018-08-15 14:26:08 +01005400static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005401{
5402 int ret;
5403
Hanno Becker5f066e72018-08-16 14:56:31 +01005404#if defined(MBEDTLS_SSL_PROTO_DTLS)
5405 /* We might have buffered a future record; if so,
5406 * and if the epoch matches now, load it.
5407 * On success, this call will set ssl->in_left to
5408 * the length of the buffered record, so that
5409 * the calls to ssl_fetch_input() below will
5410 * essentially be no-ops. */
5411 ret = ssl_load_buffered_record( ssl );
5412 if( ret != 0 )
5413 return( ret );
5414#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005419 return( ret );
5420 }
5421
5422 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5426 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005427 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005428 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5429 {
5430 ret = ssl_buffer_future_record( ssl );
5431 if( ret != 0 )
5432 return( ret );
5433
5434 /* Fall through to handling of unexpected records */
5435 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5436 }
5437
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005438 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5439 {
5440 /* Skip unexpected record (but not whole datagram) */
5441 ssl->next_record_offset = ssl->in_msglen
5442 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005443
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5445 "(header)" ) );
5446 }
5447 else
5448 {
5449 /* Skip invalid record and the rest of the datagram */
5450 ssl->next_record_offset = 0;
5451 ssl->in_left = 0;
5452
5453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5454 "(header)" ) );
5455 }
5456
5457 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005458 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005459 }
5460#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005461 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005462 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005463
5464 /*
5465 * Read and optionally decrypt the message contents
5466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5468 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005471 return( ret );
5472 }
5473
5474 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005476 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005479 if( ssl->next_record_offset < ssl->in_left )
5480 {
5481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5482 }
5483 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005484 else
5485#endif
5486 ssl->in_left = 0;
5487
5488 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005492 {
5493 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5495 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005496 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005497 /* Except when waiting for Finished as a bad mac here
5498 * probably means something went wrong in the handshake
5499 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5500 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5501 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5502 {
5503#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5504 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5505 {
5506 mbedtls_ssl_send_alert_message( ssl,
5507 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5508 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5509 }
5510#endif
5511 return( ret );
5512 }
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005515 if( ssl->conf->badmac_limit != 0 &&
5516 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005520 }
5521#endif
5522
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 /* As above, invalid records cause
5524 * dismissal of the whole datagram. */
5525
5526 ssl->next_record_offset = 0;
5527 ssl->in_left = 0;
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005530 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005531 }
5532
5533 return( ret );
5534 }
5535 else
5536#endif
5537 {
5538 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5540 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 mbedtls_ssl_send_alert_message( ssl,
5543 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5544 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005545 }
5546#endif
5547 return( ret );
5548 }
5549 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005550
Simon Butcher99000142016-10-13 17:21:01 +01005551 return( 0 );
5552}
5553
5554int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5555{
5556 int ret;
5557
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005558 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005559 * Handle particular types of records
5560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 {
Simon Butcher99000142016-10-13 17:21:01 +01005563 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5564 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005565 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 }
5568
Hanno Beckere678eaa2018-08-21 14:57:46 +01005569 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005570 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005571 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005572 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5574 ssl->in_msglen ) );
5575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005576 }
5577
Hanno Beckere678eaa2018-08-21 14:57:46 +01005578 if( ssl->in_msg[0] != 1 )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5581 ssl->in_msg[0] ) );
5582 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5583 }
5584
5585#if defined(MBEDTLS_SSL_PROTO_DTLS)
5586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5587 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5588 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5589 {
5590 if( ssl->handshake == NULL )
5591 {
5592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5593 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5594 }
5595
5596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5597 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5598 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005599#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005600 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005604 if( ssl->in_msglen != 2 )
5605 {
5606 /* Note: Standard allows for more than one 2 byte alert
5607 to be packed in a single message, but Mbed TLS doesn't
5608 currently support this. */
5609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5610 ssl->in_msglen ) );
5611 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5612 }
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005615 ssl->in_msg[0], ssl->in_msg[1] ) );
5616
5617 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005618 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005623 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005625 }
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5628 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5631 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005633
5634#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5635 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5636 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5637 {
Hanno Becker90333da2017-10-10 11:27:13 +01005638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005639 /* Will be handled when trying to parse ServerHello */
5640 return( 0 );
5641 }
5642#endif
5643
5644#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5645 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5646 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5647 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5648 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5649 {
5650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5651 /* Will be handled in mbedtls_ssl_parse_certificate() */
5652 return( 0 );
5653 }
5654#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5655
5656 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005657 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005658 }
5659
Hanno Beckerc76c6192017-06-06 10:03:17 +01005660#if defined(MBEDTLS_SSL_PROTO_DTLS)
5661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5662 ssl->handshake != NULL &&
5663 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5664 {
5665 ssl_handshake_wrapup_free_hs_transform( ssl );
5666 }
5667#endif
5668
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 return( 0 );
5670}
5671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005673{
5674 int ret;
5675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5677 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5678 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005679 {
5680 return( ret );
5681 }
5682
5683 return( 0 );
5684}
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005687 unsigned char level,
5688 unsigned char message )
5689{
5690 int ret;
5691
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005692 if( ssl == NULL || ssl->conf == NULL )
5693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005699 ssl->out_msglen = 2;
5700 ssl->out_msg[0] = level;
5701 ssl->out_msg[1] = message;
5702
Hanno Becker67bc7c32018-08-06 11:33:50 +01005703 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005706 return( ret );
5707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005709
5710 return( 0 );
5711}
5712
Hanno Beckerb9d44792019-02-08 07:19:04 +00005713#if defined(MBEDTLS_X509_CRT_PARSE_C)
5714static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5715{
5716#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5717 if( session->peer_cert != NULL )
5718 {
5719 mbedtls_x509_crt_free( session->peer_cert );
5720 mbedtls_free( session->peer_cert );
5721 session->peer_cert = NULL;
5722 }
5723#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5724 if( session->peer_cert_digest != NULL )
5725 {
5726 /* Zeroization is not necessary. */
5727 mbedtls_free( session->peer_cert_digest );
5728 session->peer_cert_digest = NULL;
5729 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5730 session->peer_cert_digest_len = 0;
5731 }
5732#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5733}
5734#endif /* MBEDTLS_X509_CRT_PARSE_C */
5735
Paul Bakker5121ce52009-01-03 21:22:43 +00005736/*
5737 * Handshake functions
5738 */
Hanno Becker21489932019-02-05 13:20:55 +00005739#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005740/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005743 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5744 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005747
Hanno Becker7177a882019-02-05 13:36:46 +00005748 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005751 ssl->state++;
5752 return( 0 );
5753 }
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5756 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005757}
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005760{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005761 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5762 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005765
Hanno Becker7177a882019-02-05 13:36:46 +00005766 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005769 ssl->state++;
5770 return( 0 );
5771 }
5772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005775}
Gilles Peskinef9828522017-05-03 12:28:43 +02005776
Hanno Becker21489932019-02-05 13:20:55 +00005777#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005778/* Some certificate support -> implement write and parse */
5779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005783 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005785 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5786 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005789
Hanno Becker7177a882019-02-05 13:36:46 +00005790 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005793 ssl->state++;
5794 return( 0 );
5795 }
5796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005798 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005799 {
5800 if( ssl->client_auth == 0 )
5801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 ssl->state++;
5804 return( 0 );
5805 }
5806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 /*
5809 * If using SSLv3 and got no cert, send an Alert message
5810 * (otherwise an empty Certificate message will be sent).
5811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5813 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 {
5815 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5817 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5818 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005821 goto write_msg;
5822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825#endif /* MBEDTLS_SSL_CLI_C */
5826#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5832 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005838
5839 /*
5840 * 0 . 0 handshake type
5841 * 1 . 3 handshake length
5842 * 4 . 6 length of all certs
5843 * 7 . 9 length of cert. 1
5844 * 10 . n-1 peer certificate
5845 * n . n+2 length of cert. 2
5846 * n+3 . ... upper level cert, etc.
5847 */
5848 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005850
Paul Bakker29087132010-03-21 21:03:34 +00005851 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005852 {
5853 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005854 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005857 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 }
5860
5861 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5862 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5863 ssl->out_msg[i + 2] = (unsigned char)( n );
5864
5865 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5866 i += n; crt = crt->next;
5867 }
5868
5869 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5870 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5871 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5872
5873 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5875 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005876
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005877#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005878write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005879#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005880
5881 ssl->state++;
5882
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005883 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 return( ret );
5887 }
5888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005890
Paul Bakkered27a042013-04-18 22:46:23 +02005891 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005892}
5893
Hanno Becker84879e32019-01-31 07:44:03 +00005894#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005895
5896#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005897static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5898 unsigned char *crt_buf,
5899 size_t crt_buf_len )
5900{
5901 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5902
5903 if( peer_crt == NULL )
5904 return( -1 );
5905
5906 if( peer_crt->raw.len != crt_buf_len )
5907 return( -1 );
5908
Hanno Becker46f34d02019-02-08 14:00:04 +00005909 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005910}
Hanno Becker177475a2019-02-05 17:02:46 +00005911#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5912static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5913 unsigned char *crt_buf,
5914 size_t crt_buf_len )
5915{
5916 int ret;
5917 unsigned char const * const peer_cert_digest =
5918 ssl->session->peer_cert_digest;
5919 mbedtls_md_type_t const peer_cert_digest_type =
5920 ssl->session->peer_cert_digest_type;
5921 mbedtls_md_info_t const * const digest_info =
5922 mbedtls_md_info_from_type( peer_cert_digest_type );
5923 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5924 size_t digest_len;
5925
5926 if( peer_cert_digest == NULL || digest_info == NULL )
5927 return( -1 );
5928
5929 digest_len = mbedtls_md_get_size( digest_info );
5930 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5931 return( -1 );
5932
5933 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5934 if( ret != 0 )
5935 return( -1 );
5936
5937 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5938}
5939#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005940#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005941
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005942/*
5943 * Once the certificate message is read, parse it into a cert chain and
5944 * perform basic checks, but leave actual verification to the caller
5945 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005946static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5947 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005948{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005949 int ret;
5950#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5951 int crt_cnt=0;
5952#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005953 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005954 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5965 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005968 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5969 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005971 }
5972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005974
Paul Bakker5121ce52009-01-03 21:22:43 +00005975 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005977 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005978 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005980 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005984 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5985 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005987 }
5988
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005989 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5990 i += 3;
5991
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005992 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005993 while( i < ssl->in_hslen )
5994 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005995 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005996 if ( i + 3 > ssl->in_hslen ) {
5997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005998 mbedtls_ssl_send_alert_message( ssl,
5999 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6000 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006001 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6002 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006003 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6004 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 if( ssl->in_msg[i] != 0 )
6006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006008 mbedtls_ssl_send_alert_message( ssl,
6009 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6010 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 }
6013
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006014 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6016 | (unsigned int) ssl->in_msg[i + 2];
6017 i += 3;
6018
6019 if( n < 128 || i + n > ssl->in_hslen )
6020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006022 mbedtls_ssl_send_alert_message( ssl,
6023 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6024 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006026 }
6027
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006028 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006029#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6030 if( crt_cnt++ == 0 &&
6031 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6032 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006033 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006034 /* During client-side renegotiation, check that the server's
6035 * end-CRTs hasn't changed compared to the initial handshake,
6036 * mitigating the triple handshake attack. On success, reuse
6037 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6039 if( ssl_check_peer_crt_unchanged( ssl,
6040 &ssl->in_msg[i],
6041 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006042 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6044 mbedtls_ssl_send_alert_message( ssl,
6045 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6046 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6047 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006048 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006049
6050 /* Now we can safely free the original chain. */
6051 ssl_clear_peer_cert( ssl->session );
6052 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006053#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6054
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006055 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006056#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006057 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006058#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006059 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006060 * it in-place from the input buffer instead of making a copy. */
6061 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6062#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006063 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006065 case 0: /*ok*/
6066 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6067 /* Ignore certificate with an unknown algorithm: maybe a
6068 prior certificate was already trusted. */
6069 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006070
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006071 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6072 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6073 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006074
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006075 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6076 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6077 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006078
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006079 default:
6080 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6081 crt_parse_der_failed:
6082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6083 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6084 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 }
6086
6087 i += n;
6088 }
6089
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006090 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006091 return( 0 );
6092}
6093
Hanno Becker4a55f632019-02-05 12:49:06 +00006094#if defined(MBEDTLS_SSL_SRV_C)
6095static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6096{
6097 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6098 return( -1 );
6099
6100#if defined(MBEDTLS_SSL_PROTO_SSL3)
6101 /*
6102 * Check if the client sent an empty certificate
6103 */
6104 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6105 {
6106 if( ssl->in_msglen == 2 &&
6107 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6108 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6109 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6110 {
6111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6112 return( 0 );
6113 }
6114
6115 return( -1 );
6116 }
6117#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6118
6119#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6120 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6121 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6122 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6123 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6124 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6125 {
6126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6127 return( 0 );
6128 }
6129
6130 return( -1 );
6131#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6132 MBEDTLS_SSL_PROTO_TLS1_2 */
6133}
6134#endif /* MBEDTLS_SSL_SRV_C */
6135
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006136/* Check if a certificate message is expected.
6137 * Return either
6138 * - SSL_CERTIFICATE_EXPECTED, or
6139 * - SSL_CERTIFICATE_SKIP
6140 * indicating whether a Certificate message is expected or not.
6141 */
6142#define SSL_CERTIFICATE_EXPECTED 0
6143#define SSL_CERTIFICATE_SKIP 1
6144static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6145 int authmode )
6146{
6147 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006148 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006149
6150 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6151 return( SSL_CERTIFICATE_SKIP );
6152
6153#if defined(MBEDTLS_SSL_SRV_C)
6154 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6155 {
6156 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6157 return( SSL_CERTIFICATE_SKIP );
6158
6159 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6160 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006161 ssl->session_negotiate->verify_result =
6162 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6163 return( SSL_CERTIFICATE_SKIP );
6164 }
6165 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006166#else
6167 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006168#endif /* MBEDTLS_SSL_SRV_C */
6169
6170 return( SSL_CERTIFICATE_EXPECTED );
6171}
6172
Hanno Becker68636192019-02-05 14:36:34 +00006173static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6174 int authmode,
6175 mbedtls_x509_crt *chain,
6176 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006177{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006178 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006179 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006180 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006181 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006182
Hanno Becker8927c832019-04-03 12:52:50 +01006183 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6184 void *p_vrfy;
6185
Hanno Becker68636192019-02-05 14:36:34 +00006186 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6187 return( 0 );
6188
Hanno Becker8927c832019-04-03 12:52:50 +01006189 if( ssl->f_vrfy != NULL )
6190 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006192 f_vrfy = ssl->f_vrfy;
6193 p_vrfy = ssl->p_vrfy;
6194 }
6195 else
6196 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006198 f_vrfy = ssl->conf->f_vrfy;
6199 p_vrfy = ssl->conf->p_vrfy;
6200 }
6201
Hanno Becker68636192019-02-05 14:36:34 +00006202 /*
6203 * Main check: verify certificate
6204 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006205#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6206 if( ssl->conf->f_ca_cb != NULL )
6207 {
6208 ((void) rs_ctx);
6209 have_ca_chain = 1;
6210
6211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006212 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006213 chain,
6214 ssl->conf->f_ca_cb,
6215 ssl->conf->p_ca_cb,
6216 ssl->conf->cert_profile,
6217 ssl->hostname,
6218 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006219 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006220 }
6221 else
6222#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6223 {
6224 mbedtls_x509_crt *ca_chain;
6225 mbedtls_x509_crl *ca_crl;
6226
6227#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6228 if( ssl->handshake->sni_ca_chain != NULL )
6229 {
6230 ca_chain = ssl->handshake->sni_ca_chain;
6231 ca_crl = ssl->handshake->sni_ca_crl;
6232 }
6233 else
6234#endif
6235 {
6236 ca_chain = ssl->conf->ca_chain;
6237 ca_crl = ssl->conf->ca_crl;
6238 }
6239
6240 if( ca_chain != NULL )
6241 have_ca_chain = 1;
6242
6243 ret = mbedtls_x509_crt_verify_restartable(
6244 chain,
6245 ca_chain, ca_crl,
6246 ssl->conf->cert_profile,
6247 ssl->hostname,
6248 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006249 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006250 }
Hanno Becker68636192019-02-05 14:36:34 +00006251
6252 if( ret != 0 )
6253 {
6254 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6255 }
6256
6257#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6258 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6259 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6260#endif
6261
6262 /*
6263 * Secondary checks: always done, but change 'ret' only if it was 0
6264 */
6265
6266#if defined(MBEDTLS_ECP_C)
6267 {
6268 const mbedtls_pk_context *pk = &chain->pk;
6269
6270 /* If certificate uses an EC key, make sure the curve is OK */
6271 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6272 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6273 {
6274 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6275
6276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6277 if( ret == 0 )
6278 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6279 }
6280 }
6281#endif /* MBEDTLS_ECP_C */
6282
6283 if( mbedtls_ssl_check_cert_usage( chain,
6284 ciphersuite_info,
6285 ! ssl->conf->endpoint,
6286 &ssl->session_negotiate->verify_result ) != 0 )
6287 {
6288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6289 if( ret == 0 )
6290 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6291 }
6292
6293 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6294 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6295 * with details encoded in the verification flags. All other kinds
6296 * of error codes, including those from the user provided f_vrfy
6297 * functions, are treated as fatal and lead to a failure of
6298 * ssl_parse_certificate even if verification was optional. */
6299 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6300 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6301 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6302 {
6303 ret = 0;
6304 }
6305
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006306 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006307 {
6308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6309 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6310 }
6311
6312 if( ret != 0 )
6313 {
6314 uint8_t alert;
6315
6316 /* The certificate may have been rejected for several reasons.
6317 Pick one and send the corresponding alert. Which alert to send
6318 may be a subject of debate in some cases. */
6319 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6320 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6321 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6322 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6323 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6324 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6325 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6326 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6327 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6328 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6329 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6330 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6331 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6332 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6333 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6334 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6335 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6336 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6337 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6338 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6339 else
6340 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6342 alert );
6343 }
6344
6345#if defined(MBEDTLS_DEBUG_C)
6346 if( ssl->session_negotiate->verify_result != 0 )
6347 {
6348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6349 ssl->session_negotiate->verify_result ) );
6350 }
6351 else
6352 {
6353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6354 }
6355#endif /* MBEDTLS_DEBUG_C */
6356
6357 return( ret );
6358}
6359
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006360#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6361static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6362 unsigned char *start, size_t len )
6363{
6364 int ret;
6365 /* Remember digest of the peer's end-CRT. */
6366 ssl->session_negotiate->peer_cert_digest =
6367 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6368 if( ssl->session_negotiate->peer_cert_digest == NULL )
6369 {
6370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6371 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6372 mbedtls_ssl_send_alert_message( ssl,
6373 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6374 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6375
6376 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6377 }
6378
6379 ret = mbedtls_md( mbedtls_md_info_from_type(
6380 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6381 start, len,
6382 ssl->session_negotiate->peer_cert_digest );
6383
6384 ssl->session_negotiate->peer_cert_digest_type =
6385 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6386 ssl->session_negotiate->peer_cert_digest_len =
6387 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6388
6389 return( ret );
6390}
6391
6392static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6393 unsigned char *start, size_t len )
6394{
6395 unsigned char *end = start + len;
6396 int ret;
6397
6398 /* Make a copy of the peer's raw public key. */
6399 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6400 ret = mbedtls_pk_parse_subpubkey( &start, end,
6401 &ssl->handshake->peer_pubkey );
6402 if( ret != 0 )
6403 {
6404 /* We should have parsed the public key before. */
6405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6406 }
6407
6408 return( 0 );
6409}
6410#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6411
Hanno Becker68636192019-02-05 14:36:34 +00006412int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6413{
6414 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006415 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006416#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6417 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6418 ? ssl->handshake->sni_authmode
6419 : ssl->conf->authmode;
6420#else
6421 const int authmode = ssl->conf->authmode;
6422#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006423 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006424 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006425
6426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6427
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006428 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6429 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006430 {
6431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006432 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006433 }
6434
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006435#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6436 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006437 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006438 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006439 chain = ssl->handshake->ecrs_peer_cert;
6440 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006441 goto crt_verify;
6442 }
6443#endif
6444
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006445 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006446 {
6447 /* mbedtls_ssl_read_record may have sent an alert already. We
6448 let it decide whether to alert. */
6449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006450 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006451 }
6452
Hanno Becker4a55f632019-02-05 12:49:06 +00006453#if defined(MBEDTLS_SSL_SRV_C)
6454 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6455 {
6456 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006457
6458 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006459 ret = 0;
6460 else
6461 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006462
Hanno Becker6bdfab22019-02-05 13:11:17 +00006463 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006464 }
6465#endif /* MBEDTLS_SSL_SRV_C */
6466
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006467 /* Clear existing peer CRT structure in case we tried to
6468 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006469 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006470
6471 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6472 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006473 {
6474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6475 sizeof( mbedtls_x509_crt ) ) );
6476 mbedtls_ssl_send_alert_message( ssl,
6477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6478 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006479
Hanno Becker3dad3112019-02-05 17:19:52 +00006480 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6481 goto exit;
6482 }
6483 mbedtls_x509_crt_init( chain );
6484
6485 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006486 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006487 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006488
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006489#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6490 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006491 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006492
6493crt_verify:
6494 if( ssl->handshake->ecrs_enabled)
6495 rs_ctx = &ssl->handshake->ecrs_ctx;
6496#endif
6497
Hanno Becker68636192019-02-05 14:36:34 +00006498 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006499 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006500 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006501 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006502
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006503#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006504 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006505 unsigned char *crt_start, *pk_start;
6506 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006507
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006508 /* We parse the CRT chain without copying, so
6509 * these pointers point into the input buffer,
6510 * and are hence still valid after freeing the
6511 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006512
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006513 crt_start = chain->raw.p;
6514 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006515
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006516 pk_start = chain->pk_raw.p;
6517 pk_len = chain->pk_raw.len;
6518
6519 /* Free the CRT structures before computing
6520 * digest and copying the peer's public key. */
6521 mbedtls_x509_crt_free( chain );
6522 mbedtls_free( chain );
6523 chain = NULL;
6524
6525 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006526 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006527 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006528
6529 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6530 if( ret != 0 )
6531 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006532 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006533#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6534 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006535 ssl->session_negotiate->peer_cert = chain;
6536 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006537#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
Hanno Becker6bdfab22019-02-05 13:11:17 +00006541exit:
6542
Hanno Becker3dad3112019-02-05 17:19:52 +00006543 if( ret == 0 )
6544 ssl->state++;
6545
6546#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6547 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6548 {
6549 ssl->handshake->ecrs_peer_cert = chain;
6550 chain = NULL;
6551 }
6552#endif
6553
6554 if( chain != NULL )
6555 {
6556 mbedtls_x509_crt_free( chain );
6557 mbedtls_free( chain );
6558 }
6559
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 return( ret );
6561}
Hanno Becker21489932019-02-05 13:20:55 +00006562#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565{
6566 int ret;
6567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 ssl->out_msglen = 1;
6572 ssl->out_msg[0] = 1;
6573
Paul Bakker5121ce52009-01-03 21:22:43 +00006574 ssl->state++;
6575
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006576 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 return( ret );
6580 }
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006583
6584 return( 0 );
6585}
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588{
6589 int ret;
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592
Hanno Becker327c93b2018-08-15 13:56:18 +01006593 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 return( ret );
6597 }
6598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6603 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 }
6606
Hanno Beckere678eaa2018-08-21 14:57:46 +01006607 /* CCS records are only accepted if they have length 1 and content '1',
6608 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006609
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006610 /*
6611 * Switch to our negotiated transform and session parameters for inbound
6612 * data.
6613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006615 ssl->transform_in = ssl->transform_negotiate;
6616 ssl->session_in = ssl->session_negotiate;
6617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006622 ssl_dtls_replay_reset( ssl );
6623#endif
6624
6625 /* Increment epoch */
6626 if( ++ssl->in_epoch == 0 )
6627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006629 /* This is highly unlikely to happen for legitimate reasons, so
6630 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006632 }
6633 }
6634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006636 memset( ssl->in_ctr, 0, 8 );
6637
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006638 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6641 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006646 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006649 }
6650 }
6651#endif
6652
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 ssl->state++;
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
6657 return( 0 );
6658}
6659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6661 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006662{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006663 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6666 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6667 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006668 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006669 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6672#if defined(MBEDTLS_SHA512_C)
6673 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006674 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6675 else
6676#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#if defined(MBEDTLS_SHA256_C)
6678 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006679 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006680 else
6681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006685 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006686 }
Paul Bakker380da532012-04-18 16:10:25 +00006687}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6692 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006693 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6694 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006695#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6697#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006698#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006699 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006700 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6701#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006702 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006703#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006706#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006707 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006708 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006709#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006710 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006711#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006714}
6715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006717 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6720 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006721 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6722 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6725#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006726#if defined(MBEDTLS_USE_PSA_CRYPTO)
6727 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6728#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006729 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006730#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006731#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006733#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006734 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006735#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006736 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006737#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006740}
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6743 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6744static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006745 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006746{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006747 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6748 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006749}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006750#endif
Paul Bakker380da532012-04-18 16:10:25 +00006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6753#if defined(MBEDTLS_SHA256_C)
6754static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006756{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006757#if defined(MBEDTLS_USE_PSA_CRYPTO)
6758 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6759#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006760 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006761#endif
Paul Bakker380da532012-04-18 16:10:25 +00006762}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006763#endif
Paul Bakker380da532012-04-18 16:10:25 +00006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765#if defined(MBEDTLS_SHA512_C)
6766static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006767 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006768{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006769#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006770 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006771#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006772 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006773#endif
Paul Bakker380da532012-04-18 16:10:25 +00006774}
Paul Bakker769075d2012-11-24 11:26:46 +01006775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006779static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006782 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006783 mbedtls_md5_context md5;
6784 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006785
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 unsigned char padbuf[48];
6787 unsigned char md5sum[16];
6788 unsigned char sha1sum[20];
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006791 if( !session )
6792 session = ssl->session;
6793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006796 mbedtls_md5_init( &md5 );
6797 mbedtls_sha1_init( &sha1 );
6798
6799 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6800 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
6802 /*
6803 * SSLv3:
6804 * hash =
6805 * MD5( master + pad2 +
6806 * MD5( handshake + sender + master + pad1 ) )
6807 * + SHA1( master + pad2 +
6808 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 */
6810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006812 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6813 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006817 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6818 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006822 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Paul Bakker1ef83d62012-04-11 12:09:53 +00006824 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006825
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006826 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6827 mbedtls_md5_update_ret( &md5, session->master, 48 );
6828 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6829 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006831 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6832 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6833 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6834 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
Paul Bakker1ef83d62012-04-11 12:09:53 +00006836 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006838 mbedtls_md5_starts_ret( &md5 );
6839 mbedtls_md5_update_ret( &md5, session->master, 48 );
6840 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6841 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6842 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006844 mbedtls_sha1_starts_ret( &sha1 );
6845 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6846 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6847 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6848 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006852 mbedtls_md5_free( &md5 );
6853 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006855 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6856 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6857 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006864static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006866{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006867 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006868 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006869 mbedtls_md5_context md5;
6870 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006871 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006874 if( !session )
6875 session = ssl->session;
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006878
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006879 mbedtls_md5_init( &md5 );
6880 mbedtls_sha1_init( &sha1 );
6881
6882 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6883 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006884
Paul Bakker1ef83d62012-04-11 12:09:53 +00006885 /*
6886 * TLSv1:
6887 * hash = PRF( master, finished_label,
6888 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6889 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006892 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6893 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006894#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006897 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6898 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006899#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006902 ? "client finished"
6903 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006905 mbedtls_md5_finish_ret( &md5, padbuf );
6906 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006907
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006908 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006909 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006912
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 mbedtls_md5_free( &md5 );
6914 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006915
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006916 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6923#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006924static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926{
6927 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006928 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006929 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006930#if defined(MBEDTLS_USE_PSA_CRYPTO)
6931 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006932 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006933 psa_status_t status;
6934#else
6935 mbedtls_sha256_context sha256;
6936#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006939 if( !session )
6940 session = ssl->session;
6941
Andrzej Kurekeb342242019-01-29 09:14:33 -05006942 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6943 ? "client finished"
6944 : "server finished";
6945
6946#if defined(MBEDTLS_USE_PSA_CRYPTO)
6947 sha256_psa = psa_hash_operation_init();
6948
6949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6950
6951 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6952 if( status != PSA_SUCCESS )
6953 {
6954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6955 return;
6956 }
6957
6958 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6959 if( status != PSA_SUCCESS )
6960 {
6961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6962 return;
6963 }
6964 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6965#else
6966
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006967 mbedtls_sha256_init( &sha256 );
6968
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006970
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006971 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006972
6973 /*
6974 * TLSv1.2:
6975 * hash = PRF( master, finished_label,
6976 * Hash( handshake ) )[0.11]
6977 */
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if !defined(MBEDTLS_SHA256_ALT)
6980 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006981 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006982#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006983
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006984 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006985 mbedtls_sha256_free( &sha256 );
6986#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006987
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006988 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006989 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006992
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006993 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007000static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007002{
7003 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007004 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007005 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007006#if defined(MBEDTLS_USE_PSA_CRYPTO)
7007 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007008 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007009 psa_status_t status;
7010#else
7011 mbedtls_sha512_context sha512;
7012#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007015 if( !session )
7016 session = ssl->session;
7017
Andrzej Kurekeb342242019-01-29 09:14:33 -05007018 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7019 ? "client finished"
7020 : "server finished";
7021
7022#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007023 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007024
7025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7026
Andrzej Kurek972fba52019-01-30 03:29:12 -05007027 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007028 if( status != PSA_SUCCESS )
7029 {
7030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7031 return;
7032 }
7033
Andrzej Kurek972fba52019-01-30 03:29:12 -05007034 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007035 if( status != PSA_SUCCESS )
7036 {
7037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7038 return;
7039 }
7040 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7041#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007042 mbedtls_sha512_init( &sha512 );
7043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007045
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007046 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007047
7048 /*
7049 * TLSv1.2:
7050 * hash = PRF( master, finished_label,
7051 * Hash( handshake ) )[0.11]
7052 */
7053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007055 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7056 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007057#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007058
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007059 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007060 mbedtls_sha512_free( &sha512 );
7061#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007062
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007063 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007064 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007067
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007068 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#endif /* MBEDTLS_SHA512_C */
7073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007078
7079 /*
7080 * Free our handshake params
7081 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007082 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007084 ssl->handshake = NULL;
7085
7086 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007087 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007088 */
7089 if( ssl->transform )
7090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 mbedtls_ssl_transform_free( ssl->transform );
7092 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007093 }
7094 ssl->transform = ssl->transform_negotiate;
7095 ssl->transform_negotiate = NULL;
7096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007098}
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007101{
7102 int resume = ssl->handshake->resume;
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#if defined(MBEDTLS_SSL_RENEGOTIATION)
7107 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007110 ssl->renego_records_seen = 0;
7111 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007112#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007113
7114 /*
7115 * Free the previous session and switch in the current one
7116 */
Paul Bakker0a597072012-09-25 21:55:46 +00007117 if( ssl->session )
7118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007120 /* RFC 7366 3.1: keep the EtM state */
7121 ssl->session_negotiate->encrypt_then_mac =
7122 ssl->session->encrypt_then_mac;
7123#endif
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_session_free( ssl->session );
7126 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007127 }
7128 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007129 ssl->session_negotiate = NULL;
7130
Paul Bakker0a597072012-09-25 21:55:46 +00007131 /*
7132 * Add cache entry
7133 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007134 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007135 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007136 resume == 0 )
7137 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007138 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007140 }
Paul Bakker0a597072012-09-25 21:55:46 +00007141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007144 ssl->handshake->flight != NULL )
7145 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007146 /* Cancel handshake timer */
7147 ssl_set_timer( ssl, 0 );
7148
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007149 /* Keep last flight around in case we need to resend it:
7150 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007152 }
7153 else
7154#endif
7155 ssl_handshake_wrapup_free_hs_transform( ssl );
7156
Paul Bakker48916f92012-09-16 19:57:18 +00007157 ssl->state++;
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007160}
7161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007163{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007164 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007167
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007168 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007169
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007170 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007171
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007172 /*
7173 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7174 * may define some other value. Currently (early 2016), no defined
7175 * ciphersuite does this (and this is unlikely to change as activity has
7176 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007181 ssl->verify_data_len = hash_len;
7182 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007183#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007184
Paul Bakker5121ce52009-01-03 21:22:43 +00007185 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7187 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007188
7189 /*
7190 * In case of session resuming, invert the client and server
7191 * ChangeCipherSpec messages order.
7192 */
Paul Bakker0a597072012-09-25 21:55:46 +00007193 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007196 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007200 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 }
7204 else
7205 ssl->state++;
7206
Paul Bakker48916f92012-09-16 19:57:18 +00007207 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007208 * Switch to our negotiated transform and session parameters for outbound
7209 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007215 {
7216 unsigned char i;
7217
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007218 /* Remember current epoch settings for resending */
7219 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007220 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007221
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007222 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007223 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007224
7225 /* Increment epoch */
7226 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007227 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007228 break;
7229
7230 /* The loop goes to its end iff the counter is wrapping */
7231 if( i == 0 )
7232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7234 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007235 }
7236 }
7237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007239 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007240
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007241 ssl->transform_out = ssl->transform_negotiate;
7242 ssl->session_out = ssl->session_negotiate;
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7245 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7250 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007251 }
7252 }
7253#endif
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007258#endif
7259
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007260 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007261 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007263 return( ret );
7264 }
7265
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007266#if defined(MBEDTLS_SSL_PROTO_DTLS)
7267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7268 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7269 {
7270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7271 return( ret );
7272 }
7273#endif
7274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007276
7277 return( 0 );
7278}
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007281#define SSL_MAX_HASH_LEN 36
7282#else
7283#define SSL_MAX_HASH_LEN 12
7284#endif
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287{
Paul Bakker23986e52011-04-24 08:57:21 +00007288 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007289 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007290 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007293
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007294 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007295
Hanno Becker327c93b2018-08-15 13:56:18 +01007296 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007299 return( ret );
7300 }
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7306 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007308 }
7309
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007310 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_PROTO_SSL3)
7312 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007313 hash_len = 36;
7314 else
7315#endif
7316 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7319 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007322 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007325 }
7326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007328 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007331 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7332 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 }
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007337 ssl->verify_data_len = hash_len;
7338 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007339#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007340
Paul Bakker0a597072012-09-25 21:55:46 +00007341 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007348 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007350#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007351 }
7352 else
7353 ssl->state++;
7354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007358#endif
7359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
7362 return( 0 );
7363}
7364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7370 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7371 mbedtls_md5_init( &handshake->fin_md5 );
7372 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007373 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7374 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7377#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007378#if defined(MBEDTLS_USE_PSA_CRYPTO)
7379 handshake->fin_sha256_psa = psa_hash_operation_init();
7380 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7381#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007383 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007384#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007387#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007388 handshake->fin_sha384_psa = psa_hash_operation_init();
7389 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007390#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007392 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007393#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007396
7397 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007398
7399#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7400 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7401 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7402#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_DHM_C)
7405 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_ECDH_C)
7408 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007409#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007410#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007411 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007412#if defined(MBEDTLS_SSL_CLI_C)
7413 handshake->ecjpake_cache = NULL;
7414 handshake->ecjpake_cache_len = 0;
7415#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007416#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007417
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007418#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007419 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007420#endif
7421
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007422#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7423 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7424#endif
Hanno Becker75173122019-02-06 16:18:31 +00007425
7426#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7427 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7428 mbedtls_pk_init( &handshake->peer_pubkey );
7429#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007430}
7431
Hanno Beckera18d1322018-01-03 14:27:32 +00007432void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7437 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007438
Hanno Beckerd56ed242018-01-03 15:32:51 +00007439#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 mbedtls_md_init( &transform->md_ctx_enc );
7441 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007442#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007443}
7444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007446{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007448}
7449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007451{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007452 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007453 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007455 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007457 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007458 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007459
7460 /*
7461 * Either the pointers are now NULL or cleared properly and can be freed.
7462 * Now allocate missing structures.
7463 */
7464 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007465 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007466 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007467 }
Paul Bakker48916f92012-09-16 19:57:18 +00007468
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007469 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007470 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007471 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007472 }
Paul Bakker48916f92012-09-16 19:57:18 +00007473
Paul Bakker82788fb2014-10-20 13:59:19 +02007474 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007475 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007476 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007477 }
Paul Bakker48916f92012-09-16 19:57:18 +00007478
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007479 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007480 if( ssl->handshake == NULL ||
7481 ssl->transform_negotiate == NULL ||
7482 ssl->session_negotiate == NULL )
7483 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 mbedtls_free( ssl->handshake );
7487 mbedtls_free( ssl->transform_negotiate );
7488 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007489
7490 ssl->handshake = NULL;
7491 ssl->transform_negotiate = NULL;
7492 ssl->session_negotiate = NULL;
7493
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007494 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007495 }
7496
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007497 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007499 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007500 ssl_handshake_params_init( ssl->handshake );
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7504 {
7505 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007506
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007507 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7508 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7509 else
7510 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007511
7512 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007513 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007514#endif
7515
Paul Bakker48916f92012-09-16 19:57:18 +00007516 return( 0 );
7517}
7518
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007519#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007520/* Dummy cookie callbacks for defaults */
7521static int ssl_cookie_write_dummy( void *ctx,
7522 unsigned char **p, unsigned char *end,
7523 const unsigned char *cli_id, size_t cli_id_len )
7524{
7525 ((void) ctx);
7526 ((void) p);
7527 ((void) end);
7528 ((void) cli_id);
7529 ((void) cli_id_len);
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007532}
7533
7534static int ssl_cookie_check_dummy( void *ctx,
7535 const unsigned char *cookie, size_t cookie_len,
7536 const unsigned char *cli_id, size_t cli_id_len )
7537{
7538 ((void) ctx);
7539 ((void) cookie);
7540 ((void) cookie_len);
7541 ((void) cli_id);
7542 ((void) cli_id_len);
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007545}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007546#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007547
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007548/* Once ssl->out_hdr as the address of the beginning of the
7549 * next outgoing record is set, deduce the other pointers.
7550 *
7551 * Note: For TLS, we save the implicit record sequence number
7552 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7553 * and the caller has to make sure there's space for this.
7554 */
7555
7556static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7557 mbedtls_ssl_transform *transform )
7558{
7559#if defined(MBEDTLS_SSL_PROTO_DTLS)
7560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7561 {
7562 ssl->out_ctr = ssl->out_hdr + 3;
7563 ssl->out_len = ssl->out_hdr + 11;
7564 ssl->out_iv = ssl->out_hdr + 13;
7565 }
7566 else
7567#endif
7568 {
7569 ssl->out_ctr = ssl->out_hdr - 8;
7570 ssl->out_len = ssl->out_hdr + 3;
7571 ssl->out_iv = ssl->out_hdr + 5;
7572 }
7573
7574 /* Adjust out_msg to make space for explicit IV, if used. */
7575 if( transform != NULL &&
7576 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7577 {
7578 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7579 }
7580 else
7581 ssl->out_msg = ssl->out_iv;
7582}
7583
7584/* Once ssl->in_hdr as the address of the beginning of the
7585 * next incoming record is set, deduce the other pointers.
7586 *
7587 * Note: For TLS, we save the implicit record sequence number
7588 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7589 * and the caller has to make sure there's space for this.
7590 */
7591
7592static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7593 mbedtls_ssl_transform *transform )
7594{
7595#if defined(MBEDTLS_SSL_PROTO_DTLS)
7596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7597 {
7598 ssl->in_ctr = ssl->in_hdr + 3;
7599 ssl->in_len = ssl->in_hdr + 11;
7600 ssl->in_iv = ssl->in_hdr + 13;
7601 }
7602 else
7603#endif
7604 {
7605 ssl->in_ctr = ssl->in_hdr - 8;
7606 ssl->in_len = ssl->in_hdr + 3;
7607 ssl->in_iv = ssl->in_hdr + 5;
7608 }
7609
7610 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7611 if( transform != NULL &&
7612 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7613 {
7614 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7615 }
7616 else
7617 ssl->in_msg = ssl->in_iv;
7618}
7619
Paul Bakker5121ce52009-01-03 21:22:43 +00007620/*
7621 * Initialize an SSL context
7622 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007623void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7624{
7625 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7626}
7627
7628/*
7629 * Setup an SSL context
7630 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007631
7632static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7633{
7634 /* Set the incoming and outgoing record pointers. */
7635#if defined(MBEDTLS_SSL_PROTO_DTLS)
7636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7637 {
7638 ssl->out_hdr = ssl->out_buf;
7639 ssl->in_hdr = ssl->in_buf;
7640 }
7641 else
7642#endif /* MBEDTLS_SSL_PROTO_DTLS */
7643 {
7644 ssl->out_hdr = ssl->out_buf + 8;
7645 ssl->in_hdr = ssl->in_buf + 8;
7646 }
7647
7648 /* Derive other internal pointers. */
7649 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7650 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7651}
7652
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007653int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007654 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007655{
Paul Bakker48916f92012-09-16 19:57:18 +00007656 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007658 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007659
7660 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007661 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007662 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007663
7664 /* Set to NULL in case of an error condition */
7665 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007666
Angus Grattond8213d02016-05-25 20:56:48 +10007667 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7668 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007669 {
Angus Grattond8213d02016-05-25 20:56:48 +10007670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007671 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007672 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007673 }
7674
7675 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7676 if( ssl->out_buf == NULL )
7677 {
7678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007679 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007680 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007681 }
7682
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007683 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007684
Paul Bakker48916f92012-09-16 19:57:18 +00007685 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007686 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007687
7688 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007689
7690error:
7691 mbedtls_free( ssl->in_buf );
7692 mbedtls_free( ssl->out_buf );
7693
7694 ssl->conf = NULL;
7695
7696 ssl->in_buf = NULL;
7697 ssl->out_buf = NULL;
7698
7699 ssl->in_hdr = NULL;
7700 ssl->in_ctr = NULL;
7701 ssl->in_len = NULL;
7702 ssl->in_iv = NULL;
7703 ssl->in_msg = NULL;
7704
7705 ssl->out_hdr = NULL;
7706 ssl->out_ctr = NULL;
7707 ssl->out_len = NULL;
7708 ssl->out_iv = NULL;
7709 ssl->out_msg = NULL;
7710
k-stachowiak9f7798e2018-07-31 16:52:32 +02007711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007712}
7713
7714/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007715 * Reset an initialized and used SSL context for re-use while retaining
7716 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007717 *
7718 * If partial is non-zero, keep data in the input buffer and client ID.
7719 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007720 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007721static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007722{
Paul Bakker48916f92012-09-16 19:57:18 +00007723 int ret;
7724
Hanno Becker7e772132018-08-10 12:38:21 +01007725#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7726 !defined(MBEDTLS_SSL_SRV_C)
7727 ((void) partial);
7728#endif
7729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007731
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007732 /* Cancel any possibly running timer */
7733 ssl_set_timer( ssl, 0 );
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_RENEGOTIATION)
7736 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007737 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007738
7739 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7741 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007744
Paul Bakker7eb013f2011-10-06 12:37:39 +00007745 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007746 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007747
7748 ssl->in_msgtype = 0;
7749 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007751 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007752 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007755 ssl_dtls_replay_reset( ssl );
7756#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007757
7758 ssl->in_hslen = 0;
7759 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007760
7761 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007762
7763 ssl->out_msgtype = 0;
7764 ssl->out_msglen = 0;
7765 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7767 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007768 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007769#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007770
Hanno Becker19859472018-08-06 09:40:20 +01007771 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7772
Paul Bakker48916f92012-09-16 19:57:18 +00007773 ssl->transform_in = NULL;
7774 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007775
Hanno Becker78640902018-08-13 16:35:15 +01007776 ssl->session_in = NULL;
7777 ssl->session_out = NULL;
7778
Angus Grattond8213d02016-05-25 20:56:48 +10007779 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007780
7781#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007782 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007783#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7784 {
7785 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007786 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007787 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7790 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7793 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007797 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007798 }
7799#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007800
Paul Bakker48916f92012-09-16 19:57:18 +00007801 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 mbedtls_ssl_transform_free( ssl->transform );
7804 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007805 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007806 }
Paul Bakker48916f92012-09-16 19:57:18 +00007807
Paul Bakkerc0463502013-02-14 11:19:38 +01007808 if( ssl->session )
7809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 mbedtls_ssl_session_free( ssl->session );
7811 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007812 ssl->session = NULL;
7813 }
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007816 ssl->alpn_chosen = NULL;
7817#endif
7818
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007819#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007820#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007821 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007822#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007823 {
7824 mbedtls_free( ssl->cli_id );
7825 ssl->cli_id = NULL;
7826 ssl->cli_id_len = 0;
7827 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007828#endif
7829
Paul Bakker48916f92012-09-16 19:57:18 +00007830 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7831 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007832
7833 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007834}
7835
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007836/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007837 * Reset an initialized and used SSL context for re-use while retaining
7838 * all application-set variables, function pointers and data.
7839 */
7840int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7841{
7842 return( ssl_session_reset_int( ssl, 0 ) );
7843}
7844
7845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 * SSL set accessors
7847 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007848void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007850 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007851}
7852
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007853void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007855 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007856}
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007859void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007861 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007862}
7863#endif
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007866void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007868 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007869}
7870#endif
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007873
Hanno Becker1841b0a2018-08-24 11:13:57 +01007874void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7875 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007876{
7877 ssl->disable_datagram_packing = !allow_packing;
7878}
7879
7880void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7881 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007882{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007883 conf->hs_timeout_min = min;
7884 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007885}
7886#endif
7887
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007888void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007891}
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007894void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007895 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007896 void *p_vrfy )
7897{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007898 conf->f_vrfy = f_vrfy;
7899 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007902
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007903void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007904 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007905 void *p_rng )
7906{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007907 conf->f_rng = f_rng;
7908 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007909}
7910
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007911void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007912 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 void *p_dbg )
7914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007915 conf->f_dbg = f_dbg;
7916 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007917}
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007920 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007921 mbedtls_ssl_send_t *f_send,
7922 mbedtls_ssl_recv_t *f_recv,
7923 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007924{
7925 ssl->p_bio = p_bio;
7926 ssl->f_send = f_send;
7927 ssl->f_recv = f_recv;
7928 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007929}
7930
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007931#if defined(MBEDTLS_SSL_PROTO_DTLS)
7932void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7933{
7934 ssl->mtu = mtu;
7935}
7936#endif
7937
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007939{
7940 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007941}
7942
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007943void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7944 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007945 mbedtls_ssl_set_timer_t *f_set_timer,
7946 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007947{
7948 ssl->p_timer = p_timer;
7949 ssl->f_set_timer = f_set_timer;
7950 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007951
7952 /* Make sure we start with no timer running */
7953 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007957void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007958 void *p_cache,
7959 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7960 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007961{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007962 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007963 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007964 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_CLI_C)
7969int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007970{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007971 int ret;
7972
7973 if( ssl == NULL ||
7974 session == NULL ||
7975 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007976 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007979 }
7980
Hanno Becker52055ae2019-02-06 14:30:46 +00007981 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7982 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007983 return( ret );
7984
Paul Bakker0a597072012-09-25 21:55:46 +00007985 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007986
7987 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007990
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007991void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007992 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007994 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7995 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7996 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7997 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007998}
7999
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008000void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008001 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008002 int major, int minor )
8003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008005 return;
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008008 return;
8009
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008010 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008011}
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008014void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008015 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008016{
8017 conf->cert_profile = profile;
8018}
8019
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008020/* Append a new keycert entry to a (possibly empty) list */
8021static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8022 mbedtls_x509_crt *cert,
8023 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008024{
niisato8ee24222018-06-25 19:05:48 +09008025 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008026
niisato8ee24222018-06-25 19:05:48 +09008027 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8028 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008029 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008030
niisato8ee24222018-06-25 19:05:48 +09008031 new_cert->cert = cert;
8032 new_cert->key = key;
8033 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008034
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008035 /* Update head is the list was null, else add to the end */
8036 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008037 {
niisato8ee24222018-06-25 19:05:48 +09008038 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008039 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008040 else
8041 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008042 mbedtls_ssl_key_cert *cur = *head;
8043 while( cur->next != NULL )
8044 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008045 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008046 }
8047
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008048 return( 0 );
8049}
8050
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008051int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008052 mbedtls_x509_crt *own_cert,
8053 mbedtls_pk_context *pk_key )
8054{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008055 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008056}
8057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008058void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008059 mbedtls_x509_crt *ca_chain,
8060 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008061{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008062 conf->ca_chain = ca_chain;
8063 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008064
8065#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8066 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8067 * cannot be used together. */
8068 conf->f_ca_cb = NULL;
8069 conf->p_ca_cb = NULL;
8070#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008071}
Hanno Becker5adaad92019-03-27 16:54:37 +00008072
8073#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8074void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008075 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008076 void *p_ca_cb )
8077{
8078 conf->f_ca_cb = f_ca_cb;
8079 conf->p_ca_cb = p_ca_cb;
8080
8081 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8082 * cannot be used together. */
8083 conf->ca_chain = NULL;
8084 conf->ca_crl = NULL;
8085}
8086#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008088
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008089#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8090int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8091 mbedtls_x509_crt *own_cert,
8092 mbedtls_pk_context *pk_key )
8093{
8094 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8095 own_cert, pk_key ) );
8096}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008097
8098void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8099 mbedtls_x509_crt *ca_chain,
8100 mbedtls_x509_crl *ca_crl )
8101{
8102 ssl->handshake->sni_ca_chain = ca_chain;
8103 ssl->handshake->sni_ca_crl = ca_crl;
8104}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008105
8106void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8107 int authmode )
8108{
8109 ssl->handshake->sni_authmode = authmode;
8110}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008111#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8112
Hanno Becker8927c832019-04-03 12:52:50 +01008113#if defined(MBEDTLS_X509_CRT_PARSE_C)
8114void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8115 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8116 void *p_vrfy )
8117{
8118 ssl->f_vrfy = f_vrfy;
8119 ssl->p_vrfy = p_vrfy;
8120}
8121#endif
8122
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008123#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008124/*
8125 * Set EC J-PAKE password for current handshake
8126 */
8127int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8128 const unsigned char *pw,
8129 size_t pw_len )
8130{
8131 mbedtls_ecjpake_role role;
8132
Janos Follath8eb64132016-06-03 15:40:57 +01008133 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8135
8136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8137 role = MBEDTLS_ECJPAKE_SERVER;
8138 else
8139 role = MBEDTLS_ECJPAKE_CLIENT;
8140
8141 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8142 role,
8143 MBEDTLS_MD_SHA256,
8144 MBEDTLS_ECP_DP_SECP256R1,
8145 pw, pw_len ) );
8146}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008147#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008150
8151static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8152{
8153 /* Remove reference to existing PSK, if any. */
8154#if defined(MBEDTLS_USE_PSA_CRYPTO)
8155 if( conf->psk_opaque != 0 )
8156 {
8157 /* The maintenance of the PSK key slot is the
8158 * user's responsibility. */
8159 conf->psk_opaque = 0;
8160 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008161 /* This and the following branch should never
8162 * be taken simultaenously as we maintain the
8163 * invariant that raw and opaque PSKs are never
8164 * configured simultaneously. As a safeguard,
8165 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008166#endif /* MBEDTLS_USE_PSA_CRYPTO */
8167 if( conf->psk != NULL )
8168 {
8169 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8170
8171 mbedtls_free( conf->psk );
8172 conf->psk = NULL;
8173 conf->psk_len = 0;
8174 }
8175
8176 /* Remove reference to PSK identity, if any. */
8177 if( conf->psk_identity != NULL )
8178 {
8179 mbedtls_free( conf->psk_identity );
8180 conf->psk_identity = NULL;
8181 conf->psk_identity_len = 0;
8182 }
8183}
8184
Hanno Becker7390c712018-11-15 13:33:04 +00008185/* This function assumes that PSK identity in the SSL config is unset.
8186 * It checks that the provided identity is well-formed and attempts
8187 * to make a copy of it in the SSL config.
8188 * On failure, the PSK identity in the config remains unset. */
8189static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8190 unsigned char const *psk_identity,
8191 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008192{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008193 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008194 if( psk_identity == NULL ||
8195 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008196 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008197 {
8198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8199 }
8200
Hanno Becker7390c712018-11-15 13:33:04 +00008201 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8202 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008203 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008204
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008205 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008206 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008207
8208 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008209}
8210
Hanno Becker7390c712018-11-15 13:33:04 +00008211int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8212 const unsigned char *psk, size_t psk_len,
8213 const unsigned char *psk_identity, size_t psk_identity_len )
8214{
8215 int ret;
8216 /* Remove opaque/raw PSK + PSK Identity */
8217 ssl_conf_remove_psk( conf );
8218
8219 /* Check and set raw PSK */
8220 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8222 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8223 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8224 conf->psk_len = psk_len;
8225 memcpy( conf->psk, psk, conf->psk_len );
8226
8227 /* Check and set PSK Identity */
8228 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8229 if( ret != 0 )
8230 ssl_conf_remove_psk( conf );
8231
8232 return( ret );
8233}
8234
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008235static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8236{
8237#if defined(MBEDTLS_USE_PSA_CRYPTO)
8238 if( ssl->handshake->psk_opaque != 0 )
8239 {
8240 ssl->handshake->psk_opaque = 0;
8241 }
8242 else
8243#endif /* MBEDTLS_USE_PSA_CRYPTO */
8244 if( ssl->handshake->psk != NULL )
8245 {
8246 mbedtls_platform_zeroize( ssl->handshake->psk,
8247 ssl->handshake->psk_len );
8248 mbedtls_free( ssl->handshake->psk );
8249 ssl->handshake->psk_len = 0;
8250 }
8251}
8252
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008253int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8254 const unsigned char *psk, size_t psk_len )
8255{
8256 if( psk == NULL || ssl->handshake == NULL )
8257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8258
8259 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8261
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008262 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008263
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008264 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008265 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008266
8267 ssl->handshake->psk_len = psk_len;
8268 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8269
8270 return( 0 );
8271}
8272
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008273#if defined(MBEDTLS_USE_PSA_CRYPTO)
8274int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008275 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008276 const unsigned char *psk_identity,
8277 size_t psk_identity_len )
8278{
Hanno Becker7390c712018-11-15 13:33:04 +00008279 int ret;
8280 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008281 ssl_conf_remove_psk( conf );
8282
Hanno Becker7390c712018-11-15 13:33:04 +00008283 /* Check and set opaque PSK */
8284 if( psk_slot == 0 )
8285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008286 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008287
8288 /* Check and set PSK Identity */
8289 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8290 psk_identity_len );
8291 if( ret != 0 )
8292 ssl_conf_remove_psk( conf );
8293
8294 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008295}
8296
8297int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008298 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008299{
8300 if( psk_slot == 0 || ssl->handshake == NULL )
8301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302
8303 ssl_remove_psk( ssl );
8304 ssl->handshake->psk_opaque = psk_slot;
8305 return( 0 );
8306}
8307#endif /* MBEDTLS_USE_PSA_CRYPTO */
8308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008309void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008311 size_t),
8312 void *p_psk )
8313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008314 conf->f_psk = f_psk;
8315 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008318
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008319#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008320
8321#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008322int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008323{
8324 int ret;
8325
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008326 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8327 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8328 {
8329 mbedtls_mpi_free( &conf->dhm_P );
8330 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008331 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008333
8334 return( 0 );
8335}
Hanno Becker470a8c42017-10-04 15:28:46 +01008336#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008337
Hanno Beckera90658f2017-10-04 15:29:08 +01008338int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8339 const unsigned char *dhm_P, size_t P_len,
8340 const unsigned char *dhm_G, size_t G_len )
8341{
8342 int ret;
8343
8344 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8345 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8346 {
8347 mbedtls_mpi_free( &conf->dhm_P );
8348 mbedtls_mpi_free( &conf->dhm_G );
8349 return( ret );
8350 }
8351
8352 return( 0 );
8353}
Paul Bakker5121ce52009-01-03 21:22:43 +00008354
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008355int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008356{
8357 int ret;
8358
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008359 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8360 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8361 {
8362 mbedtls_mpi_free( &conf->dhm_P );
8363 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008364 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008365 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008366
8367 return( 0 );
8368}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008369#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008370
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008371#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8372/*
8373 * Set the minimum length for Diffie-Hellman parameters
8374 */
8375void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8376 unsigned int bitlen )
8377{
8378 conf->dhm_min_bitlen = bitlen;
8379}
8380#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8381
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008382#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008383/*
8384 * Set allowed/preferred hashes for handshake signatures
8385 */
8386void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8387 const int *hashes )
8388{
8389 conf->sig_hashes = hashes;
8390}
Hanno Becker947194e2017-04-07 13:25:49 +01008391#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008392
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008393#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008394/*
8395 * Set the allowed elliptic curves
8396 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008398 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008401}
Hanno Becker947194e2017-04-07 13:25:49 +01008402#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008403
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008406{
Hanno Becker947194e2017-04-07 13:25:49 +01008407 /* Initialize to suppress unnecessary compiler warning */
8408 size_t hostname_len = 0;
8409
8410 /* Check if new hostname is valid before
8411 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008412 if( hostname != NULL )
8413 {
8414 hostname_len = strlen( hostname );
8415
8416 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8418 }
8419
8420 /* Now it's clear that we will overwrite the old hostname,
8421 * so we can free it safely */
8422
8423 if( ssl->hostname != NULL )
8424 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008425 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008426 mbedtls_free( ssl->hostname );
8427 }
8428
8429 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008430
Paul Bakker5121ce52009-01-03 21:22:43 +00008431 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008432 {
8433 ssl->hostname = NULL;
8434 }
8435 else
8436 {
8437 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008438 if( ssl->hostname == NULL )
8439 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008440
Hanno Becker947194e2017-04-07 13:25:49 +01008441 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008442
Hanno Becker947194e2017-04-07 13:25:49 +01008443 ssl->hostname[hostname_len] = '\0';
8444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008445
8446 return( 0 );
8447}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008448#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008449
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008450#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008453 const unsigned char *, size_t),
8454 void *p_sni )
8455{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008456 conf->f_sni = f_sni;
8457 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008462int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008463{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008464 size_t cur_len, tot_len;
8465 const char **p;
8466
8467 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008468 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8469 * MUST NOT be truncated."
8470 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008471 */
8472 tot_len = 0;
8473 for( p = protos; *p != NULL; p++ )
8474 {
8475 cur_len = strlen( *p );
8476 tot_len += cur_len;
8477
8478 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008480 }
8481
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008482 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008483
8484 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008485}
8486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008488{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008489 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008492
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008493void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008495 conf->max_major_ver = major;
8496 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008497}
8498
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008499void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008501 conf->min_major_ver = major;
8502 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008503}
8504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008507{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008508 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008509}
8510#endif
8511
Janos Follath088ce432017-04-10 12:42:31 +01008512#if defined(MBEDTLS_SSL_SRV_C)
8513void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8514 char cert_req_ca_list )
8515{
8516 conf->cert_req_ca_list = cert_req_ca_list;
8517}
8518#endif
8519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008523 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008524}
8525#endif
8526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008528void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008529{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008530 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008531}
8532#endif
8533
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008534#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008535void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008536{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008537 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008538}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008539#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008542int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008545 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008548 }
8549
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008550 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008551
8552 return( 0 );
8553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008557void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008558{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008559 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008564void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008565{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008566 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008567}
8568#endif
8569
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008570void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008572 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008573}
8574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008576void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008577{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008578 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008579}
8580
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008581void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008583 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008584}
8585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008586void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008587 const unsigned char period[8] )
8588{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008589 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008594#if defined(MBEDTLS_SSL_CLI_C)
8595void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008596{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008597 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008598}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008599#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008600
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008601#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008602void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8603 mbedtls_ssl_ticket_write_t *f_ticket_write,
8604 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8605 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008606{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008607 conf->f_ticket_write = f_ticket_write;
8608 conf->f_ticket_parse = f_ticket_parse;
8609 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008610}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008611#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008613
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008614#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8615void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8616 mbedtls_ssl_export_keys_t *f_export_keys,
8617 void *p_export_keys )
8618{
8619 conf->f_export_keys = f_export_keys;
8620 conf->p_export_keys = p_export_keys;
8621}
8622#endif
8623
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008624#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008625void mbedtls_ssl_conf_async_private_cb(
8626 mbedtls_ssl_config *conf,
8627 mbedtls_ssl_async_sign_t *f_async_sign,
8628 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8629 mbedtls_ssl_async_resume_t *f_async_resume,
8630 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008631 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008632{
8633 conf->f_async_sign_start = f_async_sign;
8634 conf->f_async_decrypt_start = f_async_decrypt;
8635 conf->f_async_resume = f_async_resume;
8636 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008637 conf->p_async_config_data = async_config_data;
8638}
8639
Gilles Peskine8f97af72018-04-26 11:46:10 +02008640void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8641{
8642 return( conf->p_async_config_data );
8643}
8644
Gilles Peskine1febfef2018-04-30 11:54:39 +02008645void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008646{
8647 if( ssl->handshake == NULL )
8648 return( NULL );
8649 else
8650 return( ssl->handshake->user_async_ctx );
8651}
8652
Gilles Peskine1febfef2018-04-30 11:54:39 +02008653void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008654 void *ctx )
8655{
8656 if( ssl->handshake != NULL )
8657 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008658}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008659#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008660
Paul Bakker5121ce52009-01-03 21:22:43 +00008661/*
8662 * SSL get accessors
8663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008665{
8666 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8667}
8668
Hanno Becker8b170a02017-10-10 11:51:19 +01008669int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8670{
8671 /*
8672 * Case A: We're currently holding back
8673 * a message for further processing.
8674 */
8675
8676 if( ssl->keep_current_message == 1 )
8677 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008679 return( 1 );
8680 }
8681
8682 /*
8683 * Case B: Further records are pending in the current datagram.
8684 */
8685
8686#if defined(MBEDTLS_SSL_PROTO_DTLS)
8687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8688 ssl->in_left > ssl->next_record_offset )
8689 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008691 return( 1 );
8692 }
8693#endif /* MBEDTLS_SSL_PROTO_DTLS */
8694
8695 /*
8696 * Case C: A handshake message is being processed.
8697 */
8698
Hanno Becker8b170a02017-10-10 11:51:19 +01008699 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8700 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008702 return( 1 );
8703 }
8704
8705 /*
8706 * Case D: An application data message is being processed
8707 */
8708 if( ssl->in_offt != NULL )
8709 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008711 return( 1 );
8712 }
8713
8714 /*
8715 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008716 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008717 * we implement support for multiple alerts in single records.
8718 */
8719
8720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8721 return( 0 );
8722}
8723
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008724uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008725{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008726 if( ssl->session != NULL )
8727 return( ssl->session->verify_result );
8728
8729 if( ssl->session_negotiate != NULL )
8730 return( ssl->session_negotiate->verify_result );
8731
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008732 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008733}
8734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008736{
Paul Bakker926c8e42013-03-06 10:23:34 +01008737 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008738 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008741}
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008747 {
8748 switch( ssl->minor_ver )
8749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008751 return( "DTLSv1.0" );
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008754 return( "DTLSv1.2" );
8755
8756 default:
8757 return( "unknown (DTLS)" );
8758 }
8759 }
8760#endif
8761
Paul Bakker43ca69c2011-01-15 17:35:19 +00008762 switch( ssl->minor_ver )
8763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008765 return( "SSLv3.0" );
8766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008768 return( "TLSv1.0" );
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008771 return( "TLSv1.1" );
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008774 return( "TLSv1.2" );
8775
Paul Bakker43ca69c2011-01-15 17:35:19 +00008776 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008777 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008778 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008779}
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008782{
Hanno Becker3136ede2018-08-17 15:28:19 +01008783 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008785 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008786
Hanno Becker78640902018-08-13 16:35:15 +01008787 if( transform == NULL )
8788 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790#if defined(MBEDTLS_ZLIB_SUPPORT)
8791 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008793#endif
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797 case MBEDTLS_MODE_GCM:
8798 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008799 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008801 transform_expansion = transform->minlen;
8802 break;
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008805
8806 block_size = mbedtls_cipher_get_block_size(
8807 &transform->cipher_ctx_enc );
8808
Hanno Becker3136ede2018-08-17 15:28:19 +01008809 /* Expansion due to the addition of the MAC. */
8810 transform_expansion += transform->maclen;
8811
8812 /* Expansion due to the addition of CBC padding;
8813 * Theoretically up to 256 bytes, but we never use
8814 * more than the block size of the underlying cipher. */
8815 transform_expansion += block_size;
8816
8817 /* For TLS 1.1 or higher, an explicit IV is added
8818 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008819#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8820 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008821 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008822#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008823
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008824 break;
8825
8826 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008829 }
8830
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008831 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008832}
8833
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008834#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8835size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8836{
8837 size_t max_len;
8838
8839 /*
8840 * Assume mfl_code is correct since it was checked when set
8841 */
Angus Grattond8213d02016-05-25 20:56:48 +10008842 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008843
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008844 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008845 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008846 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008847 {
Angus Grattond8213d02016-05-25 20:56:48 +10008848 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008849 }
8850
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008851 /* During a handshake, use the value being negotiated */
8852 if( ssl->session_negotiate != NULL &&
8853 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8854 {
8855 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8856 }
8857
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008858 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008859}
8860#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8861
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008862#if defined(MBEDTLS_SSL_PROTO_DTLS)
8863static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8864{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008865 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8866 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8867 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8868 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8869 return ( 0 );
8870
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008871 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8872 return( ssl->mtu );
8873
8874 if( ssl->mtu == 0 )
8875 return( ssl->handshake->mtu );
8876
8877 return( ssl->mtu < ssl->handshake->mtu ?
8878 ssl->mtu : ssl->handshake->mtu );
8879}
8880#endif /* MBEDTLS_SSL_PROTO_DTLS */
8881
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008882int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8883{
8884 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8885
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008886#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8887 !defined(MBEDTLS_SSL_PROTO_DTLS)
8888 (void) ssl;
8889#endif
8890
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8892 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8893
8894 if( max_len > mfl )
8895 max_len = mfl;
8896#endif
8897
8898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008899 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008900 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008901 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008902 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8903 const size_t overhead = (size_t) ret;
8904
8905 if( ret < 0 )
8906 return( ret );
8907
8908 if( mtu <= overhead )
8909 {
8910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8911 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8912 }
8913
8914 if( max_len > mtu - overhead )
8915 max_len = mtu - overhead;
8916 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008917#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008918
Hanno Becker0defedb2018-08-10 12:35:02 +01008919#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8920 !defined(MBEDTLS_SSL_PROTO_DTLS)
8921 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008922#endif
8923
8924 return( (int) max_len );
8925}
8926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927#if defined(MBEDTLS_X509_CRT_PARSE_C)
8928const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008929{
8930 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008931 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008932
Hanno Beckere6824572019-02-07 13:18:46 +00008933#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008934 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008935#else
8936 return( NULL );
8937#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008942int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8943 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008944{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008945 if( ssl == NULL ||
8946 dst == NULL ||
8947 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008948 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008951 }
8952
Hanno Becker52055ae2019-02-06 14:30:46 +00008953 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008956
Paul Bakker5121ce52009-01-03 21:22:43 +00008957/*
Paul Bakker1961b702013-01-25 14:49:24 +01008958 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008963
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008964 if( ssl == NULL || ssl->conf == NULL )
8965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008974#endif
8975
Paul Bakker1961b702013-01-25 14:49:24 +01008976 return( ret );
8977}
8978
8979/*
8980 * Perform the SSL handshake
8981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008983{
8984 int ret = 0;
8985
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008986 if( ssl == NULL || ssl->conf == NULL )
8987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008994
8995 if( ret != 0 )
8996 break;
8997 }
8998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009000
9001 return( ret );
9002}
9003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004#if defined(MBEDTLS_SSL_RENEGOTIATION)
9005#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009006/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009007 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009010{
9011 int ret;
9012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009014
9015 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9017 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009018
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009019 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009020 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009022 return( ret );
9023 }
9024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009026
9027 return( 0 );
9028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009030
9031/*
9032 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 * - any side: calling mbedtls_ssl_renegotiate(),
9034 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9035 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009036 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009037 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009041{
9042 int ret;
9043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009045
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009046 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9047 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009048
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009049 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9050 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009052 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009054 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009055 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009056 ssl->handshake->out_msg_seq = 1;
9057 else
9058 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009059 }
9060#endif
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9063 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009068 return( ret );
9069 }
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009072
9073 return( 0 );
9074}
9075
9076/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009077 * Renegotiate current connection on client,
9078 * or request renegotiation on server
9079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009083
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009084 if( ssl == NULL || ssl->conf == NULL )
9085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009088 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009089 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009095
9096 /* Did we already try/start sending HelloRequest? */
9097 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009099
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009100 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009101 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009105 /*
9106 * On client, either start the renegotiation process or,
9107 * if already in progress, continue the handshake
9108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009113
9114 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009117 return( ret );
9118 }
9119 }
9120 else
9121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009125 return( ret );
9126 }
9127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009129
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009130 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009131}
9132
9133/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009134 * Check record counters and renegotiate if they're above the limit.
9135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009137{
Andres AG2196c7f2016-12-15 17:01:16 +00009138 size_t ep_len = ssl_ep_len( ssl );
9139 int in_ctr_cmp;
9140 int out_ctr_cmp;
9141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9143 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009144 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009145 {
9146 return( 0 );
9147 }
9148
Andres AG2196c7f2016-12-15 17:01:16 +00009149 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9150 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009151 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009152 ssl->conf->renego_period + ep_len, 8 - ep_len );
9153
9154 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009155 {
9156 return( 0 );
9157 }
9158
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009163
9164/*
9165 * Receive application data decrypted from the SSL layer
9166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009168{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009169 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009170 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009171
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009172 if( ssl == NULL || ssl->conf == NULL )
9173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009181 return( ret );
9182
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009183 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009185 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009186 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009187 return( ret );
9188 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009189 }
9190#endif
9191
Hanno Becker4a810fb2017-05-24 16:27:30 +01009192 /*
9193 * Check if renegotiation is necessary and/or handshake is
9194 * in process. If yes, perform/continue, and fall through
9195 * if an unexpected packet is received while the client
9196 * is waiting for the ServerHello.
9197 *
9198 * (There is no equivalent to the last condition on
9199 * the server-side as it is not treated as within
9200 * a handshake while waiting for the ClientHello
9201 * after a renegotiation request.)
9202 */
9203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009205 ret = ssl_check_ctr_renegotiate( ssl );
9206 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9207 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009210 return( ret );
9211 }
9212#endif
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009217 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9218 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009221 return( ret );
9222 }
9223 }
9224
Hanno Beckere41158b2017-10-23 13:30:32 +01009225 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009226 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009227 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009228 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009229 if( ssl->f_get_timer != NULL &&
9230 ssl->f_get_timer( ssl->p_timer ) == -1 )
9231 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009232 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009233 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009234
Hanno Becker327c93b2018-08-15 13:56:18 +01009235 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009236 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009237 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9238 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009239
Hanno Becker4a810fb2017-05-24 16:27:30 +01009240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9241 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009242 }
9243
9244 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009246 {
9247 /*
9248 * OpenSSL sends empty messages to randomize the IV
9249 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009250 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009253 return( 0 );
9254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009256 return( ret );
9257 }
9258 }
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009263
Hanno Becker4a810fb2017-05-24 16:27:30 +01009264 /*
9265 * - For client-side, expect SERVER_HELLO_REQUEST.
9266 * - For server-side, expect CLIENT_HELLO.
9267 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9268 */
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009273 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009276
9277 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009280 {
9281 continue;
9282 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009285 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009286#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009287
Hanno Becker4a810fb2017-05-24 16:27:30 +01009288#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009289 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009293
9294 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009297 {
9298 continue;
9299 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009302 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009303#endif /* MBEDTLS_SSL_SRV_C */
9304
Hanno Becker21df7f92017-10-17 11:03:26 +01009305#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009306 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009307 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9308 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9309 ssl->conf->allow_legacy_renegotiation ==
9310 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9311 {
9312 /*
9313 * Accept renegotiation request
9314 */
Paul Bakker48916f92012-09-16 19:57:18 +00009315
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009316 /* DTLS clients need to know renego is server-initiated */
9317#if defined(MBEDTLS_SSL_PROTO_DTLS)
9318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9319 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9320 {
9321 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9322 }
9323#endif
9324 ret = ssl_start_renegotiation( ssl );
9325 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9326 ret != 0 )
9327 {
9328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9329 return( ret );
9330 }
9331 }
9332 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009333#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009334 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009335 /*
9336 * Refuse renegotiation
9337 */
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_SSL_PROTO_SSL3)
9342 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009343 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009344 /* SSLv3 does not have a "no_renegotiation" warning, so
9345 we send a fatal alert and abort the connection. */
9346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9347 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9348 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009349 }
9350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9352#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9353 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9354 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9357 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9358 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009359 {
9360 return( ret );
9361 }
Paul Bakker48916f92012-09-16 19:57:18 +00009362 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009363 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9365 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009369 }
Paul Bakker48916f92012-09-16 19:57:18 +00009370 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009371
Hanno Becker90333da2017-10-10 11:27:13 +01009372 /* At this point, we don't know whether the renegotiation has been
9373 * completed or not. The cases to consider are the following:
9374 * 1) The renegotiation is complete. In this case, no new record
9375 * has been read yet.
9376 * 2) The renegotiation is incomplete because the client received
9377 * an application data record while awaiting the ServerHello.
9378 * 3) The renegotiation is incomplete because the client received
9379 * a non-handshake, non-application data message while awaiting
9380 * the ServerHello.
9381 * In each of these case, looping will be the proper action:
9382 * - For 1), the next iteration will read a new record and check
9383 * if it's application data.
9384 * - For 2), the loop condition isn't satisfied as application data
9385 * is present, hence continue is the same as break
9386 * - For 3), the loop condition is satisfied and read_record
9387 * will re-deliver the message that was held back by the client
9388 * when expecting the ServerHello.
9389 */
9390 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009391 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009392#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009394 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009395 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009396 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009397 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009400 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009402 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009403 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9408 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009411 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009412 }
9413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9417 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009418 }
9419
9420 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009421
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009422 /* We're going to return something now, cancel timer,
9423 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009425 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009426
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009428 /* If we requested renego but received AppData, resend HelloRequest.
9429 * Do it now, after setting in_offt, to avoid taking this branch
9430 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009434 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009435 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009438 return( ret );
9439 }
9440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009442#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009443 }
9444
9445 n = ( len < ssl->in_msglen )
9446 ? len : ssl->in_msglen;
9447
9448 memcpy( buf, ssl->in_offt, n );
9449 ssl->in_msglen -= n;
9450
9451 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009452 {
9453 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009454 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009455 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009457 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009458 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009459 /* more data available */
9460 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009464
Paul Bakker23986e52011-04-24 08:57:21 +00009465 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009466}
9467
9468/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009469 * Send application data to be encrypted by the SSL layer, taking care of max
9470 * fragment length and buffer size.
9471 *
9472 * According to RFC 5246 Section 6.2.1:
9473 *
9474 * Zero-length fragments of Application data MAY be sent as they are
9475 * potentially useful as a traffic analysis countermeasure.
9476 *
9477 * Therefore, it is possible that the input message length is 0 and the
9478 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009479 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009480static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009481 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009482{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009483 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9484 const size_t max_len = (size_t) ret;
9485
9486 if( ret < 0 )
9487 {
9488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9489 return( ret );
9490 }
9491
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009492 if( len > max_len )
9493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009498 "maximum fragment length: %d > %d",
9499 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009501 }
9502 else
9503#endif
9504 len = max_len;
9505 }
Paul Bakker887bd502011-06-08 13:10:54 +00009506
Paul Bakker5121ce52009-01-03 21:22:43 +00009507 if( ssl->out_left != 0 )
9508 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009509 /*
9510 * The user has previously tried to send the data and
9511 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9512 * written. In this case, we expect the high-level write function
9513 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009518 return( ret );
9519 }
9520 }
Paul Bakker887bd502011-06-08 13:10:54 +00009521 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009522 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009523 /*
9524 * The user is trying to send a message the first time, so we need to
9525 * copy the data into the internal buffers and setup the data structure
9526 * to keep track of partial writes
9527 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009528 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009530 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009531
Hanno Becker67bc7c32018-08-06 11:33:50 +01009532 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009535 return( ret );
9536 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009537 }
9538
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009539 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009540}
9541
9542/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009543 * Write application data, doing 1/n-1 splitting if necessary.
9544 *
9545 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009546 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009547 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009550static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009551 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009552{
9553 int ret;
9554
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009555 if( ssl->conf->cbc_record_splitting ==
9556 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009557 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9559 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9560 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009561 {
9562 return( ssl_write_real( ssl, buf, len ) );
9563 }
9564
9565 if( ssl->split_done == 0 )
9566 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009567 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009568 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009569 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009570 }
9571
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009572 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9573 return( ret );
9574 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009575
9576 return( ret + 1 );
9577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009579
9580/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009581 * Write application data (public-facing wrapper)
9582 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009583int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009584{
9585 int ret;
9586
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009588
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009589 if( ssl == NULL || ssl->conf == NULL )
9590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9591
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009592#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009593 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9594 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009596 return( ret );
9597 }
9598#endif
9599
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009600 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009601 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009602 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009603 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009605 return( ret );
9606 }
9607 }
9608
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009609#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009610 ret = ssl_write_split( ssl, buf, len );
9611#else
9612 ret = ssl_write_real( ssl, buf, len );
9613#endif
9614
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009616
9617 return( ret );
9618}
9619
9620/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009621 * Notify the peer that the connection is being closed
9622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009624{
9625 int ret;
9626
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009627 if( ssl == NULL || ssl->conf == NULL )
9628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009631
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009632 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9638 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9639 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 return( ret );
9643 }
9644 }
9645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009647
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009648 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009649}
9650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009652{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009653 if( transform == NULL )
9654 return;
9655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009657 deflateEnd( &transform->ctx_deflate );
9658 inflateEnd( &transform->ctx_inflate );
9659#endif
9660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9662 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009663
Hanno Beckerd56ed242018-01-03 15:32:51 +00009664#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 mbedtls_md_free( &transform->md_ctx_enc );
9666 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009667#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009668
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009669 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009670}
9671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672#if defined(MBEDTLS_X509_CRT_PARSE_C)
9673static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009676
9677 while( cur != NULL )
9678 {
9679 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009681 cur = next;
9682 }
9683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009685
Hanno Becker0271f962018-08-16 13:23:47 +01009686#if defined(MBEDTLS_SSL_PROTO_DTLS)
9687
9688static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9689{
9690 unsigned offset;
9691 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9692
9693 if( hs == NULL )
9694 return;
9695
Hanno Becker283f5ef2018-08-24 09:34:47 +01009696 ssl_free_buffered_record( ssl );
9697
Hanno Becker0271f962018-08-16 13:23:47 +01009698 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009699 ssl_buffering_free_slot( ssl, offset );
9700}
9701
9702static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9703 uint8_t slot )
9704{
9705 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9706 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009707
9708 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9709 return;
9710
Hanno Beckere605b192018-08-21 15:59:07 +01009711 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009712 {
Hanno Beckere605b192018-08-21 15:59:07 +01009713 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009714 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009715 mbedtls_free( hs_buf->data );
9716 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009717 }
9718}
9719
9720#endif /* MBEDTLS_SSL_PROTO_DTLS */
9721
Gilles Peskine9b562d52018-04-25 20:32:43 +02009722void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009723{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009724 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9725
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009726 if( handshake == NULL )
9727 return;
9728
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009729#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9730 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9731 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009732 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009733 handshake->async_in_progress = 0;
9734 }
9735#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9736
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9738 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9739 mbedtls_md5_free( &handshake->fin_md5 );
9740 mbedtls_sha1_free( &handshake->fin_sha1 );
9741#endif
9742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9743#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009744#if defined(MBEDTLS_USE_PSA_CRYPTO)
9745 psa_hash_abort( &handshake->fin_sha256_psa );
9746#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009747 mbedtls_sha256_free( &handshake->fin_sha256 );
9748#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009749#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009750#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009751#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009752 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009753#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009754 mbedtls_sha512_free( &handshake->fin_sha512 );
9755#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009756#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_DHM_C)
9760 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#if defined(MBEDTLS_ECDH_C)
9763 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009764#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009765#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009766 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009767#if defined(MBEDTLS_SSL_CLI_C)
9768 mbedtls_free( handshake->ecjpake_cache );
9769 handshake->ecjpake_cache = NULL;
9770 handshake->ecjpake_cache_len = 0;
9771#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009772#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009773
Janos Follath4ae5c292016-02-10 11:27:43 +00009774#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9775 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009776 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009778#endif
9779
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009780#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9781 if( handshake->psk != NULL )
9782 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009783 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009784 mbedtls_free( handshake->psk );
9785 }
9786#endif
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9789 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009790 /*
9791 * Free only the linked list wrapper, not the keys themselves
9792 * since the belong to the SNI callback
9793 */
9794 if( handshake->sni_key_cert != NULL )
9795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009797
9798 while( cur != NULL )
9799 {
9800 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009802 cur = next;
9803 }
9804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009806
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009807#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009808 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009809 if( handshake->ecrs_peer_cert != NULL )
9810 {
9811 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9812 mbedtls_free( handshake->ecrs_peer_cert );
9813 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009814#endif
9815
Hanno Becker75173122019-02-06 16:18:31 +00009816#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9817 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9818 mbedtls_pk_free( &handshake->peer_pubkey );
9819#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821#if defined(MBEDTLS_SSL_PROTO_DTLS)
9822 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009823 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009824 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009825#endif
9826
Hanno Becker4a63ed42019-01-08 11:39:35 +00009827#if defined(MBEDTLS_ECDH_C) && \
9828 defined(MBEDTLS_USE_PSA_CRYPTO)
9829 psa_destroy_key( handshake->ecdh_psa_privkey );
9830#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9831
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009832 mbedtls_platform_zeroize( handshake,
9833 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009834}
9835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009837{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009838 if( session == NULL )
9839 return;
9840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009842 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009843#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009844
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009845#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009847#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009848
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009849 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009850}
9851
Paul Bakker5121ce52009-01-03 21:22:43 +00009852/*
9853 * Free an SSL context
9854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009856{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009857 if( ssl == NULL )
9858 return;
9859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009861
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009862 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009863 {
Angus Grattond8213d02016-05-25 20:56:48 +10009864 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009866 }
9867
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009868 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009869 {
Angus Grattond8213d02016-05-25 20:56:48 +10009870 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009872 }
9873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009875 if( ssl->compress_buf != NULL )
9876 {
Angus Grattond8213d02016-05-25 20:56:48 +10009877 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009879 }
9880#endif
9881
Paul Bakker48916f92012-09-16 19:57:18 +00009882 if( ssl->transform )
9883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884 mbedtls_ssl_transform_free( ssl->transform );
9885 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009886 }
9887
9888 if( ssl->handshake )
9889 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009890 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9892 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 mbedtls_free( ssl->handshake );
9895 mbedtls_free( ssl->transform_negotiate );
9896 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009897 }
9898
Paul Bakkerc0463502013-02-14 11:19:38 +01009899 if( ssl->session )
9900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901 mbedtls_ssl_session_free( ssl->session );
9902 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009903 }
9904
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009905#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009906 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009907 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009908 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009910 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9914 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9917 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009918 }
9919#endif
9920
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009921#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009923#endif
9924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009926
Paul Bakker86f04f42013-02-14 11:20:09 +01009927 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009928 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009929}
9930
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009931/*
9932 * Initialze mbedtls_ssl_config
9933 */
9934void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9935{
9936 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9937}
9938
Simon Butcherc97b6972015-12-27 23:48:17 +00009939#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009940static int ssl_preset_default_hashes[] = {
9941#if defined(MBEDTLS_SHA512_C)
9942 MBEDTLS_MD_SHA512,
9943 MBEDTLS_MD_SHA384,
9944#endif
9945#if defined(MBEDTLS_SHA256_C)
9946 MBEDTLS_MD_SHA256,
9947 MBEDTLS_MD_SHA224,
9948#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009949#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009950 MBEDTLS_MD_SHA1,
9951#endif
9952 MBEDTLS_MD_NONE
9953};
Simon Butcherc97b6972015-12-27 23:48:17 +00009954#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009955
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009956static int ssl_preset_suiteb_ciphersuites[] = {
9957 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9958 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9959 0
9960};
9961
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009962#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009963static int ssl_preset_suiteb_hashes[] = {
9964 MBEDTLS_MD_SHA256,
9965 MBEDTLS_MD_SHA384,
9966 MBEDTLS_MD_NONE
9967};
9968#endif
9969
9970#if defined(MBEDTLS_ECP_C)
9971static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9972 MBEDTLS_ECP_DP_SECP256R1,
9973 MBEDTLS_ECP_DP_SECP384R1,
9974 MBEDTLS_ECP_DP_NONE
9975};
9976#endif
9977
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009978/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009979 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009980 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009981int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009982 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009983{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009984#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009985 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009986#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009987
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009988 /* Use the functions here so that they are covered in tests,
9989 * but otherwise access member directly for efficiency */
9990 mbedtls_ssl_conf_endpoint( conf, endpoint );
9991 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009992
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009993 /*
9994 * Things that are common to all presets
9995 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009996#if defined(MBEDTLS_SSL_CLI_C)
9997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9998 {
9999 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10000#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10001 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10002#endif
10003 }
10004#endif
10005
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010006#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010007 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010008#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010009
10010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10011 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10012#endif
10013
10014#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10015 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10016#endif
10017
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010018#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10019 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10020#endif
10021
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010022#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010023 conf->f_cookie_write = ssl_cookie_write_dummy;
10024 conf->f_cookie_check = ssl_cookie_check_dummy;
10025#endif
10026
10027#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10028 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10029#endif
10030
Janos Follath088ce432017-04-10 12:42:31 +010010031#if defined(MBEDTLS_SSL_SRV_C)
10032 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10033#endif
10034
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010035#if defined(MBEDTLS_SSL_PROTO_DTLS)
10036 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10037 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10038#endif
10039
10040#if defined(MBEDTLS_SSL_RENEGOTIATION)
10041 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010042 memset( conf->renego_period, 0x00, 2 );
10043 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010044#endif
10045
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010046#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10047 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10048 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010049 const unsigned char dhm_p[] =
10050 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10051 const unsigned char dhm_g[] =
10052 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10053
Hanno Beckera90658f2017-10-04 15:29:08 +010010054 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10055 dhm_p, sizeof( dhm_p ),
10056 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010057 {
10058 return( ret );
10059 }
10060 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010061#endif
10062
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010063 /*
10064 * Preset-specific defaults
10065 */
10066 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010067 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010068 /*
10069 * NSA Suite B
10070 */
10071 case MBEDTLS_SSL_PRESET_SUITEB:
10072 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10073 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10074 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10075 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10076
10077 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10078 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10079 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10080 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10081 ssl_preset_suiteb_ciphersuites;
10082
10083#if defined(MBEDTLS_X509_CRT_PARSE_C)
10084 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010085#endif
10086
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010087#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010088 conf->sig_hashes = ssl_preset_suiteb_hashes;
10089#endif
10090
10091#if defined(MBEDTLS_ECP_C)
10092 conf->curve_list = ssl_preset_suiteb_curves;
10093#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010094 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010095
10096 /*
10097 * Default
10098 */
10099 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010100 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10101 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10102 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10103 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10104 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10105 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10106 MBEDTLS_SSL_MIN_MINOR_VERSION :
10107 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010108 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10109 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10110
10111#if defined(MBEDTLS_SSL_PROTO_DTLS)
10112 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10113 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10114#endif
10115
10116 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10117 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10118 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10119 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10120 mbedtls_ssl_list_ciphersuites();
10121
10122#if defined(MBEDTLS_X509_CRT_PARSE_C)
10123 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10124#endif
10125
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010126#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010127 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010128#endif
10129
10130#if defined(MBEDTLS_ECP_C)
10131 conf->curve_list = mbedtls_ecp_grp_id_list();
10132#endif
10133
10134#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10135 conf->dhm_min_bitlen = 1024;
10136#endif
10137 }
10138
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010139 return( 0 );
10140}
10141
10142/*
10143 * Free mbedtls_ssl_config
10144 */
10145void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10146{
10147#if defined(MBEDTLS_DHM_C)
10148 mbedtls_mpi_free( &conf->dhm_P );
10149 mbedtls_mpi_free( &conf->dhm_G );
10150#endif
10151
10152#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10153 if( conf->psk != NULL )
10154 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010155 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010156 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010157 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010158 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010159 }
10160
10161 if( conf->psk_identity != NULL )
10162 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010163 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010164 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010165 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010166 conf->psk_identity_len = 0;
10167 }
10168#endif
10169
10170#if defined(MBEDTLS_X509_CRT_PARSE_C)
10171 ssl_key_cert_free( conf->key_cert );
10172#endif
10173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010174 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010175}
10176
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010177#if defined(MBEDTLS_PK_C) && \
10178 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010179/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184#if defined(MBEDTLS_RSA_C)
10185 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10186 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188#if defined(MBEDTLS_ECDSA_C)
10189 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10190 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010191#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010193}
10194
Hanno Becker7e5437a2017-04-28 17:15:26 +010010195unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10196{
10197 switch( type ) {
10198 case MBEDTLS_PK_RSA:
10199 return( MBEDTLS_SSL_SIG_RSA );
10200 case MBEDTLS_PK_ECDSA:
10201 case MBEDTLS_PK_ECKEY:
10202 return( MBEDTLS_SSL_SIG_ECDSA );
10203 default:
10204 return( MBEDTLS_SSL_SIG_ANON );
10205 }
10206}
10207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010209{
10210 switch( sig )
10211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212#if defined(MBEDTLS_RSA_C)
10213 case MBEDTLS_SSL_SIG_RSA:
10214 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_ECDSA_C)
10217 case MBEDTLS_SSL_SIG_ECDSA:
10218 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010219#endif
10220 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010222 }
10223}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010224#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010225
Hanno Becker7e5437a2017-04-28 17:15:26 +010010226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10227 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10228
10229/* Find an entry in a signature-hash set matching a given hash algorithm. */
10230mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10231 mbedtls_pk_type_t sig_alg )
10232{
10233 switch( sig_alg )
10234 {
10235 case MBEDTLS_PK_RSA:
10236 return( set->rsa );
10237 case MBEDTLS_PK_ECDSA:
10238 return( set->ecdsa );
10239 default:
10240 return( MBEDTLS_MD_NONE );
10241 }
10242}
10243
10244/* Add a signature-hash-pair to a signature-hash set */
10245void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10246 mbedtls_pk_type_t sig_alg,
10247 mbedtls_md_type_t md_alg )
10248{
10249 switch( sig_alg )
10250 {
10251 case MBEDTLS_PK_RSA:
10252 if( set->rsa == MBEDTLS_MD_NONE )
10253 set->rsa = md_alg;
10254 break;
10255
10256 case MBEDTLS_PK_ECDSA:
10257 if( set->ecdsa == MBEDTLS_MD_NONE )
10258 set->ecdsa = md_alg;
10259 break;
10260
10261 default:
10262 break;
10263 }
10264}
10265
10266/* Allow exactly one hash algorithm for each signature. */
10267void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10268 mbedtls_md_type_t md_alg )
10269{
10270 set->rsa = md_alg;
10271 set->ecdsa = md_alg;
10272}
10273
10274#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10275 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10276
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010277/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010278 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010281{
10282 switch( hash )
10283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284#if defined(MBEDTLS_MD5_C)
10285 case MBEDTLS_SSL_HASH_MD5:
10286 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288#if defined(MBEDTLS_SHA1_C)
10289 case MBEDTLS_SSL_HASH_SHA1:
10290 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010292#if defined(MBEDTLS_SHA256_C)
10293 case MBEDTLS_SSL_HASH_SHA224:
10294 return( MBEDTLS_MD_SHA224 );
10295 case MBEDTLS_SSL_HASH_SHA256:
10296 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298#if defined(MBEDTLS_SHA512_C)
10299 case MBEDTLS_SSL_HASH_SHA384:
10300 return( MBEDTLS_MD_SHA384 );
10301 case MBEDTLS_SSL_HASH_SHA512:
10302 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010303#endif
10304 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010306 }
10307}
10308
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010309/*
10310 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10311 */
10312unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10313{
10314 switch( md )
10315 {
10316#if defined(MBEDTLS_MD5_C)
10317 case MBEDTLS_MD_MD5:
10318 return( MBEDTLS_SSL_HASH_MD5 );
10319#endif
10320#if defined(MBEDTLS_SHA1_C)
10321 case MBEDTLS_MD_SHA1:
10322 return( MBEDTLS_SSL_HASH_SHA1 );
10323#endif
10324#if defined(MBEDTLS_SHA256_C)
10325 case MBEDTLS_MD_SHA224:
10326 return( MBEDTLS_SSL_HASH_SHA224 );
10327 case MBEDTLS_MD_SHA256:
10328 return( MBEDTLS_SSL_HASH_SHA256 );
10329#endif
10330#if defined(MBEDTLS_SHA512_C)
10331 case MBEDTLS_MD_SHA384:
10332 return( MBEDTLS_SSL_HASH_SHA384 );
10333 case MBEDTLS_MD_SHA512:
10334 return( MBEDTLS_SSL_HASH_SHA512 );
10335#endif
10336 default:
10337 return( MBEDTLS_SSL_HASH_NONE );
10338 }
10339}
10340
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010341#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010342/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010343 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010344 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010345 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010346int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010349
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010350 if( ssl->conf->curve_list == NULL )
10351 return( -1 );
10352
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010353 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010354 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010355 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010356
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010357 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010358}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010359#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010360
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010361#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010362/*
10363 * Check if a hash proposed by the peer is in our list.
10364 * Return 0 if we're willing to use it, -1 otherwise.
10365 */
10366int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10367 mbedtls_md_type_t md )
10368{
10369 const int *cur;
10370
10371 if( ssl->conf->sig_hashes == NULL )
10372 return( -1 );
10373
10374 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10375 if( *cur == (int) md )
10376 return( 0 );
10377
10378 return( -1 );
10379}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010380#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382#if defined(MBEDTLS_X509_CRT_PARSE_C)
10383int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10384 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010385 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010386 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010387{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010388 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010390 int usage = 0;
10391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010393 const char *ext_oid;
10394 size_t ext_len;
10395#endif
10396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10398 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010399 ((void) cert);
10400 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010401 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010402#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10405 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010406 {
10407 /* Server part of the key exchange */
10408 switch( ciphersuite->key_exchange )
10409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 case MBEDTLS_KEY_EXCHANGE_RSA:
10411 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010412 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010413 break;
10414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10416 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10417 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10418 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010419 break;
10420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10422 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010423 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010424 break;
10425
10426 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 case MBEDTLS_KEY_EXCHANGE_NONE:
10428 case MBEDTLS_KEY_EXCHANGE_PSK:
10429 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10430 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010431 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010432 usage = 0;
10433 }
10434 }
10435 else
10436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10438 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010439 }
10440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010442 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010443 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010444 ret = -1;
10445 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010446#else
10447 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10451 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10454 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010455 }
10456 else
10457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10459 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010460 }
10461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010463 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010464 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010465 ret = -1;
10466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010468
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010469 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010472
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010473/*
10474 * Convert version numbers to/from wire format
10475 * and, for DTLS, to/from TLS equivalent.
10476 *
10477 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010478 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010479 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10480 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010483 unsigned char ver[2] )
10484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485#if defined(MBEDTLS_SSL_PROTO_DTLS)
10486 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010489 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10490
10491 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10492 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10493 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010494 else
10495#else
10496 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010497#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010498 {
10499 ver[0] = (unsigned char) major;
10500 ver[1] = (unsigned char) minor;
10501 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010502}
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010505 const unsigned char ver[2] )
10506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507#if defined(MBEDTLS_SSL_PROTO_DTLS)
10508 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010509 {
10510 *major = 255 - ver[0] + 2;
10511 *minor = 255 - ver[1] + 1;
10512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010514 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10515 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010516 else
10517#else
10518 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010519#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010520 {
10521 *major = ver[0];
10522 *minor = ver[1];
10523 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010524}
10525
Simon Butcher99000142016-10-13 17:21:01 +010010526int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10527{
10528#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10529 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10530 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10531
10532 switch( md )
10533 {
10534#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10535#if defined(MBEDTLS_MD5_C)
10536 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010537 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010538#endif
10539#if defined(MBEDTLS_SHA1_C)
10540 case MBEDTLS_SSL_HASH_SHA1:
10541 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10542 break;
10543#endif
10544#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10545#if defined(MBEDTLS_SHA512_C)
10546 case MBEDTLS_SSL_HASH_SHA384:
10547 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10548 break;
10549#endif
10550#if defined(MBEDTLS_SHA256_C)
10551 case MBEDTLS_SSL_HASH_SHA256:
10552 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10553 break;
10554#endif
10555 default:
10556 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10557 }
10558
10559 return 0;
10560#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10561 (void) ssl;
10562 (void) md;
10563
10564 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10566}
10567
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010568#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10569 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10570int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10571 unsigned char *output,
10572 unsigned char *data, size_t data_len )
10573{
10574 int ret = 0;
10575 mbedtls_md5_context mbedtls_md5;
10576 mbedtls_sha1_context mbedtls_sha1;
10577
10578 mbedtls_md5_init( &mbedtls_md5 );
10579 mbedtls_sha1_init( &mbedtls_sha1 );
10580
10581 /*
10582 * digitally-signed struct {
10583 * opaque md5_hash[16];
10584 * opaque sha_hash[20];
10585 * };
10586 *
10587 * md5_hash
10588 * MD5(ClientHello.random + ServerHello.random
10589 * + ServerParams);
10590 * sha_hash
10591 * SHA(ClientHello.random + ServerHello.random
10592 * + ServerParams);
10593 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010594 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010595 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010597 goto exit;
10598 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010599 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010600 ssl->handshake->randbytes, 64 ) ) != 0 )
10601 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010603 goto exit;
10604 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010605 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010606 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010608 goto exit;
10609 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010610 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010611 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010613 goto exit;
10614 }
10615
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010616 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010617 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010619 goto exit;
10620 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010621 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010622 ssl->handshake->randbytes, 64 ) ) != 0 )
10623 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010625 goto exit;
10626 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010627 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010628 data_len ) ) != 0 )
10629 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010631 goto exit;
10632 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010633 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010634 output + 16 ) ) != 0 )
10635 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010637 goto exit;
10638 }
10639
10640exit:
10641 mbedtls_md5_free( &mbedtls_md5 );
10642 mbedtls_sha1_free( &mbedtls_sha1 );
10643
10644 if( ret != 0 )
10645 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10646 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10647
10648 return( ret );
10649
10650}
10651#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10652 MBEDTLS_SSL_PROTO_TLS1_1 */
10653
10654#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10655 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010656
10657#if defined(MBEDTLS_USE_PSA_CRYPTO)
10658int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10659 unsigned char *hash, size_t *hashlen,
10660 unsigned char *data, size_t data_len,
10661 mbedtls_md_type_t md_alg )
10662{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010663 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010664 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010665 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10666
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010668
10669 if( ( status = psa_hash_setup( &hash_operation,
10670 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010671 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010672 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010673 goto exit;
10674 }
10675
Andrzej Kurek814feff2019-01-14 04:35:19 -050010676 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10677 64 ) ) != 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_update", 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,
10684 data, data_len ) ) != 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_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10691 hashlen ) ) != 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_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010694 goto exit;
10695 }
10696
10697exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010698 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010699 {
10700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10701 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010702 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010703 {
10704 case PSA_ERROR_NOT_SUPPORTED:
10705 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010706 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010707 case PSA_ERROR_BUFFER_TOO_SMALL:
10708 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10709 case PSA_ERROR_INSUFFICIENT_MEMORY:
10710 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10711 default:
10712 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10713 }
10714 }
10715 return( 0 );
10716}
10717
10718#else
10719
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010720int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010721 unsigned char *hash, size_t *hashlen,
10722 unsigned char *data, size_t data_len,
10723 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010724{
10725 int ret = 0;
10726 mbedtls_md_context_t ctx;
10727 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010728 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010729
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010731
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010732 mbedtls_md_init( &ctx );
10733
10734 /*
10735 * digitally-signed struct {
10736 * opaque client_random[32];
10737 * opaque server_random[32];
10738 * ServerDHParams params;
10739 * };
10740 */
10741 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10742 {
10743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10744 goto exit;
10745 }
10746 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10747 {
10748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10749 goto exit;
10750 }
10751 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10752 {
10753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10754 goto exit;
10755 }
10756 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10757 {
10758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10759 goto exit;
10760 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010761 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010762 {
10763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10764 goto exit;
10765 }
10766
10767exit:
10768 mbedtls_md_free( &ctx );
10769
10770 if( ret != 0 )
10771 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10772 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10773
10774 return( ret );
10775}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010776#endif /* MBEDTLS_USE_PSA_CRYPTO */
10777
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010778#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10779 MBEDTLS_SSL_PROTO_TLS1_2 */
10780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#endif /* MBEDTLS_SSL_TLS_C */