blob: a2350803a4918219478c320cfcca6a21e4e6abec [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001 cipher_info->mode == MBEDTLS_MODE_CCM ||
1002 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001004 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001006 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001007 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001008 transform->taglen =
1009 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001010
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001011 /* All modes haves 96-bit IVs;
1012 * GCM and CCM has 4 implicit and 8 explicit bytes
1013 * ChachaPoly has all 12 bytes implicit
1014 */
Paul Bakker68884e32013-01-07 18:20:04 +01001015 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001016 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1017 transform->fixed_ivlen = 12;
1018 else
1019 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001020
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001021 /* Minimum length of encrypted record */
1022 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025 else
1026 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001027 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1029 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001032 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001035 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001036 mac_key_len = mbedtls_md_get_size( md_info );
1037 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001040 /*
1041 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1042 * (rfc 6066 page 13 or rfc 2104 section 4),
1043 * so we only need to adjust the length here.
1044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001048
1049#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1050 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001051 * HMAC implementation which also truncates the key
1052 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001053 mac_key_len = transform->maclen;
1054#endif
1055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001057
1058 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001059 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001061 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001063 transform->minlen = transform->maclen;
1064 else
Paul Bakker68884e32013-01-07 18:20:04 +01001065 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001066 /*
1067 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001068 * 1. if EtM is in use: one block plus MAC
1069 * otherwise: * first multiple of blocklen greater than maclen
1070 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1073 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001074 {
1075 transform->minlen = transform->maclen
1076 + cipher_info->block_size;
1077 }
1078 else
1079#endif
1080 {
1081 transform->minlen = transform->maclen
1082 + cipher_info->block_size
1083 - transform->maclen % cipher_info->block_size;
1084 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1087 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1088 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001089 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001090 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1093 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1094 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001095 {
1096 transform->minlen += transform->ivlen;
1097 }
1098 else
1099#endif
1100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001103 }
Paul Bakker68884e32013-01-07 18:20:04 +01001104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 }
1106
Hanno Becker88aaf652017-12-27 08:17:40 +00001107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1108 (unsigned) keylen,
1109 (unsigned) transform->minlen,
1110 (unsigned) transform->ivlen,
1111 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 /*
1114 * Finally setup the cipher contexts, IVs and MAC secrets.
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001119 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001120 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001123 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001125 /*
1126 * This is not used in TLS v1.1.
1127 */
Paul Bakker48916f92012-09-16 19:57:18 +00001128 iv_copy_len = ( transform->fixed_ivlen ) ?
1129 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001130 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1131 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 }
1134 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_SSL_CLI_C */
1136#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001137 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001139 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001140 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Hanno Becker81c7b182017-11-09 18:39:33 +00001142 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001143 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001145 /*
1146 * This is not used in TLS v1.1.
1147 */
Paul Bakker48916f92012-09-16 19:57:18 +00001148 iv_copy_len = ( transform->fixed_ivlen ) ?
1149 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001150 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1151 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001152 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_PROTO_SSL3)
1162 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001163 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001164 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001168 }
1169
Hanno Becker81c7b182017-11-09 18:39:33 +00001170 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1171 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001172 }
1173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1176 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1177 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001178 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001179 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1180 For AEAD-based ciphersuites, there is nothing to do here. */
1181 if( mac_key_len != 0 )
1182 {
1183 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1184 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1185 }
Paul Bakker68884e32013-01-07 18:20:04 +01001186 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001187 else
1188#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001192 }
Paul Bakker68884e32013-01-07 18:20:04 +01001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1195 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001196 {
1197 int ret = 0;
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001200
Hanno Becker88aaf652017-12-27 08:17:40 +00001201 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001202 transform->iv_enc, transform->iv_dec,
1203 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001204 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001205 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1208 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001209 }
1210 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001212
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001213#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1214 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001215 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001216 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1217 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001218 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001219 iv_copy_len );
1220 }
1221#endif
1222
Hanno Beckerf704bef2018-11-16 15:21:18 +00001223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001224
1225 /* Only use PSA-based ciphers for TLS-1.2.
1226 * That's relevant at least for TLS-1.0, where
1227 * we assume that mbedtls_cipher_crypt() updates
1228 * the structure field for the IV, which the PSA-based
1229 * implementation currently doesn't. */
1230#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1231 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001232 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001233 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1234 cipher_info, taglen );
1235 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1236 {
1237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1238 return( ret );
1239 }
1240
1241 if( ret == 0 )
1242 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001244 psa_fallthrough = 0;
1245 }
1246 else
1247 {
1248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1249 psa_fallthrough = 1;
1250 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001251 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001252 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001253 psa_fallthrough = 1;
1254#else
1255 psa_fallthrough = 1;
1256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001257
Hanno Beckercb1cc802018-11-17 22:27:38 +00001258 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001259#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001260 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001261 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001264 return( ret );
1265 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001266
Hanno Beckerf704bef2018-11-16 15:21:18 +00001267#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001268 /* Only use PSA-based ciphers for TLS-1.2.
1269 * That's relevant at least for TLS-1.0, where
1270 * we assume that mbedtls_cipher_crypt() updates
1271 * the structure field for the IV, which the PSA-based
1272 * implementation currently doesn't. */
1273#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1274 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001275 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001276 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1277 cipher_info, taglen );
1278 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1279 {
1280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1281 return( ret );
1282 }
1283
1284 if( ret == 0 )
1285 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001287 psa_fallthrough = 0;
1288 }
1289 else
1290 {
1291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1292 psa_fallthrough = 1;
1293 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001294 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001295 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001296 psa_fallthrough = 1;
1297#else
1298 psa_fallthrough = 1;
1299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001300
Hanno Beckercb1cc802018-11-17 22:27:38 +00001301 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001302#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001303 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001304 cipher_info ) ) != 0 )
1305 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001307 return( ret );
1308 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001311 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315 return( ret );
1316 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001319 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001323 return( ret );
1324 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_CIPHER_MODE_CBC)
1327 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1330 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001333 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001334 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1337 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001340 return( ret );
1341 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001345 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001348 // Initialize compression
1349 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001351 {
Paul Bakker16770332013-10-11 09:59:44 +02001352 if( ssl->compress_buf == NULL )
1353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001355 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001356 if( ssl->compress_buf == NULL )
1357 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001359 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001360 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001361 }
1362 }
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001365
Paul Bakker48916f92012-09-16 19:57:18 +00001366 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1367 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001368
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001369 if( deflateInit( &transform->ctx_deflate,
1370 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001371 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1374 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001375 }
1376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
1381 return( 0 );
1382}
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_SSL_PROTO_SSL3)
1385void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001387 mbedtls_md5_context md5;
1388 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 unsigned char pad_1[48];
1390 unsigned char pad_2[48];
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001394 mbedtls_md5_init( &md5 );
1395 mbedtls_sha1_init( &sha1 );
1396
1397 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1398 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Paul Bakker380da532012-04-18 16:10:25 +00001400 memset( pad_1, 0x36, 48 );
1401 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001403 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1404 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1405 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001407 mbedtls_md5_starts_ret( &md5 );
1408 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1409 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1410 mbedtls_md5_update_ret( &md5, hash, 16 );
1411 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001413 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1414 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1415 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001416
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001417 mbedtls_sha1_starts_ret( &sha1 );
1418 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1419 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1420 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1421 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001426 mbedtls_md5_free( &md5 );
1427 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001428
Paul Bakker380da532012-04-18 16:10:25 +00001429 return;
1430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1434void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001435{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001436 mbedtls_md5_context md5;
1437 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001441 mbedtls_md5_init( &md5 );
1442 mbedtls_sha1_init( &sha1 );
1443
1444 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1445 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Andrzej Kurekeb342242019-01-29 09:14:33 -05001447 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001448 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001452
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_md5_free( &md5 );
1454 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001455
Paul Bakker380da532012-04-18 16:10:25 +00001456 return;
1457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1461#if defined(MBEDTLS_SHA256_C)
1462void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001463{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001464#if defined(MBEDTLS_USE_PSA_CRYPTO)
1465 size_t hash_size;
1466 psa_status_t status;
1467 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1468
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1470 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1471 if( status != PSA_SUCCESS )
1472 {
1473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1474 return;
1475 }
1476
1477 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1478 if( status != PSA_SUCCESS )
1479 {
1480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1481 return;
1482 }
1483 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1485#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_init( &sha256 );
1489
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001493 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001497
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001498 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001499#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001500 return;
1501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SHA512_C)
1505void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001506{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001507#if defined(MBEDTLS_USE_PSA_CRYPTO)
1508 size_t hash_size;
1509 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001510 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001511
1512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001513 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001514 if( status != PSA_SUCCESS )
1515 {
1516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1517 return;
1518 }
1519
Andrzej Kurek972fba52019-01-30 03:29:12 -05001520 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001521 if( status != PSA_SUCCESS )
1522 {
1523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1524 return;
1525 }
1526 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1528#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001529 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001530
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001531 mbedtls_sha512_init( &sha512 );
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001534
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001535 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001536 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001541 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001542#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 return;
1544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#endif /* MBEDTLS_SHA512_C */
1546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1549int 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 +02001550{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001551 unsigned char *p = ssl->handshake->premaster;
1552 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001553 const unsigned char *psk = ssl->conf->psk;
1554 size_t psk_len = ssl->conf->psk_len;
1555
1556 /* If the psk callback was called, use its result */
1557 if( ssl->handshake->psk != NULL )
1558 {
1559 psk = ssl->handshake->psk;
1560 psk_len = ssl->handshake->psk_len;
1561 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001562
1563 /*
1564 * PMS = struct {
1565 * opaque other_secret<0..2^16-1>;
1566 * opaque psk<0..2^16-1>;
1567 * };
1568 * with "other_secret" depending on the particular key exchange
1569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001572 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001573 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001575
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001576 *(p++) = (unsigned char)( psk_len >> 8 );
1577 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001578
1579 if( end < p || (size_t)( end - p ) < psk_len )
1580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1581
1582 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001583 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1587#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1588 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001589 {
1590 /*
1591 * other_secret already set by the ClientKeyExchange message,
1592 * and is 48 bytes long
1593 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001594 if( end - p < 2 )
1595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1596
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001597 *p++ = 0;
1598 *p++ = 48;
1599 p += 48;
1600 }
1601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1603#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1604 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001606 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001607 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001609 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001611 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001612 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001615 return( ret );
1616 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001617 *(p++) = (unsigned char)( len >> 8 );
1618 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 p += len;
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001622 }
1623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1625#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1626 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001628 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 size_t zlen;
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001632 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001633 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001636 return( ret );
1637 }
1638
1639 *(p++) = (unsigned char)( zlen >> 8 );
1640 *(p++) = (unsigned char)( zlen );
1641 p += zlen;
1642
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001643 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1644 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 }
1646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001651 }
1652
1653 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001654 if( end - p < 2 )
1655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001656
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001657 *(p++) = (unsigned char)( psk_len >> 8 );
1658 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001659
1660 if( end < p || (size_t)( end - p ) < psk_len )
1661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1662
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001663 memcpy( p, psk, psk_len );
1664 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001665
1666 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1667
1668 return( 0 );
1669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673/*
1674 * SSLv3.0 MAC functions
1675 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001676#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001677static void ssl_mac( mbedtls_md_context_t *md_ctx,
1678 const unsigned char *secret,
1679 const unsigned char *buf, size_t len,
1680 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001681 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682{
1683 unsigned char header[11];
1684 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001685 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1687 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001688
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001689 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001691 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001692 else
Paul Bakker68884e32013-01-07 18:20:04 +01001693 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
1695 memcpy( header, ctr, 8 );
1696 header[ 8] = (unsigned char) type;
1697 header[ 9] = (unsigned char)( len >> 8 );
1698 header[10] = (unsigned char)( len );
1699
Paul Bakker68884e32013-01-07 18:20:04 +01001700 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_md_starts( md_ctx );
1702 mbedtls_md_update( md_ctx, secret, md_size );
1703 mbedtls_md_update( md_ctx, padding, padlen );
1704 mbedtls_md_update( md_ctx, header, 11 );
1705 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001706 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Paul Bakker68884e32013-01-07 18:20:04 +01001708 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_md_starts( md_ctx );
1710 mbedtls_md_update( md_ctx, secret, md_size );
1711 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001712 mbedtls_md_update( md_ctx, out, md_size );
1713 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001716
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001717/* The function below is only used in the Lucky 13 counter-measure in
1718 * ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001719#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001720 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1721 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1722 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1723/* This function makes sure every byte in the memory region is accessed
1724 * (in ascending addresses order) */
1725static void ssl_read_memory( unsigned char *p, size_t len )
1726{
1727 unsigned char acc = 0;
1728 volatile unsigned char force;
1729
1730 for( ; len != 0; p++, len-- )
1731 acc ^= *p;
1732
1733 force = acc;
1734 (void) force;
1735}
1736#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1737
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001738/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001740 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001741
1742static void ssl_extract_add_data_from_record( unsigned char* add_data,
1743 mbedtls_record *rec )
1744{
1745 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1746 add_data[8] = rec->type;
1747 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1748 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1749 add_data[12] = rec->data_len & 0xFF;
1750}
1751
1752static int ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1753 mbedtls_ssl_transform *transform,
1754 mbedtls_record *rec,
1755 int (*f_rng)(void *, unsigned char *, size_t),
1756 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001759 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001760 unsigned char * data;
1761 unsigned char add_data[13];
1762 size_t post_avail;
1763
1764 /* The SSL context is only used for debugging purposes! */
1765#if !defined(MBEDTLS_SSL_DEBUG_C)
1766 ((void) ssl);
1767#endif
1768
1769 /* The PRNG is used for dynamic IV generation that's used
1770 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1771#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1772 ( defined(MBEDTLS_AES_C) || \
1773 defined(MBEDTLS_ARIA_C) || \
1774 defined(MBEDTLS_CAMELLIA_C) ) && \
1775 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1776 ((void) f_rng);
1777 ((void) p_rng);
1778#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001782 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001783 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1786 }
1787 if( rec == NULL ||
1788 rec->buf == NULL ||
1789 rec->buf_len < rec->data_offset ||
1790 rec->buf_len - rec->data_offset < rec->data_len )
1791 {
1792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794 }
1795
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001796 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1797 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001799 data, rec->data_len );
1800
1801 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1802
1803 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1804 {
1805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1806 (unsigned) rec->data_len,
1807 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1809 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001810
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001812 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 */
Hanno Becker52344c22018-01-03 15:24:20 +00001814#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 if( mode == MBEDTLS_MODE_STREAM ||
1816 ( mode == MBEDTLS_MODE_CBC
1817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001818 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001819#endif
1820 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001822 if( post_avail < transform->maclen )
1823 {
1824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1825 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1826 }
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001829 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001831 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001832 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1833 data, rec->data_len, rec->ctr, rec->type, mac );
1834 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 }
1836 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1839 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001840 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 {
Hanno Becker992b6872017-11-09 18:57:39 +00001842 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1843
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001844 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001845
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001846 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1847 sizeof( add_data ) );
1848 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1849 data, rec->data_len );
1850 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1851 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1852
1853 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 }
1855 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001856#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001860 }
1861
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001862 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1863 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001864
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001865 rec->data_len += transform->maclen;
1866 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001867 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001868 }
Hanno Becker52344c22018-01-03 15:24:20 +00001869#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001871 /*
1872 * Encrypt
1873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1875 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001877 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001878 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001880 "including %d bytes of padding",
1881 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001883 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1884 transform->iv_enc, transform->ivlen,
1885 data, rec->data_len,
1886 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001889 return( ret );
1890 }
1891
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001892 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 }
Paul Bakker68884e32013-01-07 18:20:04 +01001898 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001900
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001901#if defined(MBEDTLS_GCM_C) || \
1902 defined(MBEDTLS_CCM_C) || \
1903 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 mode == MBEDTLS_MODE_CCM ||
1906 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001907 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001908 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001909 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001910 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001911
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001912 /* Check that there's space for both the authentication tag
1913 * and the explicit IV before and after the record content. */
1914 if( post_avail < transform->taglen ||
1915 rec->data_offset < explicit_iv_len )
1916 {
1917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1918 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1919 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001920
Paul Bakker68884e32013-01-07 18:20:04 +01001921 /*
1922 * Generate IV
1923 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001924 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1925 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001926 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001928 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1929 explicit_iv_len );
1930 /* Prefix record content with explicit IV. */
1931 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001932 }
1933 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1934 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001935 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001936 unsigned char i;
1937
1938 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1939
1940 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001941 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001942 }
1943 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001944 {
1945 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001948 }
1949
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001950 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1951 iv, transform->ivlen );
1952 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001953 data - explicit_iv_len, explicit_iv_len );
1954 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1955 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001957 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001958 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001959
Paul Bakker68884e32013-01-07 18:20:04 +01001960 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001961 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001962 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001963
1964 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001965 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001966 iv, transform->ivlen,
1967 add_data, 13, /* add data */
1968 data, rec->data_len, /* source */
1969 data, &rec->data_len, /* destination */
1970 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001973 return( ret );
1974 }
1975
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001976 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1977 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001978
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001979 rec->data_len += transform->taglen + explicit_iv_len;
1980 rec->data_offset -= explicit_iv_len;
1981 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001982 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1986#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001987 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001990 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001991 size_t padlen, i;
1992 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001993
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001994 /* Currently we're always using minimal padding
1995 * (up to 255 bytes would be allowed). */
1996 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1997 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 padlen = 0;
1999
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002000 /* Check there's enough space in the buffer for the padding. */
2001 if( post_avail < padlen + 1 )
2002 {
2003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2004 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2005 }
2006
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002008 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002010 rec->data_len += padlen + 1;
2011 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002014 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002015 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2016 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002017 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002018 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002019 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002020 if( f_rng == NULL )
2021 {
2022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2024 }
2025
2026 if( rec->data_offset < transform->ivlen )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2030 }
2031
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002032 /*
2033 * Generate IV
2034 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002035 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002036 if( ret != 0 )
2037 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002038
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002039 memcpy( data - transform->ivlen, transform->iv_enc,
2040 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002041
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002046 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002047 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002048 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002050 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2051 transform->iv_enc,
2052 transform->ivlen,
2053 data, rec->data_len,
2054 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002057 return( ret );
2058 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002059
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002060 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002064 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002067 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002068 {
2069 /*
2070 * Save IV in SSL3 and TLS1
2071 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2073 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002075 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002076#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002077 {
2078 data -= transform->ivlen;
2079 rec->data_offset -= transform->ivlen;
2080 rec->data_len += transform->ivlen;
2081 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002084 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002085 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002086 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2087
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002088 /*
2089 * MAC(MAC_write_key, seq_num +
2090 * TLSCipherText.type +
2091 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002092 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002093 * IV + // except for TLS 1.0
2094 * ENC(content + padding + padding_length));
2095 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002096
2097 if( post_avail < transform->maclen)
2098 {
2099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2100 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2101 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2105 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002106
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002107 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002108
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2110 sizeof( add_data ) );
2111 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2112 data, rec->data_len );
2113 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2114 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002115
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002116 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002117
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118 rec->data_len += transform->maclen;
2119 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002120 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002124 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002126 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002132 /* Make extra sure authentication was performed, exactly once */
2133 if( auth_done != 1 )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002137 }
2138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
2141 return( 0 );
2142}
2143
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002144static int ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2145 mbedtls_ssl_transform *transform,
2146 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002148 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002150 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002151#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002152 size_t padlen = 0, correct = 1;
2153#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002154 unsigned char* data;
2155 unsigned char add_data[13];
2156
2157#if !defined(MBEDTLS_SSL_DEBUG_C)
2158 ((void) ssl);
2159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002162 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002163 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2166 }
2167 if( rec == NULL ||
2168 rec->buf == NULL ||
2169 rec->buf_len < rec->data_offset ||
2170 rec->buf_len - rec->data_offset < rec->data_len )
2171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002174 }
2175
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002176 data = rec->buf + rec->data_offset;
2177 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2180 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002181 {
2182 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002183 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2184 transform->iv_dec,
2185 transform->ivlen,
2186 data, rec->data_len,
2187 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002190 return( ret );
2191 }
2192
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002193 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
Paul Bakker68884e32013-01-07 18:20:04 +01002199 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002201#if defined(MBEDTLS_GCM_C) || \
2202 defined(MBEDTLS_CCM_C) || \
2203 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 mode == MBEDTLS_MODE_CCM ||
2206 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002207 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002208 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002209 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002210
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002211 /*
2212 * Compute and update sizes
2213 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002214 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002217 "+ taglen (%d)", rec->data_len,
2218 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002220 }
Paul Bakker68884e32013-01-07 18:20:04 +01002221
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002222 /*
2223 * Prepare IV
2224 */
2225 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2226 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002227 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002228 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002229 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002230
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002231 }
2232 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2233 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002234 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002235 unsigned char i;
2236
2237 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2238
2239 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002240 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002241 }
2242 else
2243 {
2244 /* Reminder if we ever add an AEAD mode with a different size */
2245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2247 }
2248
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002249 data += explicit_iv_len;
2250 rec->data_offset += explicit_iv_len;
2251 rec->data_len -= explicit_iv_len + transform->taglen;
2252
2253 ssl_extract_add_data_from_record( add_data, rec );
2254 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2255 add_data, 13 );
2256
2257 memcpy( transform->iv_dec + transform->fixed_ivlen,
2258 data - explicit_iv_len, explicit_iv_len );
2259
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002260 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002261 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002262 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002263
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002264
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002265 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002266 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002267 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002268 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2269 iv, transform->ivlen,
2270 add_data, 13,
2271 data, rec->data_len,
2272 data, &olen,
2273 data + rec->data_len,
2274 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2279 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002280
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002281 return( ret );
2282 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002283 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002284
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002285 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002289 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2293#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002294 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002297 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002298
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 /*
Paul Bakker45829992013-01-03 14:52:21 +01002300 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002303 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2304 {
2305 /* The ciphertext is prefixed with the CBC IV. */
2306 minlen += transform->ivlen;
2307 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002308#endif
Paul Bakker45829992013-01-03 14:52:21 +01002309
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002310 /* Size considerations:
2311 *
2312 * - The CBC cipher text must not be empty and hence
2313 * at least of size transform->ivlen.
2314 *
2315 * Together with the potential IV-prefix, this explains
2316 * the first of the two checks below.
2317 *
2318 * - The record must contain a MAC, either in plain or
2319 * encrypted, depending on whether Encrypt-then-MAC
2320 * is used or not.
2321 * - If it is, the message contains the IV-prefix,
2322 * the CBC ciphertext, and the MAC.
2323 * - If it is not, the padded plaintext, and hence
2324 * the CBC ciphertext, has at least length maclen + 1
2325 * because there is at least the padding length byte.
2326 *
2327 * As the CBC ciphertext is not empty, both cases give the
2328 * lower bound minlen + maclen + 1 on the record size, which
2329 * we test for in the second check below.
2330 */
2331 if( rec->data_len < minlen + transform->ivlen ||
2332 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002335 "+ 1 ) ( + expl IV )", rec->data_len,
2336 transform->ivlen,
2337 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002339 }
2340
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002341 /*
2342 * Authenticate before decrypt if enabled
2343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002345 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002346 {
Hanno Becker992b6872017-11-09 18:57:39 +00002347 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002350
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002351 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2352 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002353
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002354 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002355
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002356 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2357 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2358 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2359 data, rec->data_len );
2360 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2361 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002362
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002363 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2364 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002365 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002366 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002367
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002368 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2369 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002373 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002374 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002377
2378 /*
2379 * Check length sanity
2380 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002384 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002386 }
2387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002390 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002392 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002393 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002394 /* This is safe because data_len >= minlen + maclen + 1 initially,
2395 * and at this point we have at most subtracted maclen (note that
2396 * minlen == transform->ivlen here). */
2397 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002399 data += transform->ivlen;
2400 rec->data_offset += transform->ivlen;
2401 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002404
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002405 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2406 transform->iv_dec, transform->ivlen,
2407 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002410 return( ret );
2411 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002412
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002413 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002417 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002420 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002421 {
2422 /*
2423 * Save IV in SSL3 and TLS1
2424 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002425 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2426 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002430 /* Safe since data_len >= minlen + maclen + 1, so after having
2431 * subtracted at most minlen and maclen up to this point,
2432 * data_len > 0. */
2433 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002434
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002435 if( auth_done == 1 )
2436 {
2437 correct *= ( rec->data_len >= padlen + 1 );
2438 padlen *= ( rec->data_len >= padlen + 1 );
2439 }
2440 else
Paul Bakker45829992013-01-03 14:52:21 +01002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002443 if( rec->data_len < transform->maclen + padlen + 1 )
2444 {
2445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2446 rec->data_len,
2447 transform->maclen,
2448 padlen + 1 ) );
2449 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002450#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002451
2452 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2453 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002456 padlen++;
2457
2458 /* Regardless of the validity of the padding,
2459 * we have data_len >= padlen here. */
2460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002462 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002464 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_SSL_DEBUG_ALL)
2467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002468 "should be no more than %d",
2469 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002470#endif
Paul Bakker45829992013-01-03 14:52:21 +01002471 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 }
2473 }
2474 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2476#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2477 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002478 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002480 /* The padding check involves a series of up to 256
2481 * consecutive memory reads at the end of the record
2482 * plaintext buffer. In order to hide the length and
2483 * validity of the padding, always perform exactly
2484 * `min(256,plaintext_len)` reads (but take into account
2485 * only the last `padlen` bytes for the padding check). */
2486 size_t pad_count = 0;
2487 size_t real_count = 0;
2488 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002489
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002490 /* Index of first padding byte; it has been ensured above
2491 * that the subtraction is safe. */
2492 size_t const padding_idx = rec->data_len - padlen;
2493 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2494 size_t const start_idx = rec->data_len - num_checks;
2495 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002496
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002497 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002498 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002499 real_count |= ( idx >= padding_idx );
2500 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002501 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002502 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002505 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002507#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002508 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002510 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2512 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002516 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002518 /* If the padding was found to be invalid, padlen == 0
2519 * and the subtraction is safe. If the padding was found valid,
2520 * padlen hasn't been changed and the previous assertion
2521 * data_len >= padlen still holds. */
2522 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002526 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002532#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002534 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002535#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002538 * Authenticate if not done yet.
2539 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 */
Hanno Becker52344c22018-01-03 15:24:20 +00002541#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002542 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002543 {
Hanno Becker992b6872017-11-09 18:57:39 +00002544 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 /* If the initial value of padlen was such that
2547 * data_len < maclen + padlen + 1, then padlen
2548 * got reset to 1, and the initial check
2549 * data_len >= minlen + maclen + 1
2550 * guarantees that at this point we still
2551 * have at least data_len >= maclen.
2552 *
2553 * If the initial value of padlen was such that
2554 * data_len >= maclen + padlen + 1, then we have
2555 * subtracted either padlen + 1 (if the padding was correct)
2556 * or 0 (if the padding was incorrect) since then,
2557 * hence data_len >= maclen in any case.
2558 */
2559 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002565 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002566 ssl_mac( &transform->md_ctx_dec,
2567 transform->mac_dec,
2568 data, rec->data_len,
2569 rec->ctr, rec->type,
2570 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 }
2572 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2575 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002577 {
2578 /*
2579 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002580 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002581 *
2582 * Known timing attacks:
2583 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2584 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002585 * To compensate for different timings for the MAC calculation
2586 * depending on how much padding was removed (which is determined
2587 * by padlen), process extra_run more blocks through the hash
2588 * function.
2589 *
2590 * The formula in the paper is
2591 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2592 * where L1 is the size of the header plus the decrypted message
2593 * plus CBC padding and L2 is the size of the header plus the
2594 * decrypted message. This is for an underlying hash function
2595 * with 64-byte blocks.
2596 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2597 * correctly. We round down instead of up, so -56 is the correct
2598 * value for our calculations instead of -55.
2599 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002600 * Repeat the formula rather than defining a block_size variable.
2601 * This avoids requiring division by a variable at runtime
2602 * (which would be marginally less efficient and would require
2603 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002604 */
2605 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002606 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002607
2608 /*
2609 * The next two sizes are the minimum and maximum values of
2610 * in_msglen over all padlen values.
2611 *
2612 * They're independent of padlen, since we previously did
2613 * in_msglen -= padlen.
2614 *
2615 * Note that max_len + maclen is never more than the buffer
2616 * length, as we previously did in_msglen -= maclen too.
2617 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002619 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2620
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002621 memset( tmp, 0, sizeof( tmp ) );
2622
2623 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002624 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002625#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2626 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002627 case MBEDTLS_MD_MD5:
2628 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002629 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002630 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2632 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002633 break;
2634#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002635#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002636 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002637 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2639 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002640 break;
2641#endif
2642 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2645 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002647 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002648
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2650 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2651 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002652 /* Make sure we access everything even when padlen > 0. This
2653 * makes the synchronisation requirements for just-in-time
2654 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655 ssl_read_memory( data + rec->data_len, padlen );
2656 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002657
2658 /* Call mbedtls_md_process at least once due to cache attacks
2659 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002660 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002662
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002663 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002664
2665 /* Make sure we access all the memory that could contain the MAC,
2666 * before we check it in the next code block. This makes the
2667 * synchronisation requirements for just-in-time Prime+Probe
2668 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 ssl_read_memory( data + min_len,
2670 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002671 }
2672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2674 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002680#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2682 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2686 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#if defined(MBEDTLS_SSL_DEBUG_ALL)
2689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002690#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 correct = 0;
2692 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002693 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002694 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002695
2696 /*
2697 * Finally check the correct flag
2698 */
2699 if( correct == 0 )
2700 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002701#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002702
2703 /* Make extra sure authentication was performed, exactly once */
2704 if( auth_done != 1 )
2705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
2712 return( 0 );
2713}
2714
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002715#undef MAC_NONE
2716#undef MAC_PLAINTEXT
2717#undef MAC_CIPHERTEXT
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720/*
2721 * Compression/decompression functions
2722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724{
2725 int ret;
2726 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002727 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002729 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002733 if( len_pre == 0 )
2734 return( 0 );
2735
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736 memcpy( msg_pre, ssl->out_msg, len_pre );
2737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002739 ssl->out_msglen ) );
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002742 ssl->out_msg, ssl->out_msglen );
2743
Paul Bakker48916f92012-09-16 19:57:18 +00002744 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2745 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2746 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002747 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748
Paul Bakker48916f92012-09-16 19:57:18 +00002749 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002750 if( ret != Z_OK )
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2753 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002754 }
2755
Angus Grattond8213d02016-05-25 20:56:48 +10002756 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002757 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002760 ssl->out_msglen ) );
2761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002763 ssl->out_msg, ssl->out_msglen );
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766
2767 return( 0 );
2768}
2769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771{
2772 int ret;
2773 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002774 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002776 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002780 if( len_pre == 0 )
2781 return( 0 );
2782
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783 memcpy( msg_pre, ssl->in_msg, len_pre );
2784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002786 ssl->in_msglen ) );
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789 ssl->in_msg, ssl->in_msglen );
2790
Paul Bakker48916f92012-09-16 19:57:18 +00002791 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2792 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2793 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002794 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002795 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002796
Paul Bakker48916f92012-09-16 19:57:18 +00002797 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002798 if( ret != Z_OK )
2799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2801 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802 }
2803
Angus Grattond8213d02016-05-25 20:56:48 +10002804 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002805 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002808 ssl->in_msglen ) );
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002811 ssl->in_msg, ssl->in_msglen );
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814
2815 return( 0 );
2816}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2820static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_SSL_PROTO_DTLS)
2823static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002824{
2825 /* If renegotiation is not enforced, retransmit until we would reach max
2826 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002827 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002828 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002829 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002830 unsigned char doublings = 1;
2831
2832 while( ratio != 0 )
2833 {
2834 ++doublings;
2835 ratio >>= 1;
2836 }
2837
2838 if( ++ssl->renego_records_seen > doublings )
2839 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002841 return( 0 );
2842 }
2843 }
2844
2845 return( ssl_write_hello_request( ssl ) );
2846}
2847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002849
Paul Bakker5121ce52009-01-03 21:22:43 +00002850/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002851 * Fill the input message buffer by appending data to it.
2852 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002853 *
2854 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2855 * available (from this read and/or a previous one). Otherwise, an error code
2856 * is returned (possibly EOF or WANT_READ).
2857 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002858 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2859 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2860 * since we always read a whole datagram at once.
2861 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002862 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002863 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866{
Paul Bakker23986e52011-04-24 08:57:21 +00002867 int ret;
2868 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002872 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002875 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002877 }
2878
Angus Grattond8213d02016-05-25 20:56:48 +10002879 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002883 }
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002888 uint32_t timeout;
2889
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002890 /* Just to be sure */
2891 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2892 {
2893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2894 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2896 }
2897
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002898 /*
2899 * The point is, we need to always read a full datagram at once, so we
2900 * sometimes read more then requested, and handle the additional data.
2901 * It could be the rest of the current record (while fetching the
2902 * header) and/or some other records in the same datagram.
2903 */
2904
2905 /*
2906 * Move to the next record in the already read datagram if applicable
2907 */
2908 if( ssl->next_record_offset != 0 )
2909 {
2910 if( ssl->in_left < ssl->next_record_offset )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002914 }
2915
2916 ssl->in_left -= ssl->next_record_offset;
2917
2918 if( ssl->in_left != 0 )
2919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002921 ssl->next_record_offset ) );
2922 memmove( ssl->in_hdr,
2923 ssl->in_hdr + ssl->next_record_offset,
2924 ssl->in_left );
2925 }
2926
2927 ssl->next_record_offset = 0;
2928 }
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002932
2933 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002934 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002935 */
2936 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002939 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002940 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002941
2942 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002943 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002944 * are not at the beginning of a new record, the caller did something
2945 * wrong.
2946 */
2947 if( ssl->in_left != 0 )
2948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002951 }
2952
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002953 /*
2954 * Don't even try to read if time's out already.
2955 * This avoids by-passing the timer when repeatedly receiving messages
2956 * that will end up being dropped.
2957 */
2958 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002959 {
2960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002961 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002962 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002963 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002964 {
Angus Grattond8213d02016-05-25 20:56:48 +10002965 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002968 timeout = ssl->handshake->retransmit_timeout;
2969 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002970 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002973
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002974 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002975 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2976 timeout );
2977 else
2978 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002981
2982 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002984 }
2985
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002986 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002989 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002992 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002993 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002996 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002997 }
2998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003002 return( ret );
3003 }
3004
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003005 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003008 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003011 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003014 return( ret );
3015 }
3016
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003017 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003018 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003020 }
3021
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 if( ret < 0 )
3023 return( ret );
3024
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003025 ssl->in_left = ret;
3026 }
3027 else
3028#endif
3029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003031 ssl->in_left, nb_want ) );
3032
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003033 while( ssl->in_left < nb_want )
3034 {
3035 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003036
3037 if( ssl_check_timer( ssl ) != 0 )
3038 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3039 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003040 {
3041 if( ssl->f_recv_timeout != NULL )
3042 {
3043 ret = ssl->f_recv_timeout( ssl->p_bio,
3044 ssl->in_hdr + ssl->in_left, len,
3045 ssl->conf->read_timeout );
3046 }
3047 else
3048 {
3049 ret = ssl->f_recv( ssl->p_bio,
3050 ssl->in_hdr + ssl->in_left, len );
3051 }
3052 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003055 ssl->in_left, nb_want ) );
3056 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003057
3058 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003060
3061 if( ret < 0 )
3062 return( ret );
3063
mohammad160352aecb92018-03-28 23:41:40 -07003064 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003065 {
Darryl Green11999bb2018-03-13 15:22:58 +00003066 MBEDTLS_SSL_DEBUG_MSG( 1,
3067 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003068 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3070 }
3071
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003072 ssl->in_left += ret;
3073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 }
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
3078 return( 0 );
3079}
3080
3081/*
3082 * Flush any data not yet written
3083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003085{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003086 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003087 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003091 if( ssl->f_send == NULL )
3092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003094 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003096 }
3097
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003098 /* Avoid incrementing counter if data is flushed */
3099 if( ssl->out_left == 0 )
3100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003102 return( 0 );
3103 }
3104
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 while( ssl->out_left > 0 )
3106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3108 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Hanno Becker2b1e3542018-08-06 11:19:13 +01003110 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003111 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
3115 if( ret <= 0 )
3116 return( ret );
3117
mohammad160352aecb92018-03-28 23:41:40 -07003118 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003119 {
Darryl Green11999bb2018-03-13 15:22:58 +00003120 MBEDTLS_SSL_DEBUG_MSG( 1,
3121 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003122 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3124 }
3125
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 ssl->out_left -= ret;
3127 }
3128
Hanno Becker2b1e3542018-08-06 11:19:13 +01003129#if defined(MBEDTLS_SSL_PROTO_DTLS)
3130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003131 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003132 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003133 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003134 else
3135#endif
3136 {
3137 ssl->out_hdr = ssl->out_buf + 8;
3138 }
3139 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
3143 return( 0 );
3144}
3145
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003146/*
3147 * Functions to handle the DTLS retransmission state machine
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150/*
3151 * Append current handshake message to current outgoing flight
3152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3157 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3158 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003159
3160 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003161 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003165 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166 }
3167
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003168 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003172 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173 }
3174
3175 /* Copy current handshake message with headers */
3176 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3177 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003178 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179 msg->next = NULL;
3180
3181 /* Append to the current flight */
3182 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003183 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184 else
3185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003187 while( cur->next != NULL )
3188 cur = cur->next;
3189 cur->next = msg;
3190 }
3191
Hanno Becker3b235902018-08-06 09:54:53 +01003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003193 return( 0 );
3194}
3195
3196/*
3197 * Free the current flight of handshake messages
3198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 mbedtls_ssl_flight_item *cur = flight;
3202 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003203
3204 while( cur != NULL )
3205 {
3206 next = cur->next;
3207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 mbedtls_free( cur->p );
3209 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003210
3211 cur = next;
3212 }
3213}
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3216static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003217#endif
3218
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003219/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003220 * Swap transform_out and out_ctr with the alternative ones
3221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003225 unsigned char tmp_out_ctr[8];
3226
3227 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003230 return;
3231 }
3232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003234
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003235 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003236 tmp_transform = ssl->transform_out;
3237 ssl->transform_out = ssl->handshake->alt_transform_out;
3238 ssl->handshake->alt_transform_out = tmp_transform;
3239
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003240 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003241 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3242 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003243 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003244
3245 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003246 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3249 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3254 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003255 }
3256 }
3257#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003258}
3259
3260/*
3261 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003262 */
3263int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3264{
3265 int ret = 0;
3266
3267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3268
3269 ret = mbedtls_ssl_flight_transmit( ssl );
3270
3271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3272
3273 return( ret );
3274}
3275
3276/*
3277 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003278 *
3279 * Need to remember the current message in case flush_output returns
3280 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003281 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003282 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003283int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003284{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003285 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003289 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003291
3292 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003293 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294 ssl_swap_epochs( ssl );
3295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003297 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003298
3299 while( ssl->handshake->cur_msg != NULL )
3300 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003301 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003302 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003303
Hanno Beckere1dcb032018-08-17 16:47:58 +01003304 int const is_finished =
3305 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3306 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3307
Hanno Becker04da1892018-08-14 13:22:10 +01003308 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3309 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3310
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003311 /* Swap epochs before sending Finished: we can't do it after
3312 * sending ChangeCipherSpec, in case write returns WANT_READ.
3313 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003314 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003315 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003317 ssl_swap_epochs( ssl );
3318 }
3319
Hanno Becker67bc7c32018-08-06 11:33:50 +01003320 ret = ssl_get_remaining_payload_in_datagram( ssl );
3321 if( ret < 0 )
3322 return( ret );
3323 max_frag_len = (size_t) ret;
3324
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003325 /* CCS is copied as is, while HS messages may need fragmentation */
3326 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3327 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003328 if( max_frag_len == 0 )
3329 {
3330 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3331 return( ret );
3332
3333 continue;
3334 }
3335
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003336 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003337 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003338 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003340 /* Update position inside current message */
3341 ssl->handshake->cur_msg_p += cur->len;
3342 }
3343 else
3344 {
3345 const unsigned char * const p = ssl->handshake->cur_msg_p;
3346 const size_t hs_len = cur->len - 12;
3347 const size_t frag_off = p - ( cur->p + 12 );
3348 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003349 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350
Hanno Beckere1dcb032018-08-17 16:47:58 +01003351 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003352 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003353 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003354 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003355
Hanno Becker67bc7c32018-08-06 11:33:50 +01003356 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3357 return( ret );
3358
3359 continue;
3360 }
3361 max_hs_frag_len = max_frag_len - 12;
3362
3363 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3364 max_hs_frag_len : rem_len;
3365
3366 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003367 {
3368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003369 (unsigned) cur_hs_frag_len,
3370 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003371 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003372
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003373 /* Messages are stored with handshake headers as if not fragmented,
3374 * copy beginning of headers then fill fragmentation fields.
3375 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3376 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003377
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003378 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3379 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3380 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3381
Hanno Becker67bc7c32018-08-06 11:33:50 +01003382 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3383 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3384 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003385
3386 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3387
Hanno Becker3f7b9732018-08-28 09:53:25 +01003388 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003389 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3390 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003391 ssl->out_msgtype = cur->type;
3392
3393 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003394 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003395 }
3396
3397 /* If done with the current message move to the next one if any */
3398 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3399 {
3400 if( cur->next != NULL )
3401 {
3402 ssl->handshake->cur_msg = cur->next;
3403 ssl->handshake->cur_msg_p = cur->next->p + 12;
3404 }
3405 else
3406 {
3407 ssl->handshake->cur_msg = NULL;
3408 ssl->handshake->cur_msg_p = NULL;
3409 }
3410 }
3411
3412 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003413 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003416 return( ret );
3417 }
3418 }
3419
Hanno Becker67bc7c32018-08-06 11:33:50 +01003420 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3421 return( ret );
3422
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003423 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3425 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003426 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003429 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3430 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003431
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433
3434 return( 0 );
3435}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436
3437/*
3438 * To be called when the last message of an incoming flight is received.
3439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003441{
3442 /* We won't need to resend that one any more */
3443 ssl_flight_free( ssl->handshake->flight );
3444 ssl->handshake->flight = NULL;
3445 ssl->handshake->cur_msg = NULL;
3446
3447 /* The next incoming flight will start with this msg_seq */
3448 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3449
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003450 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003451 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003452
Hanno Becker0271f962018-08-16 13:23:47 +01003453 /* Clear future message buffering structure. */
3454 ssl_buffering_free( ssl );
3455
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003456 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003457 ssl_set_timer( ssl, 0 );
3458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3460 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003463 }
3464 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003466}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003467
3468/*
3469 * To be called when the last message of an outgoing flight is send.
3470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003472{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003473 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003474 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3477 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003480 }
3481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003485
Paul Bakker5121ce52009-01-03 21:22:43 +00003486/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003487 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003489
3490/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003491 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003492 *
3493 * - fill in handshake headers
3494 * - update handshake checksum
3495 * - DTLS: save message for resending
3496 * - then pass to the record layer
3497 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003498 * DTLS: except for HelloRequest, messages are only queued, and will only be
3499 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003500 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003501 * Inputs:
3502 * - ssl->out_msglen: 4 + actual handshake message len
3503 * (4 is the size of handshake headers for TLS)
3504 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3505 * - ssl->out_msg + 4: the handshake message body
3506 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003507 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003508 * - ssl->out_msglen: the length of the record contents
3509 * (including handshake headers but excluding record headers)
3510 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003511 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003512int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003513{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003514 int ret;
3515 const size_t hs_len = ssl->out_msglen - 4;
3516 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3519
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003520 /*
3521 * Sanity checks
3522 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003523 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003524 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3525 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003526 /* In SSLv3, the client might send a NoCertificate alert. */
3527#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3528 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3529 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3530 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3531#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3535 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003536 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003538 /* Whenever we send anything different from a
3539 * HelloRequest we should be in a handshake - double check. */
3540 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3541 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003542 ssl->handshake == NULL )
3543 {
3544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003552 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003555 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003556#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003557
Hanno Beckerb50a2532018-08-06 11:52:54 +01003558 /* Double-check that we did not exceed the bounds
3559 * of the outgoing record buffer.
3560 * This should never fail as the various message
3561 * writing functions must obey the bounds of the
3562 * outgoing record buffer, but better be safe.
3563 *
3564 * Note: We deliberately do not check for the MTU or MFL here.
3565 */
3566 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3567 {
3568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3569 "size %u, maximum %u",
3570 (unsigned) ssl->out_msglen,
3571 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3573 }
3574
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003575 /*
3576 * Fill handshake headers
3577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003580 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3581 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3582 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003584 /*
3585 * DTLS has additional fields in the Handshake layer,
3586 * between the length field and the actual payload:
3587 * uint16 message_seq;
3588 * uint24 fragment_offset;
3589 * uint24 fragment_length;
3590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003593 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003594 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003595 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003596 {
3597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3598 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003599 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003600 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3602 }
3603
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003604 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003605 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003606
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003607 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003608 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003609 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003610 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3611 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3612 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003613 }
3614 else
3615 {
3616 ssl->out_msg[4] = 0;
3617 ssl->out_msg[5] = 0;
3618 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003619
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003620 /* Handshake hashes are computed without fragmentation,
3621 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003622 memset( ssl->out_msg + 6, 0x00, 3 );
3623 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003626
Hanno Becker0207e532018-08-28 10:28:28 +01003627 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003628 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3629 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 }
3631
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003632 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003635 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3636 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637 {
3638 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641 return( ret );
3642 }
3643 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003644 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003645#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003646 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003647 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003648 {
3649 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3650 return( ret );
3651 }
3652 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003653
3654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3655
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003656 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657}
3658
3659/*
3660 * Record layer functions
3661 */
3662
3663/*
3664 * Write current record.
3665 *
3666 * Uses:
3667 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3668 * - ssl->out_msglen: length of the record content (excl headers)
3669 * - ssl->out_msg: record content
3670 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003671int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672{
3673 int ret, done = 0;
3674 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003675 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676
3677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003680 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003682 {
3683 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686 return( ret );
3687 }
3688
3689 len = ssl->out_msglen;
3690 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3694 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 ret = mbedtls_ssl_hw_record_write( ssl );
3699 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3702 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003704
3705 if( ret == 0 )
3706 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003709 if( !done )
3710 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003711 unsigned i;
3712 size_t protected_record_size;
3713
Paul Bakker05ef8352012-05-08 09:17:57 +00003714 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003716 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003717
Hanno Becker19859472018-08-06 09:40:20 +01003718 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003719 ssl->out_len[0] = (unsigned char)( len >> 8 );
3720 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003721
Paul Bakker48916f92012-09-16 19:57:18 +00003722 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003723 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003724 mbedtls_record rec;
3725
3726 rec.buf = ssl->out_iv;
3727 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3728 ( ssl->out_iv - ssl->out_buf );
3729 rec.data_len = ssl->out_msglen;
3730 rec.data_offset = ssl->out_msg - rec.buf;
3731
3732 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3733 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3734 ssl->conf->transport, rec.ver );
3735 rec.type = ssl->out_msgtype;
3736
3737 if( ( ret = ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
3738 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003741 return( ret );
3742 }
3743
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003744 if( rec.data_offset != 0 )
3745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3748 }
3749
3750 ssl->out_msglen = rec.data_len;
3751 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3752 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003753 }
3754
Hanno Becker2b1e3542018-08-06 11:19:13 +01003755 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3756
3757#if defined(MBEDTLS_SSL_PROTO_DTLS)
3758 /* In case of DTLS, double-check that we don't exceed
3759 * the remaining space in the datagram. */
3760 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3761 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003762 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003763 if( ret < 0 )
3764 return( ret );
3765
3766 if( protected_record_size > (size_t) ret )
3767 {
3768 /* Should never happen */
3769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3770 }
3771 }
3772#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003775 "version = [%d:%d], msglen = %d",
3776 ssl->out_hdr[0], ssl->out_hdr[1],
3777 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003780 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003781
3782 ssl->out_left += protected_record_size;
3783 ssl->out_hdr += protected_record_size;
3784 ssl_update_out_pointers( ssl, ssl->transform_out );
3785
Hanno Becker04484622018-08-06 09:49:38 +01003786 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3787 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3788 break;
3789
3790 /* The loop goes to its end iff the counter is wrapping */
3791 if( i == ssl_ep_len( ssl ) )
3792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3794 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 }
3797
Hanno Becker67bc7c32018-08-06 11:33:50 +01003798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003799 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3800 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003801 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003802 size_t remaining;
3803 ret = ssl_get_remaining_payload_in_datagram( ssl );
3804 if( ret < 0 )
3805 {
3806 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3807 ret );
3808 return( ret );
3809 }
3810
3811 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003813 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003815 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003816 else
3817 {
Hanno Becker513815a2018-08-20 11:56:09 +01003818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 }
3820 }
3821#endif /* MBEDTLS_SSL_PROTO_DTLS */
3822
3823 if( ( flush == SSL_FORCE_FLUSH ) &&
3824 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003827 return( ret );
3828 }
3829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003831
3832 return( 0 );
3833}
3834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003836
3837static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3838{
3839 if( ssl->in_msglen < ssl->in_hslen ||
3840 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3841 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3842 {
3843 return( 1 );
3844 }
3845 return( 0 );
3846}
Hanno Becker44650b72018-08-16 12:51:11 +01003847
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003848static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003849{
3850 return( ( ssl->in_msg[9] << 16 ) |
3851 ( ssl->in_msg[10] << 8 ) |
3852 ssl->in_msg[11] );
3853}
3854
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003855static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003856{
3857 return( ( ssl->in_msg[6] << 16 ) |
3858 ( ssl->in_msg[7] << 8 ) |
3859 ssl->in_msg[8] );
3860}
3861
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003862static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003863{
3864 uint32_t msg_len, frag_off, frag_len;
3865
3866 msg_len = ssl_get_hs_total_len( ssl );
3867 frag_off = ssl_get_hs_frag_off( ssl );
3868 frag_len = ssl_get_hs_frag_len( ssl );
3869
3870 if( frag_off > msg_len )
3871 return( -1 );
3872
3873 if( frag_len > msg_len - frag_off )
3874 return( -1 );
3875
3876 if( frag_len + 12 > ssl->in_msglen )
3877 return( -1 );
3878
3879 return( 0 );
3880}
3881
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003882/*
3883 * Mark bits in bitmask (used for DTLS HS reassembly)
3884 */
3885static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3886{
3887 unsigned int start_bits, end_bits;
3888
3889 start_bits = 8 - ( offset % 8 );
3890 if( start_bits != 8 )
3891 {
3892 size_t first_byte_idx = offset / 8;
3893
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003894 /* Special case */
3895 if( len <= start_bits )
3896 {
3897 for( ; len != 0; len-- )
3898 mask[first_byte_idx] |= 1 << ( start_bits - len );
3899
3900 /* Avoid potential issues with offset or len becoming invalid */
3901 return;
3902 }
3903
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003904 offset += start_bits; /* Now offset % 8 == 0 */
3905 len -= start_bits;
3906
3907 for( ; start_bits != 0; start_bits-- )
3908 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3909 }
3910
3911 end_bits = len % 8;
3912 if( end_bits != 0 )
3913 {
3914 size_t last_byte_idx = ( offset + len ) / 8;
3915
3916 len -= end_bits; /* Now len % 8 == 0 */
3917
3918 for( ; end_bits != 0; end_bits-- )
3919 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3920 }
3921
3922 memset( mask + offset / 8, 0xFF, len / 8 );
3923}
3924
3925/*
3926 * Check that bitmask is full
3927 */
3928static int ssl_bitmask_check( unsigned char *mask, size_t len )
3929{
3930 size_t i;
3931
3932 for( i = 0; i < len / 8; i++ )
3933 if( mask[i] != 0xFF )
3934 return( -1 );
3935
3936 for( i = 0; i < len % 8; i++ )
3937 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3938 return( -1 );
3939
3940 return( 0 );
3941}
3942
Hanno Becker56e205e2018-08-16 09:06:12 +01003943/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003944static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003945 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003946{
Hanno Becker56e205e2018-08-16 09:06:12 +01003947 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003948
Hanno Becker56e205e2018-08-16 09:06:12 +01003949 alloc_len = 12; /* Handshake header */
3950 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003951
Hanno Beckerd07df862018-08-16 09:14:58 +01003952 if( add_bitmap )
3953 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003954
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003955 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003956}
Hanno Becker56e205e2018-08-16 09:06:12 +01003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003959
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003960static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003961{
3962 return( ( ssl->in_msg[1] << 16 ) |
3963 ( ssl->in_msg[2] << 8 ) |
3964 ssl->in_msg[3] );
3965}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003966
Simon Butcher99000142016-10-13 17:21:01 +01003967int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003972 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003974 }
3975
Hanno Becker12555c62018-08-16 12:47:53 +01003976 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003979 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003980 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003984 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003985 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003986 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003987
Hanno Becker44650b72018-08-16 12:51:11 +01003988 if( ssl_check_hs_header( ssl ) != 0 )
3989 {
3990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3991 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3992 }
3993
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003994 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003995 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3996 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3997 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3998 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003999 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004000 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4001 {
4002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4003 recv_msg_seq,
4004 ssl->handshake->in_msg_seq ) );
4005 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4006 }
4007
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004008 /* Retransmit only on last message from previous flight, to avoid
4009 * too many retransmissions.
4010 * Besides, No sane server ever retransmits HelloVerifyRequest */
4011 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004015 "message_seq = %d, start_of_flight = %d",
4016 recv_msg_seq,
4017 ssl->handshake->in_flight_start_seq ) );
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004022 return( ret );
4023 }
4024 }
4025 else
4026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004028 "message_seq = %d, expected = %d",
4029 recv_msg_seq,
4030 ssl->handshake->in_msg_seq ) );
4031 }
4032
Hanno Becker90333da2017-10-10 11:27:13 +01004033 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004034 }
4035 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004036
Hanno Becker6d97ef52018-08-16 13:09:04 +01004037 /* Message reassembly is handled alongside buffering of future
4038 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004039 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004040 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004041 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004044 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004045 }
4046 }
4047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004049 /* With TLS we don't handle fragmentation (for now) */
4050 if( ssl->in_msglen < ssl->in_hslen )
4051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4053 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004054 }
4055
Simon Butcher99000142016-10-13 17:21:01 +01004056 return( 0 );
4057}
4058
4059void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4060{
Hanno Becker0271f962018-08-16 13:23:47 +01004061 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004062
Hanno Becker0271f962018-08-16 13:23:47 +01004063 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004064 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004065 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004066 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004067
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004068 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004070 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004071 ssl->handshake != NULL )
4072 {
Hanno Becker0271f962018-08-16 13:23:47 +01004073 unsigned offset;
4074 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004075
Hanno Becker0271f962018-08-16 13:23:47 +01004076 /* Increment handshake sequence number */
4077 hs->in_msg_seq++;
4078
4079 /*
4080 * Clear up handshake buffering and reassembly structure.
4081 */
4082
4083 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004084 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004085
4086 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004087 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4088 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004089 offset++, hs_buf++ )
4090 {
4091 *hs_buf = *(hs_buf + 1);
4092 }
4093
4094 /* Create a fresh last entry */
4095 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004096 }
4097#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004098}
4099
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004100/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004101 * DTLS anti-replay: RFC 6347 4.1.2.6
4102 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004103 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4104 * Bit n is set iff record number in_window_top - n has been seen.
4105 *
4106 * Usually, in_window_top is the last record number seen and the lsb of
4107 * in_window is set. The only exception is the initial state (record number 0
4108 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4111static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112{
4113 ssl->in_window_top = 0;
4114 ssl->in_window = 0;
4115}
4116
4117static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4118{
4119 return( ( (uint64_t) buf[0] << 40 ) |
4120 ( (uint64_t) buf[1] << 32 ) |
4121 ( (uint64_t) buf[2] << 24 ) |
4122 ( (uint64_t) buf[3] << 16 ) |
4123 ( (uint64_t) buf[4] << 8 ) |
4124 ( (uint64_t) buf[5] ) );
4125}
4126
4127/*
4128 * Return 0 if sequence number is acceptable, -1 otherwise
4129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131{
4132 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4133 uint64_t bit;
4134
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004135 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004136 return( 0 );
4137
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004138 if( rec_seqnum > ssl->in_window_top )
4139 return( 0 );
4140
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004141 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004142
4143 if( bit >= 64 )
4144 return( -1 );
4145
4146 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4147 return( -1 );
4148
4149 return( 0 );
4150}
4151
4152/*
4153 * Update replay window on new validated record
4154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004156{
4157 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4158
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004159 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004160 return;
4161
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004162 if( rec_seqnum > ssl->in_window_top )
4163 {
4164 /* Update window_top and the contents of the window */
4165 uint64_t shift = rec_seqnum - ssl->in_window_top;
4166
4167 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004168 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004169 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004170 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004171 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004172 ssl->in_window |= 1;
4173 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004174
4175 ssl->in_window_top = rec_seqnum;
4176 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004177 else
4178 {
4179 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004180 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004181
4182 if( bit < 64 ) /* Always true, but be extra sure */
4183 ssl->in_window |= (uint64_t) 1 << bit;
4184 }
4185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004188#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004189/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004190static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4191
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004192/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004193 * Without any SSL context, check if a datagram looks like a ClientHello with
4194 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004195 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004196 *
4197 * - if cookie is valid, return 0
4198 * - if ClientHello looks superficially valid but cookie is not,
4199 * fill obuf and set olen, then
4200 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4201 * - otherwise return a specific error code
4202 */
4203static int ssl_check_dtls_clihlo_cookie(
4204 mbedtls_ssl_cookie_write_t *f_cookie_write,
4205 mbedtls_ssl_cookie_check_t *f_cookie_check,
4206 void *p_cookie,
4207 const unsigned char *cli_id, size_t cli_id_len,
4208 const unsigned char *in, size_t in_len,
4209 unsigned char *obuf, size_t buf_len, size_t *olen )
4210{
4211 size_t sid_len, cookie_len;
4212 unsigned char *p;
4213
4214 if( f_cookie_write == NULL || f_cookie_check == NULL )
4215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4216
4217 /*
4218 * Structure of ClientHello with record and handshake headers,
4219 * and expected values. We don't need to check a lot, more checks will be
4220 * done when actually parsing the ClientHello - skipping those checks
4221 * avoids code duplication and does not make cookie forging any easier.
4222 *
4223 * 0-0 ContentType type; copied, must be handshake
4224 * 1-2 ProtocolVersion version; copied
4225 * 3-4 uint16 epoch; copied, must be 0
4226 * 5-10 uint48 sequence_number; copied
4227 * 11-12 uint16 length; (ignored)
4228 *
4229 * 13-13 HandshakeType msg_type; (ignored)
4230 * 14-16 uint24 length; (ignored)
4231 * 17-18 uint16 message_seq; copied
4232 * 19-21 uint24 fragment_offset; copied, must be 0
4233 * 22-24 uint24 fragment_length; (ignored)
4234 *
4235 * 25-26 ProtocolVersion client_version; (ignored)
4236 * 27-58 Random random; (ignored)
4237 * 59-xx SessionID session_id; 1 byte len + sid_len content
4238 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4239 * ...
4240 *
4241 * Minimum length is 61 bytes.
4242 */
4243 if( in_len < 61 ||
4244 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4245 in[3] != 0 || in[4] != 0 ||
4246 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4247 {
4248 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4249 }
4250
4251 sid_len = in[59];
4252 if( sid_len > in_len - 61 )
4253 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4254
4255 cookie_len = in[60 + sid_len];
4256 if( cookie_len > in_len - 60 )
4257 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4258
4259 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4260 cli_id, cli_id_len ) == 0 )
4261 {
4262 /* Valid cookie */
4263 return( 0 );
4264 }
4265
4266 /*
4267 * If we get here, we've got an invalid cookie, let's prepare HVR.
4268 *
4269 * 0-0 ContentType type; copied
4270 * 1-2 ProtocolVersion version; copied
4271 * 3-4 uint16 epoch; copied
4272 * 5-10 uint48 sequence_number; copied
4273 * 11-12 uint16 length; olen - 13
4274 *
4275 * 13-13 HandshakeType msg_type; hello_verify_request
4276 * 14-16 uint24 length; olen - 25
4277 * 17-18 uint16 message_seq; copied
4278 * 19-21 uint24 fragment_offset; copied
4279 * 22-24 uint24 fragment_length; olen - 25
4280 *
4281 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4282 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4283 *
4284 * Minimum length is 28.
4285 */
4286 if( buf_len < 28 )
4287 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4288
4289 /* Copy most fields and adapt others */
4290 memcpy( obuf, in, 25 );
4291 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4292 obuf[25] = 0xfe;
4293 obuf[26] = 0xff;
4294
4295 /* Generate and write actual cookie */
4296 p = obuf + 28;
4297 if( f_cookie_write( p_cookie,
4298 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4299 {
4300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4301 }
4302
4303 *olen = p - obuf;
4304
4305 /* Go back and fill length fields */
4306 obuf[27] = (unsigned char)( *olen - 28 );
4307
4308 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4309 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4310 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4311
4312 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4313 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4314
4315 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4316}
4317
4318/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004319 * Handle possible client reconnect with the same UDP quadruplet
4320 * (RFC 6347 Section 4.2.8).
4321 *
4322 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4323 * that looks like a ClientHello.
4324 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004325 * - if the input looks like a ClientHello without cookies,
4326 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004327 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004328 * - if the input looks like a ClientHello with a valid cookie,
4329 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004330 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004331 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004332 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004333 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004334 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4335 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004336 */
4337static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4338{
4339 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004340 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004341
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004342 ret = ssl_check_dtls_clihlo_cookie(
4343 ssl->conf->f_cookie_write,
4344 ssl->conf->f_cookie_check,
4345 ssl->conf->p_cookie,
4346 ssl->cli_id, ssl->cli_id_len,
4347 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004348 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004349
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004350 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4351
4352 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004353 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004354 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004355 * If the error is permanent we'll catch it later,
4356 * if it's not, then hopefully it'll work next time. */
4357 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4358
4359 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360 }
4361
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004362 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004363 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004364 /* Got a valid cookie, partially reset context */
4365 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4366 {
4367 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4368 return( ret );
4369 }
4370
4371 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004372 }
4373
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004374 return( ret );
4375}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004376#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004377
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004378/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004379 * ContentType type;
4380 * ProtocolVersion version;
4381 * uint16 epoch; // DTLS only
4382 * uint48 sequence_number; // DTLS only
4383 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004384 *
4385 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004386 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004387 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4388 *
4389 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004390 * 1. proceed with the record if this function returns 0
4391 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4392 * 3. return CLIENT_RECONNECT if this function return that value
4393 * 4. drop the whole datagram if this function returns anything else.
4394 * Point 2 is needed when the peer is resending, and we have already received
4395 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004398{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004399 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 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 +02004402
Paul Bakker5121ce52009-01-03 21:22:43 +00004403 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004404 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004405 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004408 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004409 ssl->in_msgtype,
4410 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004411
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004412 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4414 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4415 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4416 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004419
4420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004421 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4422 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004423 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4424#endif /* MBEDTLS_SSL_PROTO_DTLS */
4425 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4426 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004429 }
4430
4431 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004432 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 }
4437
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004438 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 }
4443
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004444 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004445 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004446 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4449 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004450 }
4451
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004452 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004453 * DTLS-related tests.
4454 * Check epoch before checking length constraint because
4455 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4456 * message gets duplicated before the corresponding Finished message,
4457 * the second ChangeCipherSpec should be discarded because it belongs
4458 * to an old epoch, but not because its length is shorter than
4459 * the minimum record length for packets using the new record transform.
4460 * Note that these two kinds of failures are handled differently,
4461 * as an unexpected record is silently skipped but an invalid
4462 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004463 */
4464#if defined(MBEDTLS_SSL_PROTO_DTLS)
4465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4466 {
4467 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4468
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004469 /* Check epoch (and sequence number) with DTLS */
4470 if( rec_epoch != ssl->in_epoch )
4471 {
4472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4473 "expected %d, received %d",
4474 ssl->in_epoch, rec_epoch ) );
4475
4476#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4477 /*
4478 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4479 * access the first byte of record content (handshake type), as we
4480 * have an active transform (possibly iv_len != 0), so use the
4481 * fact that the record header len is 13 instead.
4482 */
4483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4484 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4485 rec_epoch == 0 &&
4486 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4487 ssl->in_left > 13 &&
4488 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4489 {
4490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4491 "from the same port" ) );
4492 return( ssl_handle_possible_reconnect( ssl ) );
4493 }
4494 else
4495#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004496 {
4497 /* Consider buffering the record. */
4498 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4499 {
4500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4501 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4502 }
4503
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004504 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004505 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004506 }
4507
4508#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4509 /* Replay detection only works for the current epoch */
4510 if( rec_epoch == ssl->in_epoch &&
4511 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4512 {
4513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4514 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4515 }
4516#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004517
Hanno Becker52c6dc62017-05-26 16:07:36 +01004518 /* Drop unexpected ApplicationData records,
4519 * except at the beginning of renegotiations */
4520 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4521 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4522#if defined(MBEDTLS_SSL_RENEGOTIATION)
4523 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4524 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4525#endif
4526 )
4527 {
4528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4529 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4530 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004531 }
4532#endif /* MBEDTLS_SSL_PROTO_DTLS */
4533
Hanno Becker52c6dc62017-05-26 16:07:36 +01004534
4535 /* Check length against bounds of the current transform and version */
4536 if( ssl->transform_in == NULL )
4537 {
4538 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004539 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004540 {
4541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4543 }
4544 }
4545 else
4546 {
4547 if( ssl->in_msglen < ssl->transform_in->minlen )
4548 {
4549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4551 }
4552
4553#if defined(MBEDTLS_SSL_PROTO_SSL3)
4554 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004555 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004556 {
4557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4559 }
4560#endif
4561#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4562 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4563 /*
4564 * TLS encrypted messages can have up to 256 bytes of padding
4565 */
4566 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4567 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004568 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4571 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4572 }
4573#endif
4574 }
4575
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 return( 0 );
4577}
Paul Bakker5121ce52009-01-03 21:22:43 +00004578
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004579/*
4580 * If applicable, decrypt (and decompress) record content
4581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004583{
4584 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4587 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4590 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 ret = mbedtls_ssl_hw_record_read( ssl );
4595 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4598 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004599 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004600
4601 if( ret == 0 )
4602 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004605 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004607 mbedtls_record rec;
4608
4609 rec.buf = ssl->in_iv;
4610 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4611 - ( ssl->in_iv - ssl->in_buf );
4612 rec.data_len = ssl->in_msglen;
4613 rec.data_offset = 0;
4614
4615 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4616 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4617 ssl->conf->transport, rec.ver );
4618 rec.type = ssl->in_msgtype;
4619 if( ( ret = ssl_decrypt_buf( ssl, ssl->transform_in, &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 return( ret );
4623 }
4624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 ssl->in_msg, ssl->in_msglen );
4627
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004628 ssl->in_msglen = rec.data_len;
4629 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4630 ssl->in_len[1] = (unsigned char)( rec.data_len );
4631
Angus Grattond8213d02016-05-25 20:56:48 +10004632 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4635 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004637 else if( ssl->in_msglen == 0 )
4638 {
4639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4640 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4641 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4642 {
4643 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4645 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4646 }
4647#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4648
4649 ssl->nb_zero++;
4650
4651 /*
4652 * Three or more empty messages may be a DoS attack
4653 * (excessive CPU consumption).
4654 */
4655 if( ssl->nb_zero > 3 )
4656 {
4657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4658 "messages, possible DoS attack" ) );
4659 /* Q: Is that the right error code? */
4660 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4661 }
4662 }
4663 else
4664 ssl->nb_zero = 0;
4665
4666#if defined(MBEDTLS_SSL_PROTO_DTLS)
4667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4668 {
4669 ; /* in_ctr read from peer, not maintained internally */
4670 }
4671 else
4672#endif
4673 {
4674 unsigned i;
4675 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4676 if( ++ssl->in_ctr[i - 1] != 0 )
4677 break;
4678
4679 /* The loop goes to its end iff the counter is wrapping */
4680 if( i == ssl_ep_len( ssl ) )
4681 {
4682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4683 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4684 }
4685 }
4686
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 }
4688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004690 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004692 {
4693 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004696 return( ret );
4697 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004705 }
4706#endif
4707
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004708 return( 0 );
4709}
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004712
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004713/*
4714 * Read a record.
4715 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004716 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4717 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4718 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004719 */
Hanno Becker1097b342018-08-15 14:09:41 +01004720
4721/* Helper functions for mbedtls_ssl_read_record(). */
4722static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004723static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4724static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004725
Hanno Becker327c93b2018-08-15 13:56:18 +01004726int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004727 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004728{
4729 int ret;
4730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004732
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004733 if( ssl->keep_current_message == 0 )
4734 {
4735 do {
Simon Butcher99000142016-10-13 17:21:01 +01004736
Hanno Becker26994592018-08-15 14:14:59 +01004737 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004738 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004739 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004740
Hanno Beckere74d5562018-08-15 14:26:08 +01004741 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004742 {
Hanno Becker40f50842018-08-15 14:48:01 +01004743#if defined(MBEDTLS_SSL_PROTO_DTLS)
4744 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004745
Hanno Becker40f50842018-08-15 14:48:01 +01004746 /* We only check for buffered messages if the
4747 * current datagram is fully consumed. */
4748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004749 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004750 {
Hanno Becker40f50842018-08-15 14:48:01 +01004751 if( ssl_load_buffered_message( ssl ) == 0 )
4752 have_buffered = 1;
4753 }
4754
4755 if( have_buffered == 0 )
4756#endif /* MBEDTLS_SSL_PROTO_DTLS */
4757 {
4758 ret = ssl_get_next_record( ssl );
4759 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4760 continue;
4761
4762 if( ret != 0 )
4763 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004764 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004765 return( ret );
4766 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004767 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004768 }
4769
4770 ret = mbedtls_ssl_handle_message_type( ssl );
4771
Hanno Becker40f50842018-08-15 14:48:01 +01004772#if defined(MBEDTLS_SSL_PROTO_DTLS)
4773 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4774 {
4775 /* Buffer future message */
4776 ret = ssl_buffer_message( ssl );
4777 if( ret != 0 )
4778 return( ret );
4779
4780 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4781 }
4782#endif /* MBEDTLS_SSL_PROTO_DTLS */
4783
Hanno Becker90333da2017-10-10 11:27:13 +01004784 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4785 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004786
4787 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004788 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004789 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004790 return( ret );
4791 }
4792
Hanno Becker327c93b2018-08-15 13:56:18 +01004793 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004794 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004795 {
4796 mbedtls_ssl_update_handshake_status( ssl );
4797 }
Simon Butcher99000142016-10-13 17:21:01 +01004798 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004799 else
Simon Butcher99000142016-10-13 17:21:01 +01004800 {
Hanno Becker02f59072018-08-15 14:00:24 +01004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004802 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004803 }
4804
4805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4806
4807 return( 0 );
4808}
4809
Hanno Becker40f50842018-08-15 14:48:01 +01004810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004811static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004812{
Hanno Becker40f50842018-08-15 14:48:01 +01004813 if( ssl->in_left > ssl->next_record_offset )
4814 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004815
Hanno Becker40f50842018-08-15 14:48:01 +01004816 return( 0 );
4817}
4818
4819static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4820{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004821 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004822 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004823 int ret = 0;
4824
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004825 if( hs == NULL )
4826 return( -1 );
4827
Hanno Beckere00ae372018-08-20 09:39:42 +01004828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4829
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004830 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4831 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4832 {
4833 /* Check if we have seen a ChangeCipherSpec before.
4834 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004835 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004836 {
4837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4838 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004839 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004840 }
4841
Hanno Becker39b8bc92018-08-28 17:17:13 +01004842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004843 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4844 ssl->in_msglen = 1;
4845 ssl->in_msg[0] = 1;
4846
4847 /* As long as they are equal, the exact value doesn't matter. */
4848 ssl->in_left = 0;
4849 ssl->next_record_offset = 0;
4850
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004851 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004852 goto exit;
4853 }
Hanno Becker37f95322018-08-16 13:55:32 +01004854
Hanno Beckerb8f50142018-08-28 10:01:34 +01004855#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004856 /* Debug only */
4857 {
4858 unsigned offset;
4859 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4860 {
4861 hs_buf = &hs->buffering.hs[offset];
4862 if( hs_buf->is_valid == 1 )
4863 {
4864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4865 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004866 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004867 }
4868 }
4869 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004870#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004871
4872 /* Check if we have buffered and/or fully reassembled the
4873 * next handshake message. */
4874 hs_buf = &hs->buffering.hs[0];
4875 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4876 {
4877 /* Synthesize a record containing the buffered HS message. */
4878 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4879 ( hs_buf->data[2] << 8 ) |
4880 hs_buf->data[3];
4881
4882 /* Double-check that we haven't accidentally buffered
4883 * a message that doesn't fit into the input buffer. */
4884 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4885 {
4886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4888 }
4889
4890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4891 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4892 hs_buf->data, msg_len + 12 );
4893
4894 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4895 ssl->in_hslen = msg_len + 12;
4896 ssl->in_msglen = msg_len + 12;
4897 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4898
4899 ret = 0;
4900 goto exit;
4901 }
4902 else
4903 {
4904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4905 hs->in_msg_seq ) );
4906 }
4907
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004908 ret = -1;
4909
4910exit:
4911
4912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4913 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004914}
4915
Hanno Beckera02b0b42018-08-21 17:20:27 +01004916static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4917 size_t desired )
4918{
4919 int offset;
4920 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4922 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004923
Hanno Becker01315ea2018-08-21 17:22:17 +01004924 /* Get rid of future records epoch first, if such exist. */
4925 ssl_free_buffered_record( ssl );
4926
4927 /* Check if we have enough space available now. */
4928 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4929 hs->buffering.total_bytes_buffered ) )
4930 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004932 return( 0 );
4933 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004934
Hanno Becker4f432ad2018-08-28 10:02:32 +01004935 /* We don't have enough space to buffer the next expected handshake
4936 * message. Remove buffers used for future messages to gain space,
4937 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004938 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4939 offset >= 0; offset-- )
4940 {
4941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4942 offset ) );
4943
Hanno Beckerb309b922018-08-23 13:18:05 +01004944 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004945
4946 /* Check if we have enough space available now. */
4947 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4948 hs->buffering.total_bytes_buffered ) )
4949 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004951 return( 0 );
4952 }
4953 }
4954
4955 return( -1 );
4956}
4957
Hanno Becker40f50842018-08-15 14:48:01 +01004958static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4959{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004960 int ret = 0;
4961 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4962
4963 if( hs == NULL )
4964 return( 0 );
4965
4966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4967
4968 switch( ssl->in_msgtype )
4969 {
4970 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004972
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004973 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004974 break;
4975
4976 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004977 {
4978 unsigned recv_msg_seq_offset;
4979 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4980 mbedtls_ssl_hs_buffer *hs_buf;
4981 size_t msg_len = ssl->in_hslen - 12;
4982
4983 /* We should never receive an old handshake
4984 * message - double-check nonetheless. */
4985 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4986 {
4987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4989 }
4990
4991 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4992 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4993 {
4994 /* Silently ignore -- message too far in the future */
4995 MBEDTLS_SSL_DEBUG_MSG( 2,
4996 ( "Ignore future HS message with sequence number %u, "
4997 "buffering window %u - %u",
4998 recv_msg_seq, ssl->handshake->in_msg_seq,
4999 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5000
5001 goto exit;
5002 }
5003
5004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5005 recv_msg_seq, recv_msg_seq_offset ) );
5006
5007 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5008
5009 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005010 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005011 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005012 size_t reassembly_buf_sz;
5013
Hanno Becker37f95322018-08-16 13:55:32 +01005014 hs_buf->is_fragmented =
5015 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5016
5017 /* We copy the message back into the input buffer
5018 * after reassembly, so check that it's not too large.
5019 * This is an implementation-specific limitation
5020 * and not one from the standard, hence it is not
5021 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005022 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005023 {
5024 /* Ignore message */
5025 goto exit;
5026 }
5027
Hanno Beckere0b150f2018-08-21 15:51:03 +01005028 /* Check if we have enough space to buffer the message. */
5029 if( hs->buffering.total_bytes_buffered >
5030 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5031 {
5032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5033 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5034 }
5035
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005036 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5037 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005038
5039 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5040 hs->buffering.total_bytes_buffered ) )
5041 {
5042 if( recv_msg_seq_offset > 0 )
5043 {
5044 /* If we can't buffer a future message because
5045 * of space limitations -- ignore. */
5046 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",
5047 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5048 (unsigned) hs->buffering.total_bytes_buffered ) );
5049 goto exit;
5050 }
Hanno Beckere1801392018-08-21 16:51:05 +01005051 else
5052 {
5053 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",
5054 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5055 (unsigned) hs->buffering.total_bytes_buffered ) );
5056 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005057
Hanno Beckera02b0b42018-08-21 17:20:27 +01005058 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005059 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005060 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",
5061 (unsigned) msg_len,
5062 (unsigned) reassembly_buf_sz,
5063 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005064 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005065 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5066 goto exit;
5067 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005068 }
5069
5070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5071 msg_len ) );
5072
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005073 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5074 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005075 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005076 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005077 goto exit;
5078 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005079 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005080
5081 /* Prepare final header: copy msg_type, length and message_seq,
5082 * then add standardised fragment_offset and fragment_length */
5083 memcpy( hs_buf->data, ssl->in_msg, 6 );
5084 memset( hs_buf->data + 6, 0, 3 );
5085 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5086
5087 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005088
5089 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 }
5091 else
5092 {
5093 /* Make sure msg_type and length are consistent */
5094 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5095 {
5096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5097 /* Ignore */
5098 goto exit;
5099 }
5100 }
5101
Hanno Becker4422bbb2018-08-20 09:40:19 +01005102 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005103 {
5104 size_t frag_len, frag_off;
5105 unsigned char * const msg = hs_buf->data + 12;
5106
5107 /*
5108 * Check and copy current fragment
5109 */
5110
5111 /* Validation of header fields already done in
5112 * mbedtls_ssl_prepare_handshake_record(). */
5113 frag_off = ssl_get_hs_frag_off( ssl );
5114 frag_len = ssl_get_hs_frag_len( ssl );
5115
5116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5117 frag_off, frag_len ) );
5118 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5119
5120 if( hs_buf->is_fragmented )
5121 {
5122 unsigned char * const bitmask = msg + msg_len;
5123 ssl_bitmask_set( bitmask, frag_off, frag_len );
5124 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5125 msg_len ) == 0 );
5126 }
5127 else
5128 {
5129 hs_buf->is_complete = 1;
5130 }
5131
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5133 hs_buf->is_complete ? "" : "not yet " ) );
5134 }
5135
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005136 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005137 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005138
5139 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005140 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005141 break;
5142 }
5143
5144exit:
5145
5146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5147 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005148}
5149#endif /* MBEDTLS_SSL_PROTO_DTLS */
5150
Hanno Becker1097b342018-08-15 14:09:41 +01005151static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005152{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005153 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005154 * Consume last content-layer message and potentially
5155 * update in_msglen which keeps track of the contents'
5156 * consumption state.
5157 *
5158 * (1) Handshake messages:
5159 * Remove last handshake message, move content
5160 * and adapt in_msglen.
5161 *
5162 * (2) Alert messages:
5163 * Consume whole record content, in_msglen = 0.
5164 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005165 * (3) Change cipher spec:
5166 * Consume whole record content, in_msglen = 0.
5167 *
5168 * (4) Application data:
5169 * Don't do anything - the record layer provides
5170 * the application data as a stream transport
5171 * and consumes through mbedtls_ssl_read only.
5172 *
5173 */
5174
5175 /* Case (1): Handshake messages */
5176 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005177 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005178 /* Hard assertion to be sure that no application data
5179 * is in flight, as corrupting ssl->in_msglen during
5180 * ssl->in_offt != NULL is fatal. */
5181 if( ssl->in_offt != NULL )
5182 {
5183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5185 }
5186
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005187 /*
5188 * Get next Handshake message in the current record
5189 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005190
Hanno Becker4a810fb2017-05-24 16:27:30 +01005191 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005192 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005193 * current handshake content: If DTLS handshake
5194 * fragmentation is used, that's the fragment
5195 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005196 * size here is faulty and should be changed at
5197 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005198 * (2) While it doesn't seem to cause problems, one
5199 * has to be very careful not to assume that in_hslen
5200 * is always <= in_msglen in a sensible communication.
5201 * Again, it's wrong for DTLS handshake fragmentation.
5202 * The following check is therefore mandatory, and
5203 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005204 * Additionally, ssl->in_hslen might be arbitrarily out of
5205 * bounds after handling a DTLS message with an unexpected
5206 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005207 */
5208 if( ssl->in_hslen < ssl->in_msglen )
5209 {
5210 ssl->in_msglen -= ssl->in_hslen;
5211 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5212 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005213
Hanno Becker4a810fb2017-05-24 16:27:30 +01005214 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5215 ssl->in_msg, ssl->in_msglen );
5216 }
5217 else
5218 {
5219 ssl->in_msglen = 0;
5220 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005221
Hanno Becker4a810fb2017-05-24 16:27:30 +01005222 ssl->in_hslen = 0;
5223 }
5224 /* Case (4): Application data */
5225 else if( ssl->in_offt != NULL )
5226 {
5227 return( 0 );
5228 }
5229 /* Everything else (CCS & Alerts) */
5230 else
5231 {
5232 ssl->in_msglen = 0;
5233 }
5234
Hanno Becker1097b342018-08-15 14:09:41 +01005235 return( 0 );
5236}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005237
Hanno Beckere74d5562018-08-15 14:26:08 +01005238static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5239{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005240 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005241 return( 1 );
5242
5243 return( 0 );
5244}
5245
Hanno Becker5f066e72018-08-16 14:56:31 +01005246#if defined(MBEDTLS_SSL_PROTO_DTLS)
5247
5248static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5249{
5250 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5251 if( hs == NULL )
5252 return;
5253
Hanno Becker01315ea2018-08-21 17:22:17 +01005254 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005255 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005256 hs->buffering.total_bytes_buffered -=
5257 hs->buffering.future_record.len;
5258
5259 mbedtls_free( hs->buffering.future_record.data );
5260 hs->buffering.future_record.data = NULL;
5261 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005262}
5263
5264static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5265{
5266 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5267 unsigned char * rec;
5268 size_t rec_len;
5269 unsigned rec_epoch;
5270
5271 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5272 return( 0 );
5273
5274 if( hs == NULL )
5275 return( 0 );
5276
Hanno Becker5f066e72018-08-16 14:56:31 +01005277 rec = hs->buffering.future_record.data;
5278 rec_len = hs->buffering.future_record.len;
5279 rec_epoch = hs->buffering.future_record.epoch;
5280
5281 if( rec == NULL )
5282 return( 0 );
5283
Hanno Becker4cb782d2018-08-20 11:19:05 +01005284 /* Only consider loading future records if the
5285 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005286 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005287 return( 0 );
5288
Hanno Becker5f066e72018-08-16 14:56:31 +01005289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5290
5291 if( rec_epoch != ssl->in_epoch )
5292 {
5293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5294 goto exit;
5295 }
5296
5297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5298
5299 /* Double-check that the record is not too large */
5300 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5301 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5302 {
5303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5305 }
5306
5307 memcpy( ssl->in_hdr, rec, rec_len );
5308 ssl->in_left = rec_len;
5309 ssl->next_record_offset = 0;
5310
5311 ssl_free_buffered_record( ssl );
5312
5313exit:
5314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5315 return( 0 );
5316}
5317
5318static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5319{
5320 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5321 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005322 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005323
5324 /* Don't buffer future records outside handshakes. */
5325 if( hs == NULL )
5326 return( 0 );
5327
5328 /* Only buffer handshake records (we are only interested
5329 * in Finished messages). */
5330 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5331 return( 0 );
5332
5333 /* Don't buffer more than one future epoch record. */
5334 if( hs->buffering.future_record.data != NULL )
5335 return( 0 );
5336
Hanno Becker01315ea2018-08-21 17:22:17 +01005337 /* Don't buffer record if there's not enough buffering space remaining. */
5338 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5339 hs->buffering.total_bytes_buffered ) )
5340 {
5341 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",
5342 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5343 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005344 return( 0 );
5345 }
5346
Hanno Becker5f066e72018-08-16 14:56:31 +01005347 /* Buffer record */
5348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5349 ssl->in_epoch + 1 ) );
5350 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5351 rec_hdr_len + ssl->in_msglen );
5352
5353 /* ssl_parse_record_header() only considers records
5354 * of the next epoch as candidates for buffering. */
5355 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005356 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005357
5358 hs->buffering.future_record.data =
5359 mbedtls_calloc( 1, hs->buffering.future_record.len );
5360 if( hs->buffering.future_record.data == NULL )
5361 {
5362 /* If we run out of RAM trying to buffer a
5363 * record from the next epoch, just ignore. */
5364 return( 0 );
5365 }
5366
Hanno Becker01315ea2018-08-21 17:22:17 +01005367 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005368
Hanno Becker01315ea2018-08-21 17:22:17 +01005369 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005370 return( 0 );
5371}
5372
5373#endif /* MBEDTLS_SSL_PROTO_DTLS */
5374
Hanno Beckere74d5562018-08-15 14:26:08 +01005375static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005376{
5377 int ret;
5378
Hanno Becker5f066e72018-08-16 14:56:31 +01005379#if defined(MBEDTLS_SSL_PROTO_DTLS)
5380 /* We might have buffered a future record; if so,
5381 * and if the epoch matches now, load it.
5382 * On success, this call will set ssl->in_left to
5383 * the length of the buffered record, so that
5384 * the calls to ssl_fetch_input() below will
5385 * essentially be no-ops. */
5386 ret = ssl_load_buffered_record( ssl );
5387 if( ret != 0 )
5388 return( ret );
5389#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005394 return( ret );
5395 }
5396
5397 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5401 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005402 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005403 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5404 {
5405 ret = ssl_buffer_future_record( ssl );
5406 if( ret != 0 )
5407 return( ret );
5408
5409 /* Fall through to handling of unexpected records */
5410 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5411 }
5412
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005413 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5414 {
5415 /* Skip unexpected record (but not whole datagram) */
5416 ssl->next_record_offset = ssl->in_msglen
5417 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005418
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5420 "(header)" ) );
5421 }
5422 else
5423 {
5424 /* Skip invalid record and the rest of the datagram */
5425 ssl->next_record_offset = 0;
5426 ssl->in_left = 0;
5427
5428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5429 "(header)" ) );
5430 }
5431
5432 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005433 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005434 }
5435#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005436 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005437 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005438
5439 /*
5440 * Read and optionally decrypt the message contents
5441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5443 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005446 return( ret );
5447 }
5448
5449 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005454 if( ssl->next_record_offset < ssl->in_left )
5455 {
5456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5457 }
5458 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459 else
5460#endif
5461 ssl->in_left = 0;
5462
5463 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005467 {
5468 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5470 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005471 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005472 /* Except when waiting for Finished as a bad mac here
5473 * probably means something went wrong in the handshake
5474 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5475 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5476 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5477 {
5478#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5479 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5480 {
5481 mbedtls_ssl_send_alert_message( ssl,
5482 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5483 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5484 }
5485#endif
5486 return( ret );
5487 }
5488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005490 if( ssl->conf->badmac_limit != 0 &&
5491 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5494 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005495 }
5496#endif
5497
Hanno Becker4a810fb2017-05-24 16:27:30 +01005498 /* As above, invalid records cause
5499 * dismissal of the whole datagram. */
5500
5501 ssl->next_record_offset = 0;
5502 ssl->in_left = 0;
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005505 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005506 }
5507
5508 return( ret );
5509 }
5510 else
5511#endif
5512 {
5513 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5515 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 mbedtls_ssl_send_alert_message( ssl,
5518 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5519 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005520 }
5521#endif
5522 return( ret );
5523 }
5524 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005525
Simon Butcher99000142016-10-13 17:21:01 +01005526 return( 0 );
5527}
5528
5529int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5530{
5531 int ret;
5532
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005533 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005534 * Handle particular types of records
5535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005537 {
Simon Butcher99000142016-10-13 17:21:01 +01005538 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5539 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005540 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 }
5543
Hanno Beckere678eaa2018-08-21 14:57:46 +01005544 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005545 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005546 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005547 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5549 ssl->in_msglen ) );
5550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005551 }
5552
Hanno Beckere678eaa2018-08-21 14:57:46 +01005553 if( ssl->in_msg[0] != 1 )
5554 {
5555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5556 ssl->in_msg[0] ) );
5557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5558 }
5559
5560#if defined(MBEDTLS_SSL_PROTO_DTLS)
5561 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5562 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5563 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5564 {
5565 if( ssl->handshake == NULL )
5566 {
5567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5568 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5569 }
5570
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5572 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5573 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005574#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005575 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005578 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005579 if( ssl->in_msglen != 2 )
5580 {
5581 /* Note: Standard allows for more than one 2 byte alert
5582 to be packed in a single message, but Mbed TLS doesn't
5583 currently support this. */
5584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5585 ssl->in_msglen ) );
5586 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5587 }
5588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 ssl->in_msg[0], ssl->in_msg[1] ) );
5591
5592 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005593 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005598 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 }
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5603 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5606 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005607 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005608
5609#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5610 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5611 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5612 {
Hanno Becker90333da2017-10-10 11:27:13 +01005613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005614 /* Will be handled when trying to parse ServerHello */
5615 return( 0 );
5616 }
5617#endif
5618
5619#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5620 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5621 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5622 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5623 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5624 {
5625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5626 /* Will be handled in mbedtls_ssl_parse_certificate() */
5627 return( 0 );
5628 }
5629#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5630
5631 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005632 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 }
5634
Hanno Beckerc76c6192017-06-06 10:03:17 +01005635#if defined(MBEDTLS_SSL_PROTO_DTLS)
5636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5637 ssl->handshake != NULL &&
5638 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5639 {
5640 ssl_handshake_wrapup_free_hs_transform( ssl );
5641 }
5642#endif
5643
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 return( 0 );
5645}
5646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005648{
5649 int ret;
5650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5653 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005654 {
5655 return( ret );
5656 }
5657
5658 return( 0 );
5659}
5660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005662 unsigned char level,
5663 unsigned char message )
5664{
5665 int ret;
5666
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005667 if( ssl == NULL || ssl->conf == NULL )
5668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005674 ssl->out_msglen = 2;
5675 ssl->out_msg[0] = level;
5676 ssl->out_msg[1] = message;
5677
Hanno Becker67bc7c32018-08-06 11:33:50 +01005678 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005681 return( ret );
5682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005684
5685 return( 0 );
5686}
5687
Hanno Beckerb9d44792019-02-08 07:19:04 +00005688#if defined(MBEDTLS_X509_CRT_PARSE_C)
5689static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5690{
5691#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5692 if( session->peer_cert != NULL )
5693 {
5694 mbedtls_x509_crt_free( session->peer_cert );
5695 mbedtls_free( session->peer_cert );
5696 session->peer_cert = NULL;
5697 }
5698#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5699 if( session->peer_cert_digest != NULL )
5700 {
5701 /* Zeroization is not necessary. */
5702 mbedtls_free( session->peer_cert_digest );
5703 session->peer_cert_digest = NULL;
5704 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5705 session->peer_cert_digest_len = 0;
5706 }
5707#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5708}
5709#endif /* MBEDTLS_X509_CRT_PARSE_C */
5710
Paul Bakker5121ce52009-01-03 21:22:43 +00005711/*
5712 * Handshake functions
5713 */
Hanno Becker21489932019-02-05 13:20:55 +00005714#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005715/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005718 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5719 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
Hanno Becker7177a882019-02-05 13:36:46 +00005723 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005726 ssl->state++;
5727 return( 0 );
5728 }
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005735{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005736 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5737 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005740
Hanno Becker7177a882019-02-05 13:36:46 +00005741 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005744 ssl->state++;
5745 return( 0 );
5746 }
5747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5749 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005750}
Gilles Peskinef9828522017-05-03 12:28:43 +02005751
Hanno Becker21489932019-02-05 13:20:55 +00005752#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005753/* Some certificate support -> implement write and parse */
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005758 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005760 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5761 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005764
Hanno Becker7177a882019-02-05 13:36:46 +00005765 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005768 ssl->state++;
5769 return( 0 );
5770 }
5771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005773 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 {
5775 if( ssl->client_auth == 0 )
5776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005778 ssl->state++;
5779 return( 0 );
5780 }
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 /*
5784 * If using SSLv3 and got no cert, send an Alert message
5785 * (otherwise an empty Certificate message will be sent).
5786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5788 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005789 {
5790 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5792 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5793 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 goto write_msg;
5797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005799 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800#endif /* MBEDTLS_SSL_CLI_C */
5801#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005802 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5807 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005810#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005813
5814 /*
5815 * 0 . 0 handshake type
5816 * 1 . 3 handshake length
5817 * 4 . 6 length of all certs
5818 * 7 . 9 length of cert. 1
5819 * 10 . n-1 peer certificate
5820 * n . n+2 length of cert. 2
5821 * n+3 . ... upper level cert, etc.
5822 */
5823 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825
Paul Bakker29087132010-03-21 21:03:34 +00005826 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 {
5828 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005829 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005832 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 }
5835
5836 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5837 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5838 ssl->out_msg[i + 2] = (unsigned char)( n );
5839
5840 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5841 i += n; crt = crt->next;
5842 }
5843
5844 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5845 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5846 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5847
5848 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5850 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005851
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005852#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005853write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005855
5856 ssl->state++;
5857
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005858 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 return( ret );
5862 }
5863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005865
Paul Bakkered27a042013-04-18 22:46:23 +02005866 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867}
5868
Hanno Becker84879e32019-01-31 07:44:03 +00005869#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005870
5871#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005872static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5873 unsigned char *crt_buf,
5874 size_t crt_buf_len )
5875{
5876 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5877
5878 if( peer_crt == NULL )
5879 return( -1 );
5880
5881 if( peer_crt->raw.len != crt_buf_len )
5882 return( -1 );
5883
Hanno Becker46f34d02019-02-08 14:00:04 +00005884 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005885}
Hanno Becker177475a2019-02-05 17:02:46 +00005886#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5887static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5888 unsigned char *crt_buf,
5889 size_t crt_buf_len )
5890{
5891 int ret;
5892 unsigned char const * const peer_cert_digest =
5893 ssl->session->peer_cert_digest;
5894 mbedtls_md_type_t const peer_cert_digest_type =
5895 ssl->session->peer_cert_digest_type;
5896 mbedtls_md_info_t const * const digest_info =
5897 mbedtls_md_info_from_type( peer_cert_digest_type );
5898 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5899 size_t digest_len;
5900
5901 if( peer_cert_digest == NULL || digest_info == NULL )
5902 return( -1 );
5903
5904 digest_len = mbedtls_md_get_size( digest_info );
5905 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5906 return( -1 );
5907
5908 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5909 if( ret != 0 )
5910 return( -1 );
5911
5912 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5913}
5914#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005915#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005916
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005917/*
5918 * Once the certificate message is read, parse it into a cert chain and
5919 * perform basic checks, but leave actual verification to the caller
5920 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005921static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5922 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005923{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005924 int ret;
5925#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5926 int crt_cnt=0;
5927#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005928 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005929 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005934 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5935 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 }
5938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5940 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005943 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5944 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 }
5947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005949
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005953 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005954
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005955 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005964 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5965 i += 3;
5966
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005967 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 while( i < ssl->in_hslen )
5969 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005970 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005971 if ( i + 3 > ssl->in_hslen ) {
5972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005973 mbedtls_ssl_send_alert_message( ssl,
5974 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5975 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005976 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5977 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005978 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5979 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005980 if( ssl->in_msg[i] != 0 )
5981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005983 mbedtls_ssl_send_alert_message( ssl,
5984 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 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005990 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5991 | (unsigned int) ssl->in_msg[i + 2];
5992 i += 3;
5993
5994 if( n < 128 || i + n > ssl->in_hslen )
5995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005997 mbedtls_ssl_send_alert_message( ssl,
5998 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5999 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006001 }
6002
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006003 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006004#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6005 if( crt_cnt++ == 0 &&
6006 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6007 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006008 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006009 /* During client-side renegotiation, check that the server's
6010 * end-CRTs hasn't changed compared to the initial handshake,
6011 * mitigating the triple handshake attack. On success, reuse
6012 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006013 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6014 if( ssl_check_peer_crt_unchanged( ssl,
6015 &ssl->in_msg[i],
6016 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006017 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6019 mbedtls_ssl_send_alert_message( ssl,
6020 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6021 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6022 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006023 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006024
6025 /* Now we can safely free the original chain. */
6026 ssl_clear_peer_cert( ssl->session );
6027 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006028#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6029
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006030 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006031#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006032 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006033#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006034 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006035 * it in-place from the input buffer instead of making a copy. */
6036 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6037#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006038 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006040 case 0: /*ok*/
6041 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6042 /* Ignore certificate with an unknown algorithm: maybe a
6043 prior certificate was already trusted. */
6044 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006045
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006046 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6047 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6048 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006049
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006050 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6051 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6052 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006053
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006054 default:
6055 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6056 crt_parse_der_failed:
6057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6058 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6059 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
6062 i += n;
6063 }
6064
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006065 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006066 return( 0 );
6067}
6068
Hanno Becker4a55f632019-02-05 12:49:06 +00006069#if defined(MBEDTLS_SSL_SRV_C)
6070static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6071{
6072 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6073 return( -1 );
6074
6075#if defined(MBEDTLS_SSL_PROTO_SSL3)
6076 /*
6077 * Check if the client sent an empty certificate
6078 */
6079 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6080 {
6081 if( ssl->in_msglen == 2 &&
6082 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6083 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6084 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6085 {
6086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6087 return( 0 );
6088 }
6089
6090 return( -1 );
6091 }
6092#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6093
6094#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6095 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6096 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6097 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6098 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6099 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6100 {
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6102 return( 0 );
6103 }
6104
6105 return( -1 );
6106#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6107 MBEDTLS_SSL_PROTO_TLS1_2 */
6108}
6109#endif /* MBEDTLS_SSL_SRV_C */
6110
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006111/* Check if a certificate message is expected.
6112 * Return either
6113 * - SSL_CERTIFICATE_EXPECTED, or
6114 * - SSL_CERTIFICATE_SKIP
6115 * indicating whether a Certificate message is expected or not.
6116 */
6117#define SSL_CERTIFICATE_EXPECTED 0
6118#define SSL_CERTIFICATE_SKIP 1
6119static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6120 int authmode )
6121{
6122 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006123 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006124
6125 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6126 return( SSL_CERTIFICATE_SKIP );
6127
6128#if defined(MBEDTLS_SSL_SRV_C)
6129 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6130 {
6131 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6132 return( SSL_CERTIFICATE_SKIP );
6133
6134 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6135 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006136 ssl->session_negotiate->verify_result =
6137 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6138 return( SSL_CERTIFICATE_SKIP );
6139 }
6140 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006141#else
6142 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006143#endif /* MBEDTLS_SSL_SRV_C */
6144
6145 return( SSL_CERTIFICATE_EXPECTED );
6146}
6147
Hanno Becker68636192019-02-05 14:36:34 +00006148static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6149 int authmode,
6150 mbedtls_x509_crt *chain,
6151 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006152{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006153 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006155 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006156 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006157
Hanno Becker8927c832019-04-03 12:52:50 +01006158 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6159 void *p_vrfy;
6160
Hanno Becker68636192019-02-05 14:36:34 +00006161 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6162 return( 0 );
6163
Hanno Becker8927c832019-04-03 12:52:50 +01006164 if( ssl->f_vrfy != NULL )
6165 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006167 f_vrfy = ssl->f_vrfy;
6168 p_vrfy = ssl->p_vrfy;
6169 }
6170 else
6171 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006173 f_vrfy = ssl->conf->f_vrfy;
6174 p_vrfy = ssl->conf->p_vrfy;
6175 }
6176
Hanno Becker68636192019-02-05 14:36:34 +00006177 /*
6178 * Main check: verify certificate
6179 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006180#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6181 if( ssl->conf->f_ca_cb != NULL )
6182 {
6183 ((void) rs_ctx);
6184 have_ca_chain = 1;
6185
6186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006187 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006188 chain,
6189 ssl->conf->f_ca_cb,
6190 ssl->conf->p_ca_cb,
6191 ssl->conf->cert_profile,
6192 ssl->hostname,
6193 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006194 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006195 }
6196 else
6197#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6198 {
6199 mbedtls_x509_crt *ca_chain;
6200 mbedtls_x509_crl *ca_crl;
6201
6202#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6203 if( ssl->handshake->sni_ca_chain != NULL )
6204 {
6205 ca_chain = ssl->handshake->sni_ca_chain;
6206 ca_crl = ssl->handshake->sni_ca_crl;
6207 }
6208 else
6209#endif
6210 {
6211 ca_chain = ssl->conf->ca_chain;
6212 ca_crl = ssl->conf->ca_crl;
6213 }
6214
6215 if( ca_chain != NULL )
6216 have_ca_chain = 1;
6217
6218 ret = mbedtls_x509_crt_verify_restartable(
6219 chain,
6220 ca_chain, ca_crl,
6221 ssl->conf->cert_profile,
6222 ssl->hostname,
6223 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006224 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006225 }
Hanno Becker68636192019-02-05 14:36:34 +00006226
6227 if( ret != 0 )
6228 {
6229 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6230 }
6231
6232#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6233 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6234 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6235#endif
6236
6237 /*
6238 * Secondary checks: always done, but change 'ret' only if it was 0
6239 */
6240
6241#if defined(MBEDTLS_ECP_C)
6242 {
6243 const mbedtls_pk_context *pk = &chain->pk;
6244
6245 /* If certificate uses an EC key, make sure the curve is OK */
6246 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6247 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6248 {
6249 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6250
6251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6252 if( ret == 0 )
6253 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6254 }
6255 }
6256#endif /* MBEDTLS_ECP_C */
6257
6258 if( mbedtls_ssl_check_cert_usage( chain,
6259 ciphersuite_info,
6260 ! ssl->conf->endpoint,
6261 &ssl->session_negotiate->verify_result ) != 0 )
6262 {
6263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6264 if( ret == 0 )
6265 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6266 }
6267
6268 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6269 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6270 * with details encoded in the verification flags. All other kinds
6271 * of error codes, including those from the user provided f_vrfy
6272 * functions, are treated as fatal and lead to a failure of
6273 * ssl_parse_certificate even if verification was optional. */
6274 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6275 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6276 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6277 {
6278 ret = 0;
6279 }
6280
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006281 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6284 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6285 }
6286
6287 if( ret != 0 )
6288 {
6289 uint8_t alert;
6290
6291 /* The certificate may have been rejected for several reasons.
6292 Pick one and send the corresponding alert. Which alert to send
6293 may be a subject of debate in some cases. */
6294 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6295 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6296 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6297 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6298 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6299 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6300 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6301 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6302 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6303 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6304 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6305 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6306 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6307 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6308 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6309 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6310 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6311 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6312 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6313 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6314 else
6315 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6317 alert );
6318 }
6319
6320#if defined(MBEDTLS_DEBUG_C)
6321 if( ssl->session_negotiate->verify_result != 0 )
6322 {
6323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6324 ssl->session_negotiate->verify_result ) );
6325 }
6326 else
6327 {
6328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6329 }
6330#endif /* MBEDTLS_DEBUG_C */
6331
6332 return( ret );
6333}
6334
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006335#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6336static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6337 unsigned char *start, size_t len )
6338{
6339 int ret;
6340 /* Remember digest of the peer's end-CRT. */
6341 ssl->session_negotiate->peer_cert_digest =
6342 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6343 if( ssl->session_negotiate->peer_cert_digest == NULL )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6346 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6347 mbedtls_ssl_send_alert_message( ssl,
6348 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6349 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6350
6351 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6352 }
6353
6354 ret = mbedtls_md( mbedtls_md_info_from_type(
6355 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6356 start, len,
6357 ssl->session_negotiate->peer_cert_digest );
6358
6359 ssl->session_negotiate->peer_cert_digest_type =
6360 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6361 ssl->session_negotiate->peer_cert_digest_len =
6362 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6363
6364 return( ret );
6365}
6366
6367static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6368 unsigned char *start, size_t len )
6369{
6370 unsigned char *end = start + len;
6371 int ret;
6372
6373 /* Make a copy of the peer's raw public key. */
6374 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6375 ret = mbedtls_pk_parse_subpubkey( &start, end,
6376 &ssl->handshake->peer_pubkey );
6377 if( ret != 0 )
6378 {
6379 /* We should have parsed the public key before. */
6380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6381 }
6382
6383 return( 0 );
6384}
6385#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6386
Hanno Becker68636192019-02-05 14:36:34 +00006387int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6388{
6389 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006390 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006391#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6392 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6393 ? ssl->handshake->sni_authmode
6394 : ssl->conf->authmode;
6395#else
6396 const int authmode = ssl->conf->authmode;
6397#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006398 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006399 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006400
6401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6402
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006403 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6404 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006405 {
6406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006407 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006408 }
6409
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006410#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6411 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006412 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006413 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006414 chain = ssl->handshake->ecrs_peer_cert;
6415 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006416 goto crt_verify;
6417 }
6418#endif
6419
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006420 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006421 {
6422 /* mbedtls_ssl_read_record may have sent an alert already. We
6423 let it decide whether to alert. */
6424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006425 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006426 }
6427
Hanno Becker4a55f632019-02-05 12:49:06 +00006428#if defined(MBEDTLS_SSL_SRV_C)
6429 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6430 {
6431 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006432
6433 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006434 ret = 0;
6435 else
6436 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006437
Hanno Becker6bdfab22019-02-05 13:11:17 +00006438 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006439 }
6440#endif /* MBEDTLS_SSL_SRV_C */
6441
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006442 /* Clear existing peer CRT structure in case we tried to
6443 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006444 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006445
6446 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6447 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006448 {
6449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6450 sizeof( mbedtls_x509_crt ) ) );
6451 mbedtls_ssl_send_alert_message( ssl,
6452 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6453 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006454
Hanno Becker3dad3112019-02-05 17:19:52 +00006455 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6456 goto exit;
6457 }
6458 mbedtls_x509_crt_init( chain );
6459
6460 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006461 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006462 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006463
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006464#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6465 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006466 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006467
6468crt_verify:
6469 if( ssl->handshake->ecrs_enabled)
6470 rs_ctx = &ssl->handshake->ecrs_ctx;
6471#endif
6472
Hanno Becker68636192019-02-05 14:36:34 +00006473 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006474 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006475 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006476 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006477
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006478#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006479 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006480 unsigned char *crt_start, *pk_start;
6481 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006482
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006483 /* We parse the CRT chain without copying, so
6484 * these pointers point into the input buffer,
6485 * and are hence still valid after freeing the
6486 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006487
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006488 crt_start = chain->raw.p;
6489 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006490
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006491 pk_start = chain->pk_raw.p;
6492 pk_len = chain->pk_raw.len;
6493
6494 /* Free the CRT structures before computing
6495 * digest and copying the peer's public key. */
6496 mbedtls_x509_crt_free( chain );
6497 mbedtls_free( chain );
6498 chain = NULL;
6499
6500 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006501 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006502 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006503
6504 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6505 if( ret != 0 )
6506 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006507 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006508#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6509 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006510 ssl->session_negotiate->peer_cert = chain;
6511 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006512#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
Hanno Becker6bdfab22019-02-05 13:11:17 +00006516exit:
6517
Hanno Becker3dad3112019-02-05 17:19:52 +00006518 if( ret == 0 )
6519 ssl->state++;
6520
6521#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6522 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6523 {
6524 ssl->handshake->ecrs_peer_cert = chain;
6525 chain = NULL;
6526 }
6527#endif
6528
6529 if( chain != NULL )
6530 {
6531 mbedtls_x509_crt_free( chain );
6532 mbedtls_free( chain );
6533 }
6534
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 return( ret );
6536}
Hanno Becker21489932019-02-05 13:20:55 +00006537#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006540{
6541 int ret;
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 ssl->out_msglen = 1;
6547 ssl->out_msg[0] = 1;
6548
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 ssl->state++;
6550
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006551 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006554 return( ret );
6555 }
6556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
6559 return( 0 );
6560}
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006563{
6564 int ret;
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
Hanno Becker327c93b2018-08-15 13:56:18 +01006568 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 return( ret );
6572 }
6573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006577 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6578 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 }
6581
Hanno Beckere678eaa2018-08-21 14:57:46 +01006582 /* CCS records are only accepted if they have length 1 and content '1',
6583 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006585 /*
6586 * Switch to our negotiated transform and session parameters for inbound
6587 * data.
6588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006590 ssl->transform_in = ssl->transform_negotiate;
6591 ssl->session_in = ssl->session_negotiate;
6592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006594 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006597 ssl_dtls_replay_reset( ssl );
6598#endif
6599
6600 /* Increment epoch */
6601 if( ++ssl->in_epoch == 0 )
6602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006604 /* This is highly unlikely to happen for legitimate reasons, so
6605 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006607 }
6608 }
6609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006611 memset( ssl->in_ctr, 0, 8 );
6612
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006613 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6616 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006621 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6622 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006624 }
6625 }
6626#endif
6627
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 ssl->state++;
6629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006631
6632 return( 0 );
6633}
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6636 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006637{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006638 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6641 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6642 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006643 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006644 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6647#if defined(MBEDTLS_SHA512_C)
6648 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006649 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6650 else
6651#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SHA256_C)
6653 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006654 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006655 else
6656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006660 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006661 }
Paul Bakker380da532012-04-18 16:10:25 +00006662}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6667 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006668 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6669 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6672#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006673#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006674 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006675 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6676#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006677 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006678#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006681#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006682 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006683 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006684#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006685 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006686#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006687#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006689}
6690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006692 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006696 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6697 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6700#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006701#if defined(MBEDTLS_USE_PSA_CRYPTO)
6702 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6703#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006704 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006705#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006709 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006710#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006711 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006712#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006715}
6716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6718 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6719static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006720 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006721{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006722 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6723 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006724}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006725#endif
Paul Bakker380da532012-04-18 16:10:25 +00006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6728#if defined(MBEDTLS_SHA256_C)
6729static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006730 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006731{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006732#if defined(MBEDTLS_USE_PSA_CRYPTO)
6733 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6734#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006735 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006736#endif
Paul Bakker380da532012-04-18 16:10:25 +00006737}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006738#endif
Paul Bakker380da532012-04-18 16:10:25 +00006739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SHA512_C)
6741static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006742 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006743{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006744#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006745 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006746#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006747 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006748#endif
Paul Bakker380da532012-04-18 16:10:25 +00006749}
Paul Bakker769075d2012-11-24 11:26:46 +01006750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006754static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006757 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006758 mbedtls_md5_context md5;
6759 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006760
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 unsigned char padbuf[48];
6762 unsigned char md5sum[16];
6763 unsigned char sha1sum[20];
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006766 if( !session )
6767 session = ssl->session;
6768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006770
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006771 mbedtls_md5_init( &md5 );
6772 mbedtls_sha1_init( &sha1 );
6773
6774 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6775 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776
6777 /*
6778 * SSLv3:
6779 * hash =
6780 * MD5( master + pad2 +
6781 * MD5( handshake + sender + master + pad1 ) )
6782 * + SHA1( master + pad2 +
6783 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 */
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006787 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6788 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006792 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6793 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006794#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006797 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006798
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006801 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6802 mbedtls_md5_update_ret( &md5, session->master, 48 );
6803 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6804 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006806 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6807 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6808 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6809 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006813 mbedtls_md5_starts_ret( &md5 );
6814 mbedtls_md5_update_ret( &md5, session->master, 48 );
6815 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6816 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6817 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006819 mbedtls_sha1_starts_ret( &sha1 );
6820 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6821 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6822 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6823 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006827 mbedtls_md5_free( &md5 );
6828 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006830 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6831 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6832 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006841{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006842 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006843 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006844 mbedtls_md5_context md5;
6845 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006846 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006849 if( !session )
6850 session = ssl->session;
6851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006854 mbedtls_md5_init( &md5 );
6855 mbedtls_sha1_init( &sha1 );
6856
6857 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6858 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Paul Bakker1ef83d62012-04-11 12:09:53 +00006860 /*
6861 * TLSv1:
6862 * hash = PRF( master, finished_label,
6863 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6864 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006867 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6868 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006869#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006872 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6873 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006874#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006877 ? "client finished"
6878 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006879
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006880 mbedtls_md5_finish_ret( &md5, padbuf );
6881 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006882
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006883 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006884 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006888 mbedtls_md5_free( &md5 );
6889 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006890
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006891 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6898#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006899static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006901{
6902 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006903 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006905#if defined(MBEDTLS_USE_PSA_CRYPTO)
6906 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006907 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006908 psa_status_t status;
6909#else
6910 mbedtls_sha256_context sha256;
6911#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006914 if( !session )
6915 session = ssl->session;
6916
Andrzej Kurekeb342242019-01-29 09:14:33 -05006917 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6918 ? "client finished"
6919 : "server finished";
6920
6921#if defined(MBEDTLS_USE_PSA_CRYPTO)
6922 sha256_psa = psa_hash_operation_init();
6923
6924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6925
6926 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6927 if( status != PSA_SUCCESS )
6928 {
6929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6930 return;
6931 }
6932
6933 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6934 if( status != PSA_SUCCESS )
6935 {
6936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6937 return;
6938 }
6939 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6940#else
6941
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006942 mbedtls_sha256_init( &sha256 );
6943
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006945
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006946 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006947
6948 /*
6949 * TLSv1.2:
6950 * hash = PRF( master, finished_label,
6951 * Hash( handshake ) )[0.11]
6952 */
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if !defined(MBEDTLS_SHA256_ALT)
6955 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006956 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006957#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006959 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006960 mbedtls_sha256_free( &sha256 );
6961#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006962
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006963 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006964 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006967
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006968 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006975static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977{
6978 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006979 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006980 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006981#if defined(MBEDTLS_USE_PSA_CRYPTO)
6982 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006983 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006984 psa_status_t status;
6985#else
6986 mbedtls_sha512_context sha512;
6987#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006990 if( !session )
6991 session = ssl->session;
6992
Andrzej Kurekeb342242019-01-29 09:14:33 -05006993 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6994 ? "client finished"
6995 : "server finished";
6996
6997#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006998 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006999
7000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7001
Andrzej Kurek972fba52019-01-30 03:29:12 -05007002 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007003 if( status != PSA_SUCCESS )
7004 {
7005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7006 return;
7007 }
7008
Andrzej Kurek972fba52019-01-30 03:29:12 -05007009 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007010 if( status != PSA_SUCCESS )
7011 {
7012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7013 return;
7014 }
7015 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7016#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007017 mbedtls_sha512_init( &sha512 );
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007020
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007021 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007022
7023 /*
7024 * TLSv1.2:
7025 * hash = PRF( master, finished_label,
7026 * Hash( handshake ) )[0.11]
7027 */
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007030 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7031 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007032#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007033
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007034 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007035 mbedtls_sha512_free( &sha512 );
7036#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007037
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007038 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007039 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007042
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007043 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007046}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#endif /* MBEDTLS_SHA512_C */
7048#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007053
7054 /*
7055 * Free our handshake params
7056 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007057 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007059 ssl->handshake = NULL;
7060
7061 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007062 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007063 */
7064 if( ssl->transform )
7065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 mbedtls_ssl_transform_free( ssl->transform );
7067 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007068 }
7069 ssl->transform = ssl->transform_negotiate;
7070 ssl->transform_negotiate = NULL;
7071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007073}
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007076{
7077 int resume = ssl->handshake->resume;
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_RENEGOTIATION)
7082 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007085 ssl->renego_records_seen = 0;
7086 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007087#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007088
7089 /*
7090 * Free the previous session and switch in the current one
7091 */
Paul Bakker0a597072012-09-25 21:55:46 +00007092 if( ssl->session )
7093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007095 /* RFC 7366 3.1: keep the EtM state */
7096 ssl->session_negotiate->encrypt_then_mac =
7097 ssl->session->encrypt_then_mac;
7098#endif
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 mbedtls_ssl_session_free( ssl->session );
7101 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007102 }
7103 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007104 ssl->session_negotiate = NULL;
7105
Paul Bakker0a597072012-09-25 21:55:46 +00007106 /*
7107 * Add cache entry
7108 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007109 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007110 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007111 resume == 0 )
7112 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007113 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007115 }
Paul Bakker0a597072012-09-25 21:55:46 +00007116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007118 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007119 ssl->handshake->flight != NULL )
7120 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007121 /* Cancel handshake timer */
7122 ssl_set_timer( ssl, 0 );
7123
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007124 /* Keep last flight around in case we need to resend it:
7125 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007127 }
7128 else
7129#endif
7130 ssl_handshake_wrapup_free_hs_transform( ssl );
7131
Paul Bakker48916f92012-09-16 19:57:18 +00007132 ssl->state++;
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007135}
7136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007139 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007143 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007144
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007145 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007147 /*
7148 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7149 * may define some other value. Currently (early 2016), no defined
7150 * ciphersuite does this (and this is unlikely to change as activity has
7151 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007156 ssl->verify_data_len = hash_len;
7157 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007158#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007159
Paul Bakker5121ce52009-01-03 21:22:43 +00007160 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7162 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007163
7164 /*
7165 * In case of session resuming, invert the client and server
7166 * ChangeCipherSpec messages order.
7167 */
Paul Bakker0a597072012-09-25 21:55:46 +00007168 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007177#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 }
7179 else
7180 ssl->state++;
7181
Paul Bakker48916f92012-09-16 19:57:18 +00007182 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007183 * Switch to our negotiated transform and session parameters for outbound
7184 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007189 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007190 {
7191 unsigned char i;
7192
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007193 /* Remember current epoch settings for resending */
7194 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007195 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007196
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007197 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007198 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007199
7200 /* Increment epoch */
7201 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007202 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007203 break;
7204
7205 /* The loop goes to its end iff the counter is wrapping */
7206 if( i == 0 )
7207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7209 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007210 }
7211 }
7212 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007214 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007215
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007216 ssl->transform_out = ssl->transform_negotiate;
7217 ssl->session_out = ssl->session_negotiate;
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7220 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7225 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007226 }
7227 }
7228#endif
7229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007233#endif
7234
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007235 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007236 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238 return( ret );
7239 }
7240
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007241#if defined(MBEDTLS_SSL_PROTO_DTLS)
7242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7243 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7244 {
7245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7246 return( ret );
7247 }
7248#endif
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
7252 return( 0 );
7253}
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007256#define SSL_MAX_HASH_LEN 36
7257#else
7258#define SSL_MAX_HASH_LEN 12
7259#endif
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007262{
Paul Bakker23986e52011-04-24 08:57:21 +00007263 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007264 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007265 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007268
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007269 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007270
Hanno Becker327c93b2018-08-15 13:56:18 +01007271 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 return( ret );
7275 }
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007280 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7281 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 }
7284
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007285 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_SSL_PROTO_SSL3)
7287 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007288 hash_len = 36;
7289 else
7290#endif
7291 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7294 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7298 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007300 }
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007303 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007306 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7307 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 }
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007312 ssl->verify_data_len = hash_len;
7313 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007314#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007315
Paul Bakker0a597072012-09-25 21:55:46 +00007316 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007319 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007326 }
7327 else
7328 ssl->state++;
7329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007333#endif
7334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007336
7337 return( 0 );
7338}
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7345 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7346 mbedtls_md5_init( &handshake->fin_md5 );
7347 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007348 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7349 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7352#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007353#if defined(MBEDTLS_USE_PSA_CRYPTO)
7354 handshake->fin_sha256_psa = psa_hash_operation_init();
7355 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7356#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007358 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007362#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007363 handshake->fin_sha384_psa = psa_hash_operation_init();
7364 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007365#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007367 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007368#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007371
7372 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007373
7374#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7375 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7376 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7377#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#if defined(MBEDTLS_DHM_C)
7380 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382#if defined(MBEDTLS_ECDH_C)
7383 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007384#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007385#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007386 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007387#if defined(MBEDTLS_SSL_CLI_C)
7388 handshake->ecjpake_cache = NULL;
7389 handshake->ecjpake_cache_len = 0;
7390#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007391#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007392
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007393#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007394 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007395#endif
7396
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007397#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7398 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7399#endif
Hanno Becker75173122019-02-06 16:18:31 +00007400
7401#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7402 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7403 mbedtls_pk_init( &handshake->peer_pubkey );
7404#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007405}
7406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7412 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 mbedtls_md_init( &transform->md_ctx_enc );
7415 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007416}
7417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007421}
7422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007424{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007425 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007426 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007428 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007430 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007431 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007432
7433 /*
7434 * Either the pointers are now NULL or cleared properly and can be freed.
7435 * Now allocate missing structures.
7436 */
7437 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007438 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007439 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007440 }
Paul Bakker48916f92012-09-16 19:57:18 +00007441
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007442 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007443 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007444 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007445 }
Paul Bakker48916f92012-09-16 19:57:18 +00007446
Paul Bakker82788fb2014-10-20 13:59:19 +02007447 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007448 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007449 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007450 }
Paul Bakker48916f92012-09-16 19:57:18 +00007451
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007452 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007453 if( ssl->handshake == NULL ||
7454 ssl->transform_negotiate == NULL ||
7455 ssl->session_negotiate == NULL )
7456 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 mbedtls_free( ssl->handshake );
7460 mbedtls_free( ssl->transform_negotiate );
7461 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007462
7463 ssl->handshake = NULL;
7464 ssl->transform_negotiate = NULL;
7465 ssl->session_negotiate = NULL;
7466
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007467 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007468 }
7469
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007470 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007472 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007473 ssl_handshake_params_init( ssl->handshake );
7474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007476 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7477 {
7478 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007479
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007480 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7481 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7482 else
7483 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007484
7485 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007486 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007487#endif
7488
Paul Bakker48916f92012-09-16 19:57:18 +00007489 return( 0 );
7490}
7491
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007492#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007493/* Dummy cookie callbacks for defaults */
7494static int ssl_cookie_write_dummy( void *ctx,
7495 unsigned char **p, unsigned char *end,
7496 const unsigned char *cli_id, size_t cli_id_len )
7497{
7498 ((void) ctx);
7499 ((void) p);
7500 ((void) end);
7501 ((void) cli_id);
7502 ((void) cli_id_len);
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007505}
7506
7507static int ssl_cookie_check_dummy( void *ctx,
7508 const unsigned char *cookie, size_t cookie_len,
7509 const unsigned char *cli_id, size_t cli_id_len )
7510{
7511 ((void) ctx);
7512 ((void) cookie);
7513 ((void) cookie_len);
7514 ((void) cli_id);
7515 ((void) cli_id_len);
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007518}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007519#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007520
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007521/* Once ssl->out_hdr as the address of the beginning of the
7522 * next outgoing record is set, deduce the other pointers.
7523 *
7524 * Note: For TLS, we save the implicit record sequence number
7525 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7526 * and the caller has to make sure there's space for this.
7527 */
7528
7529static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7530 mbedtls_ssl_transform *transform )
7531{
7532#if defined(MBEDTLS_SSL_PROTO_DTLS)
7533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7534 {
7535 ssl->out_ctr = ssl->out_hdr + 3;
7536 ssl->out_len = ssl->out_hdr + 11;
7537 ssl->out_iv = ssl->out_hdr + 13;
7538 }
7539 else
7540#endif
7541 {
7542 ssl->out_ctr = ssl->out_hdr - 8;
7543 ssl->out_len = ssl->out_hdr + 3;
7544 ssl->out_iv = ssl->out_hdr + 5;
7545 }
7546
7547 /* Adjust out_msg to make space for explicit IV, if used. */
7548 if( transform != NULL &&
7549 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7550 {
7551 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7552 }
7553 else
7554 ssl->out_msg = ssl->out_iv;
7555}
7556
7557/* Once ssl->in_hdr as the address of the beginning of the
7558 * next incoming record is set, deduce the other pointers.
7559 *
7560 * Note: For TLS, we save the implicit record sequence number
7561 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7562 * and the caller has to make sure there's space for this.
7563 */
7564
7565static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7566 mbedtls_ssl_transform *transform )
7567{
7568#if defined(MBEDTLS_SSL_PROTO_DTLS)
7569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7570 {
7571 ssl->in_ctr = ssl->in_hdr + 3;
7572 ssl->in_len = ssl->in_hdr + 11;
7573 ssl->in_iv = ssl->in_hdr + 13;
7574 }
7575 else
7576#endif
7577 {
7578 ssl->in_ctr = ssl->in_hdr - 8;
7579 ssl->in_len = ssl->in_hdr + 3;
7580 ssl->in_iv = ssl->in_hdr + 5;
7581 }
7582
7583 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7584 if( transform != NULL &&
7585 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7586 {
7587 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7588 }
7589 else
7590 ssl->in_msg = ssl->in_iv;
7591}
7592
Paul Bakker5121ce52009-01-03 21:22:43 +00007593/*
7594 * Initialize an SSL context
7595 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007596void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7597{
7598 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7599}
7600
7601/*
7602 * Setup an SSL context
7603 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007604
7605static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7606{
7607 /* Set the incoming and outgoing record pointers. */
7608#if defined(MBEDTLS_SSL_PROTO_DTLS)
7609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7610 {
7611 ssl->out_hdr = ssl->out_buf;
7612 ssl->in_hdr = ssl->in_buf;
7613 }
7614 else
7615#endif /* MBEDTLS_SSL_PROTO_DTLS */
7616 {
7617 ssl->out_hdr = ssl->out_buf + 8;
7618 ssl->in_hdr = ssl->in_buf + 8;
7619 }
7620
7621 /* Derive other internal pointers. */
7622 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7623 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7624}
7625
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007626int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007627 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007628{
Paul Bakker48916f92012-09-16 19:57:18 +00007629 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007630
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007631 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007632
7633 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007634 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007635 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007636
7637 /* Set to NULL in case of an error condition */
7638 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007639
Angus Grattond8213d02016-05-25 20:56:48 +10007640 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7641 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007642 {
Angus Grattond8213d02016-05-25 20:56:48 +10007643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007644 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007645 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007646 }
7647
7648 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7649 if( ssl->out_buf == NULL )
7650 {
7651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007652 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007653 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007654 }
7655
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007656 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007657
Paul Bakker48916f92012-09-16 19:57:18 +00007658 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007659 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007660
7661 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007662
7663error:
7664 mbedtls_free( ssl->in_buf );
7665 mbedtls_free( ssl->out_buf );
7666
7667 ssl->conf = NULL;
7668
7669 ssl->in_buf = NULL;
7670 ssl->out_buf = NULL;
7671
7672 ssl->in_hdr = NULL;
7673 ssl->in_ctr = NULL;
7674 ssl->in_len = NULL;
7675 ssl->in_iv = NULL;
7676 ssl->in_msg = NULL;
7677
7678 ssl->out_hdr = NULL;
7679 ssl->out_ctr = NULL;
7680 ssl->out_len = NULL;
7681 ssl->out_iv = NULL;
7682 ssl->out_msg = NULL;
7683
k-stachowiak9f7798e2018-07-31 16:52:32 +02007684 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007685}
7686
7687/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007688 * Reset an initialized and used SSL context for re-use while retaining
7689 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007690 *
7691 * If partial is non-zero, keep data in the input buffer and client ID.
7692 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007693 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007694static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007695{
Paul Bakker48916f92012-09-16 19:57:18 +00007696 int ret;
7697
Hanno Becker7e772132018-08-10 12:38:21 +01007698#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7699 !defined(MBEDTLS_SSL_SRV_C)
7700 ((void) partial);
7701#endif
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007704
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007705 /* Cancel any possibly running timer */
7706 ssl_set_timer( ssl, 0 );
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_RENEGOTIATION)
7709 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007710 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007711
7712 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7714 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007717
Paul Bakker7eb013f2011-10-06 12:37:39 +00007718 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007719 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007720
7721 ssl->in_msgtype = 0;
7722 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007724 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007725 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007726#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007728 ssl_dtls_replay_reset( ssl );
7729#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007730
7731 ssl->in_hslen = 0;
7732 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007733
7734 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007735
7736 ssl->out_msgtype = 0;
7737 ssl->out_msglen = 0;
7738 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7740 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007741 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007742#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007743
Hanno Becker19859472018-08-06 09:40:20 +01007744 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7745
Paul Bakker48916f92012-09-16 19:57:18 +00007746 ssl->transform_in = NULL;
7747 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007748
Hanno Becker78640902018-08-13 16:35:15 +01007749 ssl->session_in = NULL;
7750 ssl->session_out = NULL;
7751
Angus Grattond8213d02016-05-25 20:56:48 +10007752 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007753
7754#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007755 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007756#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7757 {
7758 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007759 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007760 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7763 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7766 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7769 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007770 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007771 }
7772#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007773
Paul Bakker48916f92012-09-16 19:57:18 +00007774 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 mbedtls_ssl_transform_free( ssl->transform );
7777 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007778 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007779 }
Paul Bakker48916f92012-09-16 19:57:18 +00007780
Paul Bakkerc0463502013-02-14 11:19:38 +01007781 if( ssl->session )
7782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 mbedtls_ssl_session_free( ssl->session );
7784 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007785 ssl->session = NULL;
7786 }
7787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007789 ssl->alpn_chosen = NULL;
7790#endif
7791
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007792#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007793#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007794 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007795#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007796 {
7797 mbedtls_free( ssl->cli_id );
7798 ssl->cli_id = NULL;
7799 ssl->cli_id_len = 0;
7800 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007801#endif
7802
Paul Bakker48916f92012-09-16 19:57:18 +00007803 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7804 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007805
7806 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007807}
7808
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007809/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007810 * Reset an initialized and used SSL context for re-use while retaining
7811 * all application-set variables, function pointers and data.
7812 */
7813int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7814{
7815 return( ssl_session_reset_int( ssl, 0 ) );
7816}
7817
7818/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007819 * SSL set accessors
7820 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007821void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007823 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007824}
7825
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007826void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007827{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007828 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007829}
7830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007832void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007833{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007834 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007835}
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007839void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007840{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007841 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007842}
7843#endif
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007846
Hanno Becker1841b0a2018-08-24 11:13:57 +01007847void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7848 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007849{
7850 ssl->disable_datagram_packing = !allow_packing;
7851}
7852
7853void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7854 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007855{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007856 conf->hs_timeout_min = min;
7857 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007858}
7859#endif
7860
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007861void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007863 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007864}
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007867void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007868 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007869 void *p_vrfy )
7870{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007871 conf->f_vrfy = f_vrfy;
7872 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007875
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007876void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007877 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 void *p_rng )
7879{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007880 conf->f_rng = f_rng;
7881 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007882}
7883
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007884void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007885 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 void *p_dbg )
7887{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007888 conf->f_dbg = f_dbg;
7889 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007893 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007894 mbedtls_ssl_send_t *f_send,
7895 mbedtls_ssl_recv_t *f_recv,
7896 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007897{
7898 ssl->p_bio = p_bio;
7899 ssl->f_send = f_send;
7900 ssl->f_recv = f_recv;
7901 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007902}
7903
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007904#if defined(MBEDTLS_SSL_PROTO_DTLS)
7905void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7906{
7907 ssl->mtu = mtu;
7908}
7909#endif
7910
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007911void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007912{
7913 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007914}
7915
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007916void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7917 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007918 mbedtls_ssl_set_timer_t *f_set_timer,
7919 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007920{
7921 ssl->p_timer = p_timer;
7922 ssl->f_set_timer = f_set_timer;
7923 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007924
7925 /* Make sure we start with no timer running */
7926 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007927}
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007930void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007931 void *p_cache,
7932 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7933 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007934{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007935 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007936 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007937 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_CLI_C)
7942int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007943{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007944 int ret;
7945
7946 if( ssl == NULL ||
7947 session == NULL ||
7948 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007949 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007952 }
7953
Hanno Becker52055ae2019-02-06 14:30:46 +00007954 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7955 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007956 return( ret );
7957
Paul Bakker0a597072012-09-25 21:55:46 +00007958 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007959
7960 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007963
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007964void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007965 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7968 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7969 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7970 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007971}
7972
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007973void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007974 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007975 int major, int minor )
7976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007978 return;
7979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007981 return;
7982
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007983 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007984}
7985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007987void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007988 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007989{
7990 conf->cert_profile = profile;
7991}
7992
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007993/* Append a new keycert entry to a (possibly empty) list */
7994static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7995 mbedtls_x509_crt *cert,
7996 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007997{
niisato8ee24222018-06-25 19:05:48 +09007998 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007999
niisato8ee24222018-06-25 19:05:48 +09008000 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8001 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008002 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008003
niisato8ee24222018-06-25 19:05:48 +09008004 new_cert->cert = cert;
8005 new_cert->key = key;
8006 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008007
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008008 /* Update head is the list was null, else add to the end */
8009 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008010 {
niisato8ee24222018-06-25 19:05:48 +09008011 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008012 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008013 else
8014 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008015 mbedtls_ssl_key_cert *cur = *head;
8016 while( cur->next != NULL )
8017 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008018 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008019 }
8020
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008021 return( 0 );
8022}
8023
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008024int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008025 mbedtls_x509_crt *own_cert,
8026 mbedtls_pk_context *pk_key )
8027{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008028 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008029}
8030
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008031void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008032 mbedtls_x509_crt *ca_chain,
8033 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008034{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008035 conf->ca_chain = ca_chain;
8036 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008037
8038#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8039 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8040 * cannot be used together. */
8041 conf->f_ca_cb = NULL;
8042 conf->p_ca_cb = NULL;
8043#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008044}
Hanno Becker5adaad92019-03-27 16:54:37 +00008045
8046#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8047void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008048 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008049 void *p_ca_cb )
8050{
8051 conf->f_ca_cb = f_ca_cb;
8052 conf->p_ca_cb = p_ca_cb;
8053
8054 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8055 * cannot be used together. */
8056 conf->ca_chain = NULL;
8057 conf->ca_crl = NULL;
8058}
8059#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008061
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008062#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8063int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8064 mbedtls_x509_crt *own_cert,
8065 mbedtls_pk_context *pk_key )
8066{
8067 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8068 own_cert, pk_key ) );
8069}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008070
8071void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8072 mbedtls_x509_crt *ca_chain,
8073 mbedtls_x509_crl *ca_crl )
8074{
8075 ssl->handshake->sni_ca_chain = ca_chain;
8076 ssl->handshake->sni_ca_crl = ca_crl;
8077}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008078
8079void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8080 int authmode )
8081{
8082 ssl->handshake->sni_authmode = authmode;
8083}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008084#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8085
Hanno Becker8927c832019-04-03 12:52:50 +01008086#if defined(MBEDTLS_X509_CRT_PARSE_C)
8087void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8088 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8089 void *p_vrfy )
8090{
8091 ssl->f_vrfy = f_vrfy;
8092 ssl->p_vrfy = p_vrfy;
8093}
8094#endif
8095
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008096#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008097/*
8098 * Set EC J-PAKE password for current handshake
8099 */
8100int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8101 const unsigned char *pw,
8102 size_t pw_len )
8103{
8104 mbedtls_ecjpake_role role;
8105
Janos Follath8eb64132016-06-03 15:40:57 +01008106 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8108
8109 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8110 role = MBEDTLS_ECJPAKE_SERVER;
8111 else
8112 role = MBEDTLS_ECJPAKE_CLIENT;
8113
8114 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8115 role,
8116 MBEDTLS_MD_SHA256,
8117 MBEDTLS_ECP_DP_SECP256R1,
8118 pw, pw_len ) );
8119}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008120#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008123
8124static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8125{
8126 /* Remove reference to existing PSK, if any. */
8127#if defined(MBEDTLS_USE_PSA_CRYPTO)
8128 if( conf->psk_opaque != 0 )
8129 {
8130 /* The maintenance of the PSK key slot is the
8131 * user's responsibility. */
8132 conf->psk_opaque = 0;
8133 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008134 /* This and the following branch should never
8135 * be taken simultaenously as we maintain the
8136 * invariant that raw and opaque PSKs are never
8137 * configured simultaneously. As a safeguard,
8138 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008139#endif /* MBEDTLS_USE_PSA_CRYPTO */
8140 if( conf->psk != NULL )
8141 {
8142 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8143
8144 mbedtls_free( conf->psk );
8145 conf->psk = NULL;
8146 conf->psk_len = 0;
8147 }
8148
8149 /* Remove reference to PSK identity, if any. */
8150 if( conf->psk_identity != NULL )
8151 {
8152 mbedtls_free( conf->psk_identity );
8153 conf->psk_identity = NULL;
8154 conf->psk_identity_len = 0;
8155 }
8156}
8157
Hanno Becker7390c712018-11-15 13:33:04 +00008158/* This function assumes that PSK identity in the SSL config is unset.
8159 * It checks that the provided identity is well-formed and attempts
8160 * to make a copy of it in the SSL config.
8161 * On failure, the PSK identity in the config remains unset. */
8162static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8163 unsigned char const *psk_identity,
8164 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008165{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008166 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008167 if( psk_identity == NULL ||
8168 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008169 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008170 {
8171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8172 }
8173
Hanno Becker7390c712018-11-15 13:33:04 +00008174 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8175 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008176 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008177
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008178 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008179 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008180
8181 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008182}
8183
Hanno Becker7390c712018-11-15 13:33:04 +00008184int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8185 const unsigned char *psk, size_t psk_len,
8186 const unsigned char *psk_identity, size_t psk_identity_len )
8187{
8188 int ret;
8189 /* Remove opaque/raw PSK + PSK Identity */
8190 ssl_conf_remove_psk( conf );
8191
8192 /* Check and set raw PSK */
8193 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8195 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8196 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8197 conf->psk_len = psk_len;
8198 memcpy( conf->psk, psk, conf->psk_len );
8199
8200 /* Check and set PSK Identity */
8201 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8202 if( ret != 0 )
8203 ssl_conf_remove_psk( conf );
8204
8205 return( ret );
8206}
8207
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008208static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8209{
8210#if defined(MBEDTLS_USE_PSA_CRYPTO)
8211 if( ssl->handshake->psk_opaque != 0 )
8212 {
8213 ssl->handshake->psk_opaque = 0;
8214 }
8215 else
8216#endif /* MBEDTLS_USE_PSA_CRYPTO */
8217 if( ssl->handshake->psk != NULL )
8218 {
8219 mbedtls_platform_zeroize( ssl->handshake->psk,
8220 ssl->handshake->psk_len );
8221 mbedtls_free( ssl->handshake->psk );
8222 ssl->handshake->psk_len = 0;
8223 }
8224}
8225
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008226int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8227 const unsigned char *psk, size_t psk_len )
8228{
8229 if( psk == NULL || ssl->handshake == NULL )
8230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8231
8232 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8234
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008235 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008236
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008237 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008239
8240 ssl->handshake->psk_len = psk_len;
8241 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8242
8243 return( 0 );
8244}
8245
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008246#if defined(MBEDTLS_USE_PSA_CRYPTO)
8247int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008248 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008249 const unsigned char *psk_identity,
8250 size_t psk_identity_len )
8251{
Hanno Becker7390c712018-11-15 13:33:04 +00008252 int ret;
8253 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008254 ssl_conf_remove_psk( conf );
8255
Hanno Becker7390c712018-11-15 13:33:04 +00008256 /* Check and set opaque PSK */
8257 if( psk_slot == 0 )
8258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008259 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008260
8261 /* Check and set PSK Identity */
8262 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8263 psk_identity_len );
8264 if( ret != 0 )
8265 ssl_conf_remove_psk( conf );
8266
8267 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008268}
8269
8270int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008271 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008272{
8273 if( psk_slot == 0 || ssl->handshake == NULL )
8274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8275
8276 ssl_remove_psk( ssl );
8277 ssl->handshake->psk_opaque = psk_slot;
8278 return( 0 );
8279}
8280#endif /* MBEDTLS_USE_PSA_CRYPTO */
8281
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008282void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008284 size_t),
8285 void *p_psk )
8286{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008287 conf->f_psk = f_psk;
8288 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008291
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008292#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008293
8294#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008296{
8297 int ret;
8298
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008299 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8300 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8301 {
8302 mbedtls_mpi_free( &conf->dhm_P );
8303 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008306
8307 return( 0 );
8308}
Hanno Becker470a8c42017-10-04 15:28:46 +01008309#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008310
Hanno Beckera90658f2017-10-04 15:29:08 +01008311int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8312 const unsigned char *dhm_P, size_t P_len,
8313 const unsigned char *dhm_G, size_t G_len )
8314{
8315 int ret;
8316
8317 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8318 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8319 {
8320 mbedtls_mpi_free( &conf->dhm_P );
8321 mbedtls_mpi_free( &conf->dhm_G );
8322 return( ret );
8323 }
8324
8325 return( 0 );
8326}
Paul Bakker5121ce52009-01-03 21:22:43 +00008327
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008328int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008329{
8330 int ret;
8331
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008332 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8333 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8334 {
8335 mbedtls_mpi_free( &conf->dhm_P );
8336 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008337 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008338 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008339
8340 return( 0 );
8341}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008342#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008343
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008344#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8345/*
8346 * Set the minimum length for Diffie-Hellman parameters
8347 */
8348void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8349 unsigned int bitlen )
8350{
8351 conf->dhm_min_bitlen = bitlen;
8352}
8353#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8354
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008355#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008356/*
8357 * Set allowed/preferred hashes for handshake signatures
8358 */
8359void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8360 const int *hashes )
8361{
8362 conf->sig_hashes = hashes;
8363}
Hanno Becker947194e2017-04-07 13:25:49 +01008364#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008365
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008366#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008367/*
8368 * Set the allowed elliptic curves
8369 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008370void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008371 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008373 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008374}
Hanno Becker947194e2017-04-07 13:25:49 +01008375#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008376
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008379{
Hanno Becker947194e2017-04-07 13:25:49 +01008380 /* Initialize to suppress unnecessary compiler warning */
8381 size_t hostname_len = 0;
8382
8383 /* Check if new hostname is valid before
8384 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008385 if( hostname != NULL )
8386 {
8387 hostname_len = strlen( hostname );
8388
8389 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8391 }
8392
8393 /* Now it's clear that we will overwrite the old hostname,
8394 * so we can free it safely */
8395
8396 if( ssl->hostname != NULL )
8397 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008398 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008399 mbedtls_free( ssl->hostname );
8400 }
8401
8402 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008403
Paul Bakker5121ce52009-01-03 21:22:43 +00008404 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008405 {
8406 ssl->hostname = NULL;
8407 }
8408 else
8409 {
8410 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008411 if( ssl->hostname == NULL )
8412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008413
Hanno Becker947194e2017-04-07 13:25:49 +01008414 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008415
Hanno Becker947194e2017-04-07 13:25:49 +01008416 ssl->hostname[hostname_len] = '\0';
8417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008418
8419 return( 0 );
8420}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008421#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008422
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008423#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008424void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008426 const unsigned char *, size_t),
8427 void *p_sni )
8428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008429 conf->f_sni = f_sni;
8430 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008435int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008436{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008437 size_t cur_len, tot_len;
8438 const char **p;
8439
8440 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008441 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8442 * MUST NOT be truncated."
8443 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008444 */
8445 tot_len = 0;
8446 for( p = protos; *p != NULL; p++ )
8447 {
8448 cur_len = strlen( *p );
8449 tot_len += cur_len;
8450
8451 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008453 }
8454
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008455 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008456
8457 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008458}
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008461{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008462 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008465
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008466void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->max_major_ver = major;
8469 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008470}
8471
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008472void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008473{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008474 conf->min_major_ver = major;
8475 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008476}
8477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008479void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008480{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008481 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008482}
8483#endif
8484
Janos Follath088ce432017-04-10 12:42:31 +01008485#if defined(MBEDTLS_SSL_SRV_C)
8486void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8487 char cert_req_ca_list )
8488{
8489 conf->cert_req_ca_list = cert_req_ca_list;
8490}
8491#endif
8492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008494void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008495{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008496 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008497}
8498#endif
8499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008501void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008502{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008503 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008504}
8505#endif
8506
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008507#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008508void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008510 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008511}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008512#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008515int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008518 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008521 }
8522
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008523 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008524
8525 return( 0 );
8526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008530void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008532 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008537void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008538{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008539 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008540}
8541#endif
8542
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008543void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008545 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008546}
8547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008549void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008551 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008552}
8553
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008554void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008556 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008557}
8558
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008559void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008560 const unsigned char period[8] )
8561{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008562 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008567#if defined(MBEDTLS_SSL_CLI_C)
8568void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008569{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008570 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008571}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008572#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008573
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008574#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008575void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8576 mbedtls_ssl_ticket_write_t *f_ticket_write,
8577 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8578 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008579{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008580 conf->f_ticket_write = f_ticket_write;
8581 conf->f_ticket_parse = f_ticket_parse;
8582 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008583}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008586
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008587#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8588void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8589 mbedtls_ssl_export_keys_t *f_export_keys,
8590 void *p_export_keys )
8591{
8592 conf->f_export_keys = f_export_keys;
8593 conf->p_export_keys = p_export_keys;
8594}
8595#endif
8596
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008597#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008598void mbedtls_ssl_conf_async_private_cb(
8599 mbedtls_ssl_config *conf,
8600 mbedtls_ssl_async_sign_t *f_async_sign,
8601 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8602 mbedtls_ssl_async_resume_t *f_async_resume,
8603 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008604 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008605{
8606 conf->f_async_sign_start = f_async_sign;
8607 conf->f_async_decrypt_start = f_async_decrypt;
8608 conf->f_async_resume = f_async_resume;
8609 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008610 conf->p_async_config_data = async_config_data;
8611}
8612
Gilles Peskine8f97af72018-04-26 11:46:10 +02008613void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8614{
8615 return( conf->p_async_config_data );
8616}
8617
Gilles Peskine1febfef2018-04-30 11:54:39 +02008618void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008619{
8620 if( ssl->handshake == NULL )
8621 return( NULL );
8622 else
8623 return( ssl->handshake->user_async_ctx );
8624}
8625
Gilles Peskine1febfef2018-04-30 11:54:39 +02008626void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008627 void *ctx )
8628{
8629 if( ssl->handshake != NULL )
8630 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008631}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008632#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008633
Paul Bakker5121ce52009-01-03 21:22:43 +00008634/*
8635 * SSL get accessors
8636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008638{
8639 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8640}
8641
Hanno Becker8b170a02017-10-10 11:51:19 +01008642int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8643{
8644 /*
8645 * Case A: We're currently holding back
8646 * a message for further processing.
8647 */
8648
8649 if( ssl->keep_current_message == 1 )
8650 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008652 return( 1 );
8653 }
8654
8655 /*
8656 * Case B: Further records are pending in the current datagram.
8657 */
8658
8659#if defined(MBEDTLS_SSL_PROTO_DTLS)
8660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8661 ssl->in_left > ssl->next_record_offset )
8662 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008664 return( 1 );
8665 }
8666#endif /* MBEDTLS_SSL_PROTO_DTLS */
8667
8668 /*
8669 * Case C: A handshake message is being processed.
8670 */
8671
Hanno Becker8b170a02017-10-10 11:51:19 +01008672 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8673 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008675 return( 1 );
8676 }
8677
8678 /*
8679 * Case D: An application data message is being processed
8680 */
8681 if( ssl->in_offt != NULL )
8682 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008684 return( 1 );
8685 }
8686
8687 /*
8688 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008689 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008690 * we implement support for multiple alerts in single records.
8691 */
8692
8693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8694 return( 0 );
8695}
8696
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008697uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008698{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008699 if( ssl->session != NULL )
8700 return( ssl->session->verify_result );
8701
8702 if( ssl->session_negotiate != NULL )
8703 return( ssl->session_negotiate->verify_result );
8704
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008705 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008706}
8707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008709{
Paul Bakker926c8e42013-03-06 10:23:34 +01008710 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008711 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008714}
8715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008720 {
8721 switch( ssl->minor_ver )
8722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008724 return( "DTLSv1.0" );
8725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008727 return( "DTLSv1.2" );
8728
8729 default:
8730 return( "unknown (DTLS)" );
8731 }
8732 }
8733#endif
8734
Paul Bakker43ca69c2011-01-15 17:35:19 +00008735 switch( ssl->minor_ver )
8736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008738 return( "SSLv3.0" );
8739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008741 return( "TLSv1.0" );
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008744 return( "TLSv1.1" );
8745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008747 return( "TLSv1.2" );
8748
Paul Bakker43ca69c2011-01-15 17:35:19 +00008749 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008750 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008751 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008752}
8753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008755{
Hanno Becker3136ede2018-08-17 15:28:19 +01008756 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008758 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008759
Hanno Becker78640902018-08-13 16:35:15 +01008760 if( transform == NULL )
8761 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_ZLIB_SUPPORT)
8764 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8765 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008766#endif
8767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 case MBEDTLS_MODE_GCM:
8771 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008772 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008774 transform_expansion = transform->minlen;
8775 break;
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008778
8779 block_size = mbedtls_cipher_get_block_size(
8780 &transform->cipher_ctx_enc );
8781
Hanno Becker3136ede2018-08-17 15:28:19 +01008782 /* Expansion due to the addition of the MAC. */
8783 transform_expansion += transform->maclen;
8784
8785 /* Expansion due to the addition of CBC padding;
8786 * Theoretically up to 256 bytes, but we never use
8787 * more than the block size of the underlying cipher. */
8788 transform_expansion += block_size;
8789
8790 /* For TLS 1.1 or higher, an explicit IV is added
8791 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008792#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8793 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008794 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008795#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008796
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008797 break;
8798
8799 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008802 }
8803
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008804 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008805}
8806
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008807#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8808size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8809{
8810 size_t max_len;
8811
8812 /*
8813 * Assume mfl_code is correct since it was checked when set
8814 */
Angus Grattond8213d02016-05-25 20:56:48 +10008815 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008816
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008817 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008818 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008819 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008820 {
Angus Grattond8213d02016-05-25 20:56:48 +10008821 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008822 }
8823
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008824 /* During a handshake, use the value being negotiated */
8825 if( ssl->session_negotiate != NULL &&
8826 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8827 {
8828 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8829 }
8830
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008831 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008832}
8833#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8834
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008835#if defined(MBEDTLS_SSL_PROTO_DTLS)
8836static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8837{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008838 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8839 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8840 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8841 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8842 return ( 0 );
8843
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008844 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8845 return( ssl->mtu );
8846
8847 if( ssl->mtu == 0 )
8848 return( ssl->handshake->mtu );
8849
8850 return( ssl->mtu < ssl->handshake->mtu ?
8851 ssl->mtu : ssl->handshake->mtu );
8852}
8853#endif /* MBEDTLS_SSL_PROTO_DTLS */
8854
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008855int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8856{
8857 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8858
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008859#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8860 !defined(MBEDTLS_SSL_PROTO_DTLS)
8861 (void) ssl;
8862#endif
8863
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008864#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8865 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8866
8867 if( max_len > mfl )
8868 max_len = mfl;
8869#endif
8870
8871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008872 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008873 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008874 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008875 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8876 const size_t overhead = (size_t) ret;
8877
8878 if( ret < 0 )
8879 return( ret );
8880
8881 if( mtu <= overhead )
8882 {
8883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8884 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8885 }
8886
8887 if( max_len > mtu - overhead )
8888 max_len = mtu - overhead;
8889 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008890#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008891
Hanno Becker0defedb2018-08-10 12:35:02 +01008892#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8893 !defined(MBEDTLS_SSL_PROTO_DTLS)
8894 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008895#endif
8896
8897 return( (int) max_len );
8898}
8899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900#if defined(MBEDTLS_X509_CRT_PARSE_C)
8901const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008902{
8903 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008904 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008905
Hanno Beckere6824572019-02-07 13:18:46 +00008906#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008907 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008908#else
8909 return( NULL );
8910#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008915int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8916 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008917{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008918 if( ssl == NULL ||
8919 dst == NULL ||
8920 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008921 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008924 }
8925
Hanno Becker52055ae2019-02-06 14:30:46 +00008926 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008929
Paul Bakker5121ce52009-01-03 21:22:43 +00008930/*
Paul Bakker1961b702013-01-25 14:49:24 +01008931 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008936
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008937 if( ssl == NULL || ssl->conf == NULL )
8938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008941 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008947#endif
8948
Paul Bakker1961b702013-01-25 14:49:24 +01008949 return( ret );
8950}
8951
8952/*
8953 * Perform the SSL handshake
8954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008956{
8957 int ret = 0;
8958
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008959 if( ssl == NULL || ssl->conf == NULL )
8960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008967
8968 if( ret != 0 )
8969 break;
8970 }
8971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008973
8974 return( ret );
8975}
8976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977#if defined(MBEDTLS_SSL_RENEGOTIATION)
8978#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008979/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008980 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008983{
8984 int ret;
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008987
8988 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8990 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008991
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008992 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008993 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008995 return( ret );
8996 }
8997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008999
9000 return( 0 );
9001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009003
9004/*
9005 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 * - any side: calling mbedtls_ssl_renegotiate(),
9007 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9008 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009009 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009010 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009014{
9015 int ret;
9016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009018
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009019 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9020 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009021
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009022 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9023 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009027 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009028 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009029 ssl->handshake->out_msg_seq = 1;
9030 else
9031 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009032 }
9033#endif
9034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9036 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009041 return( ret );
9042 }
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
9046 return( 0 );
9047}
9048
9049/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009050 * Renegotiate current connection on client,
9051 * or request renegotiation on server
9052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009056
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009057 if( ssl == NULL || ssl->conf == NULL )
9058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009061 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009062 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009068
9069 /* Did we already try/start sending HelloRequest? */
9070 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009072
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009073 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009078 /*
9079 * On client, either start the renegotiation process or,
9080 * if already in progress, continue the handshake
9081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009086
9087 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009090 return( ret );
9091 }
9092 }
9093 else
9094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009098 return( ret );
9099 }
9100 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009102
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009103 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009104}
9105
9106/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009107 * Check record counters and renegotiate if they're above the limit.
9108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009110{
Andres AG2196c7f2016-12-15 17:01:16 +00009111 size_t ep_len = ssl_ep_len( ssl );
9112 int in_ctr_cmp;
9113 int out_ctr_cmp;
9114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9116 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009117 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009118 {
9119 return( 0 );
9120 }
9121
Andres AG2196c7f2016-12-15 17:01:16 +00009122 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9123 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009124 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009125 ssl->conf->renego_period + ep_len, 8 - ep_len );
9126
9127 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009128 {
9129 return( 0 );
9130 }
9131
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009136
9137/*
9138 * Receive application data decrypted from the SSL layer
9139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009141{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009142 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009143 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009144
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009145 if( ssl == NULL || ssl->conf == NULL )
9146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009154 return( ret );
9155
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009156 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009158 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009159 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009160 return( ret );
9161 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009162 }
9163#endif
9164
Hanno Becker4a810fb2017-05-24 16:27:30 +01009165 /*
9166 * Check if renegotiation is necessary and/or handshake is
9167 * in process. If yes, perform/continue, and fall through
9168 * if an unexpected packet is received while the client
9169 * is waiting for the ServerHello.
9170 *
9171 * (There is no equivalent to the last condition on
9172 * the server-side as it is not treated as within
9173 * a handshake while waiting for the ClientHello
9174 * after a renegotiation request.)
9175 */
9176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009178 ret = ssl_check_ctr_renegotiate( ssl );
9179 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9180 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009183 return( ret );
9184 }
9185#endif
9186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009190 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9191 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009194 return( ret );
9195 }
9196 }
9197
Hanno Beckere41158b2017-10-23 13:30:32 +01009198 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009199 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009200 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009201 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009202 if( ssl->f_get_timer != NULL &&
9203 ssl->f_get_timer( ssl->p_timer ) == -1 )
9204 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009205 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009206 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009207
Hanno Becker327c93b2018-08-15 13:56:18 +01009208 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009209 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009210 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9211 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009212
Hanno Becker4a810fb2017-05-24 16:27:30 +01009213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9214 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009215 }
9216
9217 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009219 {
9220 /*
9221 * OpenSSL sends empty messages to randomize the IV
9222 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009223 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009226 return( 0 );
9227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009229 return( ret );
9230 }
9231 }
9232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009236
Hanno Becker4a810fb2017-05-24 16:27:30 +01009237 /*
9238 * - For client-side, expect SERVER_HELLO_REQUEST.
9239 * - For server-side, expect CLIENT_HELLO.
9240 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9241 */
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009244 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009246 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009249
9250 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009253 {
9254 continue;
9255 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009258 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009259#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009260
Hanno Becker4a810fb2017-05-24 16:27:30 +01009261#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009262 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009266
9267 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009270 {
9271 continue;
9272 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009275 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009276#endif /* MBEDTLS_SSL_SRV_C */
9277
Hanno Becker21df7f92017-10-17 11:03:26 +01009278#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009279 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009280 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9281 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9282 ssl->conf->allow_legacy_renegotiation ==
9283 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9284 {
9285 /*
9286 * Accept renegotiation request
9287 */
Paul Bakker48916f92012-09-16 19:57:18 +00009288
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009289 /* DTLS clients need to know renego is server-initiated */
9290#if defined(MBEDTLS_SSL_PROTO_DTLS)
9291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9292 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9293 {
9294 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9295 }
9296#endif
9297 ret = ssl_start_renegotiation( ssl );
9298 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9299 ret != 0 )
9300 {
9301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9302 return( ret );
9303 }
9304 }
9305 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009306#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009307 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009308 /*
9309 * Refuse renegotiation
9310 */
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314#if defined(MBEDTLS_SSL_PROTO_SSL3)
9315 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009316 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009317 /* SSLv3 does not have a "no_renegotiation" warning, so
9318 we send a fatal alert and abort the connection. */
9319 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9320 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9321 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009322 }
9323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9325#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9326 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9327 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9330 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9331 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009332 {
9333 return( ret );
9334 }
Paul Bakker48916f92012-09-16 19:57:18 +00009335 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9338 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009342 }
Paul Bakker48916f92012-09-16 19:57:18 +00009343 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009344
Hanno Becker90333da2017-10-10 11:27:13 +01009345 /* At this point, we don't know whether the renegotiation has been
9346 * completed or not. The cases to consider are the following:
9347 * 1) The renegotiation is complete. In this case, no new record
9348 * has been read yet.
9349 * 2) The renegotiation is incomplete because the client received
9350 * an application data record while awaiting the ServerHello.
9351 * 3) The renegotiation is incomplete because the client received
9352 * a non-handshake, non-application data message while awaiting
9353 * the ServerHello.
9354 * In each of these case, looping will be the proper action:
9355 * - For 1), the next iteration will read a new record and check
9356 * if it's application data.
9357 * - For 2), the loop condition isn't satisfied as application data
9358 * is present, hence continue is the same as break
9359 * - For 3), the loop condition is satisfied and read_record
9360 * will re-deliver the message that was held back by the client
9361 * when expecting the ServerHello.
9362 */
9363 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009364 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009365#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009367 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009368 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009369 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009370 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009373 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009375 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009376 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9381 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009384 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009385 }
9386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9390 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009391 }
9392
9393 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009394
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009395 /* We're going to return something now, cancel timer,
9396 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009398 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009399
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009401 /* If we requested renego but received AppData, resend HelloRequest.
9402 * Do it now, after setting in_offt, to avoid taking this branch
9403 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009405 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009407 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009408 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009411 return( ret );
9412 }
9413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009415#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009416 }
9417
9418 n = ( len < ssl->in_msglen )
9419 ? len : ssl->in_msglen;
9420
9421 memcpy( buf, ssl->in_offt, n );
9422 ssl->in_msglen -= n;
9423
9424 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009425 {
9426 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009427 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009428 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009430 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009431 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009432 /* more data available */
9433 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009437
Paul Bakker23986e52011-04-24 08:57:21 +00009438 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009439}
9440
9441/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009442 * Send application data to be encrypted by the SSL layer, taking care of max
9443 * fragment length and buffer size.
9444 *
9445 * According to RFC 5246 Section 6.2.1:
9446 *
9447 * Zero-length fragments of Application data MAY be sent as they are
9448 * potentially useful as a traffic analysis countermeasure.
9449 *
9450 * Therefore, it is possible that the input message length is 0 and the
9451 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009452 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009453static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009454 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009455{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009456 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9457 const size_t max_len = (size_t) ret;
9458
9459 if( ret < 0 )
9460 {
9461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9462 return( ret );
9463 }
9464
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009465 if( len > max_len )
9466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009471 "maximum fragment length: %d > %d",
9472 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009474 }
9475 else
9476#endif
9477 len = max_len;
9478 }
Paul Bakker887bd502011-06-08 13:10:54 +00009479
Paul Bakker5121ce52009-01-03 21:22:43 +00009480 if( ssl->out_left != 0 )
9481 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009482 /*
9483 * The user has previously tried to send the data and
9484 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9485 * written. In this case, we expect the high-level write function
9486 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009491 return( ret );
9492 }
9493 }
Paul Bakker887bd502011-06-08 13:10:54 +00009494 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009495 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009496 /*
9497 * The user is trying to send a message the first time, so we need to
9498 * copy the data into the internal buffers and setup the data structure
9499 * to keep track of partial writes
9500 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009501 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009503 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009504
Hanno Becker67bc7c32018-08-06 11:33:50 +01009505 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009508 return( ret );
9509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009510 }
9511
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009512 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009513}
9514
9515/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009516 * Write application data, doing 1/n-1 splitting if necessary.
9517 *
9518 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009519 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009520 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009523static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009524 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009525{
9526 int ret;
9527
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009528 if( ssl->conf->cbc_record_splitting ==
9529 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009530 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9532 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9533 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009534 {
9535 return( ssl_write_real( ssl, buf, len ) );
9536 }
9537
9538 if( ssl->split_done == 0 )
9539 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009540 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009541 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009542 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009543 }
9544
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009545 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9546 return( ret );
9547 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009548
9549 return( ret + 1 );
9550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009552
9553/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009554 * Write application data (public-facing wrapper)
9555 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009556int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009557{
9558 int ret;
9559
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009561
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009562 if( ssl == NULL || ssl->conf == NULL )
9563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9564
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009565#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009566 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9567 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009568 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009569 return( ret );
9570 }
9571#endif
9572
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009573 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009574 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009575 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009576 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009578 return( ret );
9579 }
9580 }
9581
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009582#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009583 ret = ssl_write_split( ssl, buf, len );
9584#else
9585 ret = ssl_write_real( ssl, buf, len );
9586#endif
9587
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009589
9590 return( ret );
9591}
9592
9593/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009594 * Notify the peer that the connection is being closed
9595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009597{
9598 int ret;
9599
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009600 if( ssl == NULL || ssl->conf == NULL )
9601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009604
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009605 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9611 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9612 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009615 return( ret );
9616 }
9617 }
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009620
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009621 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009622}
9623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009626 if( transform == NULL )
9627 return;
9628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009630 deflateEnd( &transform->ctx_deflate );
9631 inflateEnd( &transform->ctx_inflate );
9632#endif
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9635 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 mbedtls_md_free( &transform->md_ctx_enc );
9638 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009639
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009640 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009641}
9642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643#if defined(MBEDTLS_X509_CRT_PARSE_C)
9644static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009647
9648 while( cur != NULL )
9649 {
9650 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009652 cur = next;
9653 }
9654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009656
Hanno Becker0271f962018-08-16 13:23:47 +01009657#if defined(MBEDTLS_SSL_PROTO_DTLS)
9658
9659static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9660{
9661 unsigned offset;
9662 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9663
9664 if( hs == NULL )
9665 return;
9666
Hanno Becker283f5ef2018-08-24 09:34:47 +01009667 ssl_free_buffered_record( ssl );
9668
Hanno Becker0271f962018-08-16 13:23:47 +01009669 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009670 ssl_buffering_free_slot( ssl, offset );
9671}
9672
9673static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9674 uint8_t slot )
9675{
9676 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9677 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009678
9679 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9680 return;
9681
Hanno Beckere605b192018-08-21 15:59:07 +01009682 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009683 {
Hanno Beckere605b192018-08-21 15:59:07 +01009684 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009685 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009686 mbedtls_free( hs_buf->data );
9687 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009688 }
9689}
9690
9691#endif /* MBEDTLS_SSL_PROTO_DTLS */
9692
Gilles Peskine9b562d52018-04-25 20:32:43 +02009693void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009694{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009695 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9696
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009697 if( handshake == NULL )
9698 return;
9699
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009700#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9701 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9702 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009703 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009704 handshake->async_in_progress = 0;
9705 }
9706#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9707
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009708#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9709 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9710 mbedtls_md5_free( &handshake->fin_md5 );
9711 mbedtls_sha1_free( &handshake->fin_sha1 );
9712#endif
9713#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9714#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009715#if defined(MBEDTLS_USE_PSA_CRYPTO)
9716 psa_hash_abort( &handshake->fin_sha256_psa );
9717#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009718 mbedtls_sha256_free( &handshake->fin_sha256 );
9719#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009720#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009721#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009722#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009723 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009724#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009725 mbedtls_sha512_free( &handshake->fin_sha512 );
9726#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009727#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009728#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730#if defined(MBEDTLS_DHM_C)
9731 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733#if defined(MBEDTLS_ECDH_C)
9734 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009735#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009736#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009737 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009738#if defined(MBEDTLS_SSL_CLI_C)
9739 mbedtls_free( handshake->ecjpake_cache );
9740 handshake->ecjpake_cache = NULL;
9741 handshake->ecjpake_cache_len = 0;
9742#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009743#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009744
Janos Follath4ae5c292016-02-10 11:27:43 +00009745#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9746 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009747 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009749#endif
9750
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009751#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9752 if( handshake->psk != NULL )
9753 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009754 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009755 mbedtls_free( handshake->psk );
9756 }
9757#endif
9758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9760 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009761 /*
9762 * Free only the linked list wrapper, not the keys themselves
9763 * since the belong to the SNI callback
9764 */
9765 if( handshake->sni_key_cert != NULL )
9766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009768
9769 while( cur != NULL )
9770 {
9771 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009773 cur = next;
9774 }
9775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009777
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009778#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009779 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009780 if( handshake->ecrs_peer_cert != NULL )
9781 {
9782 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9783 mbedtls_free( handshake->ecrs_peer_cert );
9784 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009785#endif
9786
Hanno Becker75173122019-02-06 16:18:31 +00009787#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9788 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9789 mbedtls_pk_free( &handshake->peer_pubkey );
9790#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792#if defined(MBEDTLS_SSL_PROTO_DTLS)
9793 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009794 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009795 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009796#endif
9797
Hanno Becker4a63ed42019-01-08 11:39:35 +00009798#if defined(MBEDTLS_ECDH_C) && \
9799 defined(MBEDTLS_USE_PSA_CRYPTO)
9800 psa_destroy_key( handshake->ecdh_psa_privkey );
9801#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9802
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009803 mbedtls_platform_zeroize( handshake,
9804 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009805}
9806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009808{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009809 if( session == NULL )
9810 return;
9811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009813 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009814#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009815
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009816#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009818#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009819
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009820 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009821}
9822
Paul Bakker5121ce52009-01-03 21:22:43 +00009823/*
9824 * Free an SSL context
9825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009827{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009828 if( ssl == NULL )
9829 return;
9830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009832
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009833 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009834 {
Angus Grattond8213d02016-05-25 20:56:48 +10009835 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009837 }
9838
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009839 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009840 {
Angus Grattond8213d02016-05-25 20:56:48 +10009841 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009843 }
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009846 if( ssl->compress_buf != NULL )
9847 {
Angus Grattond8213d02016-05-25 20:56:48 +10009848 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009850 }
9851#endif
9852
Paul Bakker48916f92012-09-16 19:57:18 +00009853 if( ssl->transform )
9854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 mbedtls_ssl_transform_free( ssl->transform );
9856 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009857 }
9858
9859 if( ssl->handshake )
9860 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009861 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9863 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 mbedtls_free( ssl->handshake );
9866 mbedtls_free( ssl->transform_negotiate );
9867 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009868 }
9869
Paul Bakkerc0463502013-02-14 11:19:38 +01009870 if( ssl->session )
9871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 mbedtls_ssl_session_free( ssl->session );
9873 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009874 }
9875
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009876#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009877 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009878 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009879 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009881 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009882#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9885 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9888 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009889 }
9890#endif
9891
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009892#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009894#endif
9895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009897
Paul Bakker86f04f42013-02-14 11:20:09 +01009898 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009899 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009900}
9901
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009902/*
9903 * Initialze mbedtls_ssl_config
9904 */
9905void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9906{
9907 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9908}
9909
Simon Butcherc97b6972015-12-27 23:48:17 +00009910#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009911static int ssl_preset_default_hashes[] = {
9912#if defined(MBEDTLS_SHA512_C)
9913 MBEDTLS_MD_SHA512,
9914 MBEDTLS_MD_SHA384,
9915#endif
9916#if defined(MBEDTLS_SHA256_C)
9917 MBEDTLS_MD_SHA256,
9918 MBEDTLS_MD_SHA224,
9919#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009920#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009921 MBEDTLS_MD_SHA1,
9922#endif
9923 MBEDTLS_MD_NONE
9924};
Simon Butcherc97b6972015-12-27 23:48:17 +00009925#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009926
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009927static int ssl_preset_suiteb_ciphersuites[] = {
9928 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9929 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9930 0
9931};
9932
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009933#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009934static int ssl_preset_suiteb_hashes[] = {
9935 MBEDTLS_MD_SHA256,
9936 MBEDTLS_MD_SHA384,
9937 MBEDTLS_MD_NONE
9938};
9939#endif
9940
9941#if defined(MBEDTLS_ECP_C)
9942static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9943 MBEDTLS_ECP_DP_SECP256R1,
9944 MBEDTLS_ECP_DP_SECP384R1,
9945 MBEDTLS_ECP_DP_NONE
9946};
9947#endif
9948
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009949/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009950 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009951 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009952int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009953 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009954{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009955#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009956 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009957#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009958
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009959 /* Use the functions here so that they are covered in tests,
9960 * but otherwise access member directly for efficiency */
9961 mbedtls_ssl_conf_endpoint( conf, endpoint );
9962 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009963
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009964 /*
9965 * Things that are common to all presets
9966 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009967#if defined(MBEDTLS_SSL_CLI_C)
9968 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9969 {
9970 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9971#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9972 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9973#endif
9974 }
9975#endif
9976
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009977#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009978 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009979#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009980
9981#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9982 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9983#endif
9984
9985#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9986 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9987#endif
9988
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009989#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9990 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9991#endif
9992
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009993#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009994 conf->f_cookie_write = ssl_cookie_write_dummy;
9995 conf->f_cookie_check = ssl_cookie_check_dummy;
9996#endif
9997
9998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9999 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10000#endif
10001
Janos Follath088ce432017-04-10 12:42:31 +010010002#if defined(MBEDTLS_SSL_SRV_C)
10003 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10004#endif
10005
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010006#if defined(MBEDTLS_SSL_PROTO_DTLS)
10007 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10008 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10009#endif
10010
10011#if defined(MBEDTLS_SSL_RENEGOTIATION)
10012 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010013 memset( conf->renego_period, 0x00, 2 );
10014 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010015#endif
10016
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010017#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10018 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10019 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010020 const unsigned char dhm_p[] =
10021 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10022 const unsigned char dhm_g[] =
10023 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10024
Hanno Beckera90658f2017-10-04 15:29:08 +010010025 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10026 dhm_p, sizeof( dhm_p ),
10027 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010028 {
10029 return( ret );
10030 }
10031 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010032#endif
10033
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010034 /*
10035 * Preset-specific defaults
10036 */
10037 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010038 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010039 /*
10040 * NSA Suite B
10041 */
10042 case MBEDTLS_SSL_PRESET_SUITEB:
10043 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10044 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10045 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10046 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10047
10048 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10049 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10050 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10051 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10052 ssl_preset_suiteb_ciphersuites;
10053
10054#if defined(MBEDTLS_X509_CRT_PARSE_C)
10055 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010056#endif
10057
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010058#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010059 conf->sig_hashes = ssl_preset_suiteb_hashes;
10060#endif
10061
10062#if defined(MBEDTLS_ECP_C)
10063 conf->curve_list = ssl_preset_suiteb_curves;
10064#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010065 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010066
10067 /*
10068 * Default
10069 */
10070 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010071 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10072 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10073 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10074 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10075 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10076 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10077 MBEDTLS_SSL_MIN_MINOR_VERSION :
10078 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010079 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10080 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10081
10082#if defined(MBEDTLS_SSL_PROTO_DTLS)
10083 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10084 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10085#endif
10086
10087 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10088 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10089 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10090 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10091 mbedtls_ssl_list_ciphersuites();
10092
10093#if defined(MBEDTLS_X509_CRT_PARSE_C)
10094 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10095#endif
10096
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010097#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010098 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010099#endif
10100
10101#if defined(MBEDTLS_ECP_C)
10102 conf->curve_list = mbedtls_ecp_grp_id_list();
10103#endif
10104
10105#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10106 conf->dhm_min_bitlen = 1024;
10107#endif
10108 }
10109
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010110 return( 0 );
10111}
10112
10113/*
10114 * Free mbedtls_ssl_config
10115 */
10116void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10117{
10118#if defined(MBEDTLS_DHM_C)
10119 mbedtls_mpi_free( &conf->dhm_P );
10120 mbedtls_mpi_free( &conf->dhm_G );
10121#endif
10122
10123#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10124 if( conf->psk != NULL )
10125 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010126 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010127 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010128 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010129 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010130 }
10131
10132 if( conf->psk_identity != NULL )
10133 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010134 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010135 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010136 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010137 conf->psk_identity_len = 0;
10138 }
10139#endif
10140
10141#if defined(MBEDTLS_X509_CRT_PARSE_C)
10142 ssl_key_cert_free( conf->key_cert );
10143#endif
10144
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010145 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010146}
10147
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010148#if defined(MBEDTLS_PK_C) && \
10149 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010150/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155#if defined(MBEDTLS_RSA_C)
10156 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10157 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159#if defined(MBEDTLS_ECDSA_C)
10160 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10161 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010164}
10165
Hanno Becker7e5437a2017-04-28 17:15:26 +010010166unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10167{
10168 switch( type ) {
10169 case MBEDTLS_PK_RSA:
10170 return( MBEDTLS_SSL_SIG_RSA );
10171 case MBEDTLS_PK_ECDSA:
10172 case MBEDTLS_PK_ECKEY:
10173 return( MBEDTLS_SSL_SIG_ECDSA );
10174 default:
10175 return( MBEDTLS_SSL_SIG_ANON );
10176 }
10177}
10178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010180{
10181 switch( sig )
10182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183#if defined(MBEDTLS_RSA_C)
10184 case MBEDTLS_SSL_SIG_RSA:
10185 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187#if defined(MBEDTLS_ECDSA_C)
10188 case MBEDTLS_SSL_SIG_ECDSA:
10189 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010190#endif
10191 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010193 }
10194}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010195#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010196
Hanno Becker7e5437a2017-04-28 17:15:26 +010010197#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10198 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10199
10200/* Find an entry in a signature-hash set matching a given hash algorithm. */
10201mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10202 mbedtls_pk_type_t sig_alg )
10203{
10204 switch( sig_alg )
10205 {
10206 case MBEDTLS_PK_RSA:
10207 return( set->rsa );
10208 case MBEDTLS_PK_ECDSA:
10209 return( set->ecdsa );
10210 default:
10211 return( MBEDTLS_MD_NONE );
10212 }
10213}
10214
10215/* Add a signature-hash-pair to a signature-hash set */
10216void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10217 mbedtls_pk_type_t sig_alg,
10218 mbedtls_md_type_t md_alg )
10219{
10220 switch( sig_alg )
10221 {
10222 case MBEDTLS_PK_RSA:
10223 if( set->rsa == MBEDTLS_MD_NONE )
10224 set->rsa = md_alg;
10225 break;
10226
10227 case MBEDTLS_PK_ECDSA:
10228 if( set->ecdsa == MBEDTLS_MD_NONE )
10229 set->ecdsa = md_alg;
10230 break;
10231
10232 default:
10233 break;
10234 }
10235}
10236
10237/* Allow exactly one hash algorithm for each signature. */
10238void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10239 mbedtls_md_type_t md_alg )
10240{
10241 set->rsa = md_alg;
10242 set->ecdsa = md_alg;
10243}
10244
10245#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10246 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10247
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010248/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010249 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010252{
10253 switch( hash )
10254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255#if defined(MBEDTLS_MD5_C)
10256 case MBEDTLS_SSL_HASH_MD5:
10257 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#if defined(MBEDTLS_SHA1_C)
10260 case MBEDTLS_SSL_HASH_SHA1:
10261 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263#if defined(MBEDTLS_SHA256_C)
10264 case MBEDTLS_SSL_HASH_SHA224:
10265 return( MBEDTLS_MD_SHA224 );
10266 case MBEDTLS_SSL_HASH_SHA256:
10267 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269#if defined(MBEDTLS_SHA512_C)
10270 case MBEDTLS_SSL_HASH_SHA384:
10271 return( MBEDTLS_MD_SHA384 );
10272 case MBEDTLS_SSL_HASH_SHA512:
10273 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010274#endif
10275 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010277 }
10278}
10279
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010280/*
10281 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10282 */
10283unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10284{
10285 switch( md )
10286 {
10287#if defined(MBEDTLS_MD5_C)
10288 case MBEDTLS_MD_MD5:
10289 return( MBEDTLS_SSL_HASH_MD5 );
10290#endif
10291#if defined(MBEDTLS_SHA1_C)
10292 case MBEDTLS_MD_SHA1:
10293 return( MBEDTLS_SSL_HASH_SHA1 );
10294#endif
10295#if defined(MBEDTLS_SHA256_C)
10296 case MBEDTLS_MD_SHA224:
10297 return( MBEDTLS_SSL_HASH_SHA224 );
10298 case MBEDTLS_MD_SHA256:
10299 return( MBEDTLS_SSL_HASH_SHA256 );
10300#endif
10301#if defined(MBEDTLS_SHA512_C)
10302 case MBEDTLS_MD_SHA384:
10303 return( MBEDTLS_SSL_HASH_SHA384 );
10304 case MBEDTLS_MD_SHA512:
10305 return( MBEDTLS_SSL_HASH_SHA512 );
10306#endif
10307 default:
10308 return( MBEDTLS_SSL_HASH_NONE );
10309 }
10310}
10311
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010312#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010313/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010314 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010315 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010316 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010317int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010320
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010321 if( ssl->conf->curve_list == NULL )
10322 return( -1 );
10323
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010324 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010325 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010326 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010327
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010328 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010329}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010330#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010331
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010332#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010333/*
10334 * Check if a hash proposed by the peer is in our list.
10335 * Return 0 if we're willing to use it, -1 otherwise.
10336 */
10337int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10338 mbedtls_md_type_t md )
10339{
10340 const int *cur;
10341
10342 if( ssl->conf->sig_hashes == NULL )
10343 return( -1 );
10344
10345 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10346 if( *cur == (int) md )
10347 return( 0 );
10348
10349 return( -1 );
10350}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010351#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353#if defined(MBEDTLS_X509_CRT_PARSE_C)
10354int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10355 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010356 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010357 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010358{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010359 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010361 int usage = 0;
10362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010364 const char *ext_oid;
10365 size_t ext_len;
10366#endif
10367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10369 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010370 ((void) cert);
10371 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010372 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010373#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10376 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010377 {
10378 /* Server part of the key exchange */
10379 switch( ciphersuite->key_exchange )
10380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 case MBEDTLS_KEY_EXCHANGE_RSA:
10382 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010383 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010384 break;
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10387 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10388 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10389 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010390 break;
10391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10393 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010394 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010395 break;
10396
10397 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 case MBEDTLS_KEY_EXCHANGE_NONE:
10399 case MBEDTLS_KEY_EXCHANGE_PSK:
10400 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10401 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010402 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010403 usage = 0;
10404 }
10405 }
10406 else
10407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10409 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010410 }
10411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010413 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010414 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010415 ret = -1;
10416 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010417#else
10418 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10422 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10425 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010426 }
10427 else
10428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10430 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010431 }
10432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010434 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010435 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010436 ret = -1;
10437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010439
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010440 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010443
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010444/*
10445 * Convert version numbers to/from wire format
10446 * and, for DTLS, to/from TLS equivalent.
10447 *
10448 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010449 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010450 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10451 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010454 unsigned char ver[2] )
10455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456#if defined(MBEDTLS_SSL_PROTO_DTLS)
10457 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010460 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10461
10462 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10463 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10464 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010465 else
10466#else
10467 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010468#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010469 {
10470 ver[0] = (unsigned char) major;
10471 ver[1] = (unsigned char) minor;
10472 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010473}
10474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010476 const unsigned char ver[2] )
10477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478#if defined(MBEDTLS_SSL_PROTO_DTLS)
10479 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010480 {
10481 *major = 255 - ver[0] + 2;
10482 *minor = 255 - ver[1] + 1;
10483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010485 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10486 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010487 else
10488#else
10489 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010490#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010491 {
10492 *major = ver[0];
10493 *minor = ver[1];
10494 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010495}
10496
Simon Butcher99000142016-10-13 17:21:01 +010010497int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10498{
10499#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10500 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10501 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10502
10503 switch( md )
10504 {
10505#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10506#if defined(MBEDTLS_MD5_C)
10507 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010508 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010509#endif
10510#if defined(MBEDTLS_SHA1_C)
10511 case MBEDTLS_SSL_HASH_SHA1:
10512 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10513 break;
10514#endif
10515#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10516#if defined(MBEDTLS_SHA512_C)
10517 case MBEDTLS_SSL_HASH_SHA384:
10518 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10519 break;
10520#endif
10521#if defined(MBEDTLS_SHA256_C)
10522 case MBEDTLS_SSL_HASH_SHA256:
10523 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10524 break;
10525#endif
10526 default:
10527 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10528 }
10529
10530 return 0;
10531#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10532 (void) ssl;
10533 (void) md;
10534
10535 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10537}
10538
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010539#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10540 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10541int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10542 unsigned char *output,
10543 unsigned char *data, size_t data_len )
10544{
10545 int ret = 0;
10546 mbedtls_md5_context mbedtls_md5;
10547 mbedtls_sha1_context mbedtls_sha1;
10548
10549 mbedtls_md5_init( &mbedtls_md5 );
10550 mbedtls_sha1_init( &mbedtls_sha1 );
10551
10552 /*
10553 * digitally-signed struct {
10554 * opaque md5_hash[16];
10555 * opaque sha_hash[20];
10556 * };
10557 *
10558 * md5_hash
10559 * MD5(ClientHello.random + ServerHello.random
10560 * + ServerParams);
10561 * sha_hash
10562 * SHA(ClientHello.random + ServerHello.random
10563 * + ServerParams);
10564 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010565 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010566 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010568 goto exit;
10569 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010570 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010571 ssl->handshake->randbytes, 64 ) ) != 0 )
10572 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010574 goto exit;
10575 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010576 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010577 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010579 goto exit;
10580 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010581 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010582 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010584 goto exit;
10585 }
10586
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010587 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010588 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010590 goto exit;
10591 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010592 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010593 ssl->handshake->randbytes, 64 ) ) != 0 )
10594 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010596 goto exit;
10597 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010598 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010599 data_len ) ) != 0 )
10600 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010602 goto exit;
10603 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010604 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010605 output + 16 ) ) != 0 )
10606 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010608 goto exit;
10609 }
10610
10611exit:
10612 mbedtls_md5_free( &mbedtls_md5 );
10613 mbedtls_sha1_free( &mbedtls_sha1 );
10614
10615 if( ret != 0 )
10616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10617 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10618
10619 return( ret );
10620
10621}
10622#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10623 MBEDTLS_SSL_PROTO_TLS1_1 */
10624
10625#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10626 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010627
10628#if defined(MBEDTLS_USE_PSA_CRYPTO)
10629int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10630 unsigned char *hash, size_t *hashlen,
10631 unsigned char *data, size_t data_len,
10632 mbedtls_md_type_t md_alg )
10633{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010634 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010635 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010636 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10637
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010639
10640 if( ( status = psa_hash_setup( &hash_operation,
10641 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010642 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010643 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010644 goto exit;
10645 }
10646
Andrzej Kurek814feff2019-01-14 04:35:19 -050010647 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10648 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010649 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010650 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010651 goto exit;
10652 }
10653
Andrzej Kurek814feff2019-01-14 04:35:19 -050010654 if( ( status = psa_hash_update( &hash_operation,
10655 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010656 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010657 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010658 goto exit;
10659 }
10660
Andrzej Kurek814feff2019-01-14 04:35:19 -050010661 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10662 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010663 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010664 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010665 goto exit;
10666 }
10667
10668exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010669 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010670 {
10671 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10672 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010673 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010674 {
10675 case PSA_ERROR_NOT_SUPPORTED:
10676 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010677 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010678 case PSA_ERROR_BUFFER_TOO_SMALL:
10679 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10680 case PSA_ERROR_INSUFFICIENT_MEMORY:
10681 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10682 default:
10683 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10684 }
10685 }
10686 return( 0 );
10687}
10688
10689#else
10690
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010691int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010692 unsigned char *hash, size_t *hashlen,
10693 unsigned char *data, size_t data_len,
10694 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010695{
10696 int ret = 0;
10697 mbedtls_md_context_t ctx;
10698 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010699 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010700
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010702
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010703 mbedtls_md_init( &ctx );
10704
10705 /*
10706 * digitally-signed struct {
10707 * opaque client_random[32];
10708 * opaque server_random[32];
10709 * ServerDHParams params;
10710 * };
10711 */
10712 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10713 {
10714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10715 goto exit;
10716 }
10717 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10718 {
10719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10720 goto exit;
10721 }
10722 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10723 {
10724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10725 goto exit;
10726 }
10727 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10728 {
10729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10730 goto exit;
10731 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010732 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010733 {
10734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10735 goto exit;
10736 }
10737
10738exit:
10739 mbedtls_md_free( &ctx );
10740
10741 if( ret != 0 )
10742 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10743 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10744
10745 return( ret );
10746}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010747#endif /* MBEDTLS_USE_PSA_CRYPTO */
10748
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010749#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10750 MBEDTLS_SSL_PROTO_TLS1_2 */
10751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752#endif /* MBEDTLS_SSL_TLS_C */