blob: b0dcf1d331ad4bba572599015c440acdb014a131 [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 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001900#if defined(MBEDTLS_GCM_C) || \
1901 defined(MBEDTLS_CCM_C) || \
1902 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001904 mode == MBEDTLS_MODE_CCM ||
1905 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001906 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001907 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001909 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001910
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001911 /* Check that there's space for both the authentication tag
1912 * and the explicit IV before and after the record content. */
1913 if( post_avail < transform->taglen ||
1914 rec->data_offset < explicit_iv_len )
1915 {
1916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1917 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1918 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001919
Paul Bakker68884e32013-01-07 18:20:04 +01001920 /*
1921 * Generate IV
1922 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001923 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1924 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001925 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001926 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001927 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1928 explicit_iv_len );
1929 /* Prefix record content with explicit IV. */
1930 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001931 }
1932 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1933 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001934 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001935 unsigned char i;
1936
1937 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1938
1939 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001940 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001941 }
1942 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001943 {
1944 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001947 }
1948
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001949 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1950 iv, transform->ivlen );
1951 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001952 data - explicit_iv_len, explicit_iv_len );
1953 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1954 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001956 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001957 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001958
Paul Bakker68884e32013-01-07 18:20:04 +01001959 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001960 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001961 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001962
1963 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001964 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001965 iv, transform->ivlen,
1966 add_data, 13, /* add data */
1967 data, rec->data_len, /* source */
1968 data, &rec->data_len, /* destination */
1969 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001972 return( ret );
1973 }
1974
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001975 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1976 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001977
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001978 rec->data_len += transform->taglen + explicit_iv_len;
1979 rec->data_offset -= explicit_iv_len;
1980 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001981 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1985#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001986 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001989 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001990 size_t padlen, i;
1991 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001993 /* Currently we're always using minimal padding
1994 * (up to 255 bytes would be allowed). */
1995 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1996 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 padlen = 0;
1998
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001999 /* Check there's enough space in the buffer for the padding. */
2000 if( post_avail < padlen + 1 )
2001 {
2002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2003 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2004 }
2005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002009 rec->data_len += padlen + 1;
2010 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002014 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2015 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002016 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002017 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002019 if( f_rng == NULL )
2020 {
2021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2023 }
2024
2025 if( rec->data_offset < transform->ivlen )
2026 {
2027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2028 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2029 }
2030
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002031 /*
2032 * Generate IV
2033 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002034 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002035 if( ret != 0 )
2036 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002038 memcpy( data - transform->ivlen, transform->iv_enc,
2039 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002040
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002041 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002045 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002046 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002047 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002049 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2050 transform->iv_enc,
2051 transform->ivlen,
2052 data, rec->data_len,
2053 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002056 return( ret );
2057 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002058
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002059 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002063 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002066 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002067 {
2068 /*
2069 * Save IV in SSL3 and TLS1
2070 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002071 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2072 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002074 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002075#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002076 {
2077 data -= transform->ivlen;
2078 rec->data_offset -= transform->ivlen;
2079 rec->data_len += transform->ivlen;
2080 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002083 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002084 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002085 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2086
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002087 /*
2088 * MAC(MAC_write_key, seq_num +
2089 * TLSCipherText.type +
2090 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002091 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002092 * IV + // except for TLS 1.0
2093 * ENC(content + padding + padding_length));
2094 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002095
2096 if( post_avail < transform->maclen)
2097 {
2098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2099 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2100 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002103 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2104 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002105
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002107
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002108 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2109 sizeof( add_data ) );
2110 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2111 data, rec->data_len );
2112 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2113 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002114
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002115 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002116
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 rec->data_len += transform->maclen;
2118 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002119 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002125 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002131 /* Make extra sure authentication was performed, exactly once */
2132 if( auth_done != 1 )
2133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
2140 return( 0 );
2141}
2142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002146 int auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002147#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002148 size_t padlen = 0, correct = 1;
2149#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002153 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002157 }
2158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002160
Paul Bakker48916f92012-09-16 19:57:18 +00002161 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002164 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2169 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002170 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002171 int ret;
2172 size_t olen = 0;
2173
Paul Bakker68884e32013-01-07 18:20:04 +01002174 padlen = 0;
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002177 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002178 ssl->transform_in->ivlen,
2179 ssl->in_msg, ssl->in_msglen,
2180 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002183 return( ret );
2184 }
2185
2186 if( ssl->in_msglen != olen )
2187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2189 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
Paul Bakker68884e32013-01-07 18:20:04 +01002192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002194#if defined(MBEDTLS_GCM_C) || \
2195 defined(MBEDTLS_CCM_C) || \
2196 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002198 mode == MBEDTLS_MODE_CCM ||
2199 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002200 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002201 int ret;
2202 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002203 unsigned char *dec_msg;
2204 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002205 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002206 unsigned char iv[12];
2207 mbedtls_ssl_transform *transform = ssl->transform_in;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002208 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002209
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002210 /*
2211 * Compute and update sizes
2212 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002213 if( ssl->in_msglen < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002216 "+ taglen (%d)", ssl->in_msglen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002217 explicit_iv_len, ssl->transform_in->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002219 }
Hanno Beckere694c3e2017-12-27 21:34:08 +00002220 dec_msglen = ssl->in_msglen - explicit_iv_len - transform->taglen;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002221
Paul Bakker68884e32013-01-07 18:20:04 +01002222 dec_msg = ssl->in_msg;
2223 dec_msg_result = ssl->in_msg;
2224 ssl->in_msglen = dec_msglen;
2225
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002226 /*
2227 * Prepare additional authenticated data
2228 */
Paul Bakker68884e32013-01-07 18:20:04 +01002229 memcpy( add_data, ssl->in_ctr, 8 );
2230 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002232 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002233 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2234 add_data[12] = ssl->in_msglen & 0xFF;
2235
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002236 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002237
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002238 /*
2239 * Prepare IV
2240 */
2241 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2242 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002243 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002244 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2245 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002246
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002247 }
2248 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2249 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002250 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002251 unsigned char i;
2252
2253 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2254
2255 for( i = 0; i < 8; i++ )
2256 iv[i+4] ^= ssl->in_ctr[i];
2257 }
2258 else
2259 {
2260 /* Reminder if we ever add an AEAD mode with a different size */
2261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2263 }
2264
2265 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Beckere694c3e2017-12-27 21:34:08 +00002266 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen,
2267 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002268
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002269 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002270 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002273 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002274 add_data, 13,
2275 dec_msg, dec_msglen,
2276 dec_msg_result, &olen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002277 dec_msg + dec_msglen,
2278 ssl->transform_in->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2283 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002284
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002285 return( ret );
2286 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002288
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002289 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002293 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2297#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002298 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 {
Paul Bakker45829992013-01-03 14:52:21 +01002301 /*
2302 * Decrypt and check the padding
2303 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002304 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002305 unsigned char *dec_msg;
2306 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002307 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002308 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002309 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 /*
Paul Bakker45829992013-01-03 14:52:21 +01002312 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2315 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002316 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002317#endif
Paul Bakker45829992013-01-03 14:52:21 +01002318
2319 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2320 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002323 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2324 ssl->transform_in->ivlen,
2325 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002327 }
2328
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002329 dec_msglen = ssl->in_msglen;
2330 dec_msg = ssl->in_msg;
2331 dec_msg_result = ssl->in_msg;
2332
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002333 /*
2334 * Authenticate before decrypt if enabled
2335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2337 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002338 {
Hanno Becker992b6872017-11-09 18:57:39 +00002339 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002340 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002343
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002344 dec_msglen -= ssl->transform_in->maclen;
2345 ssl->in_msglen -= ssl->transform_in->maclen;
2346
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002347 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2348 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2349 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2350 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2355 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002356 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002357 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002361 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002362 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002363 ssl->transform_in->maclen );
2364
Hanno Becker992b6872017-11-09 18:57:39 +00002365 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2366 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002371 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002372 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002375
2376 /*
2377 * Check length sanity
2378 */
2379 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002382 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002384 }
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002387 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002388 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002393 dec_msglen -= ssl->transform_in->ivlen;
2394 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002395
Paul Bakker48916f92012-09-16 19:57:18 +00002396 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002397 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002402 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002403 ssl->transform_in->ivlen,
2404 dec_msg, dec_msglen,
2405 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002408 return( ret );
2409 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002410
Paul Bakkercca5b812013-08-31 17:40:26 +02002411 if( dec_msglen != olen )
2412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002415 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2418 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002419 {
2420 /*
2421 * Save IV in SSL3 and TLS1
2422 */
2423 memcpy( ssl->transform_in->iv_dec,
2424 ssl->transform_in->cipher_ctx_dec.iv,
2425 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002427#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002430
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002431 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002432 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_SSL_DEBUG_ALL)
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002436 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002437#endif
Paul Bakker45829992013-01-03 14:52:21 +01002438 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002439 correct = 0;
2440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_SSL_PROTO_SSL3)
2443 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 {
Paul Bakker48916f92012-09-16 19:57:18 +00002445 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_DEBUG_ALL)
2448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002450 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002451#endif
Paul Bakker45829992013-01-03 14:52:21 +01002452 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454 }
2455 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2457#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2458 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2459 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
2461 /*
Paul Bakker45829992013-01-03 14:52:21 +01002462 * TLSv1+: always check the padding up to the first failure
2463 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002465 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002466 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002467 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002468
Paul Bakker956c9e02013-12-19 14:42:28 +01002469 /*
2470 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002471 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002472 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002473 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002474 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002475 *
2476 * In both cases we reset padding_idx to a safe value (0) to
2477 * prevent out-of-buffer reads.
2478 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002479 correct &= ( padlen <= ssl->in_msglen );
2480 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002481 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002482
2483 padding_idx *= correct;
2484
Angus Grattonb512bc12018-06-19 15:57:50 +10002485 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002486 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002487 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002488 pad_count += real_count *
2489 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2490 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002491
2492 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002495 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002497#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002498 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002500 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2502 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002506 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
2508 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002510 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002512 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +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 );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002518#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524 * Authenticate if not done yet.
2525 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 */
Hanno Becker52344c22018-01-03 15:24:20 +00002527#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002529 {
Hanno Becker992b6872017-11-09 18:57:39 +00002530 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002531
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002532 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002534 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2535 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#if defined(MBEDTLS_SSL_PROTO_SSL3)
2538 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002539 {
2540 ssl_mac( &ssl->transform_in->md_ctx_dec,
2541 ssl->transform_in->mac_dec,
2542 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002543 ssl->in_ctr, ssl->in_msgtype,
2544 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002545 }
2546 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2548#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2549 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2550 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002551 {
2552 /*
2553 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002554 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002555 *
2556 * Known timing attacks:
2557 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2558 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002559 * To compensate for different timings for the MAC calculation
2560 * depending on how much padding was removed (which is determined
2561 * by padlen), process extra_run more blocks through the hash
2562 * function.
2563 *
2564 * The formula in the paper is
2565 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2566 * where L1 is the size of the header plus the decrypted message
2567 * plus CBC padding and L2 is the size of the header plus the
2568 * decrypted message. This is for an underlying hash function
2569 * with 64-byte blocks.
2570 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2571 * correctly. We round down instead of up, so -56 is the correct
2572 * value for our calculations instead of -55.
2573 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002574 * Repeat the formula rather than defining a block_size variable.
2575 * This avoids requiring division by a variable at runtime
2576 * (which would be marginally less efficient and would require
2577 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002578 */
2579 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002580
2581 /*
2582 * The next two sizes are the minimum and maximum values of
2583 * in_msglen over all padlen values.
2584 *
2585 * They're independent of padlen, since we previously did
2586 * in_msglen -= padlen.
2587 *
2588 * Note that max_len + maclen is never more than the buffer
2589 * length, as we previously did in_msglen -= maclen too.
2590 */
2591 const size_t max_len = ssl->in_msglen + padlen;
2592 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2593
Hanno Beckere694c3e2017-12-27 21:34:08 +00002594 switch( ssl->handshake->ciphersuite_info->mac )
Gilles Peskine20b44082018-05-29 14:06:49 +02002595 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002596#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2597 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002598 case MBEDTLS_MD_MD5:
2599 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002600 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002601 /* 8 bytes of message size, 64-byte compression blocks */
2602 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2603 ( 13 + ssl->in_msglen + 8 ) / 64;
2604 break;
2605#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002606#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002607 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002608 /* 16 bytes of message size, 128-byte compression blocks */
2609 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2610 ( 13 + ssl->in_msglen + 16 ) / 128;
2611 break;
2612#endif
2613 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2616 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002617
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002618 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2621 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2622 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2623 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002624 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002625 /* Make sure we access everything even when padlen > 0. This
2626 * makes the synchronisation requirements for just-in-time
2627 * Prime+Probe attacks much tighter and hopefully impractical. */
2628 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002629 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002630
2631 /* Call mbedtls_md_process at least once due to cache attacks
2632 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002633 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002637
2638 /* Make sure we access all the memory that could contain the MAC,
2639 * before we check it in the next code block. This makes the
2640 * synchronisation requirements for just-in-time Prime+Probe
2641 * attacks much tighter and hopefully impractical. */
2642 ssl_read_memory( ssl->in_msg + min_len,
2643 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002644 }
2645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2647 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002653#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002654 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2655 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2656 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Hanno Becker992b6872017-11-09 18:57:39 +00002659 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2660 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_SSL_DEBUG_ALL)
2663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002664#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002665 correct = 0;
2666 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002667 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002668 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002669
2670 /*
2671 * Finally check the correct flag
2672 */
2673 if( correct == 0 )
2674 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002675#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002676
2677 /* Make extra sure authentication was performed, exactly once */
2678 if( auth_done != 1 )
2679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 if( ssl->in_msglen == 0 )
2685 {
Angus Gratton34817922018-06-19 15:58:22 +10002686#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2687 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2688 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2689 {
2690 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2693 }
2694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2695
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 ssl->nb_zero++;
2697
2698 /*
2699 * Three or more empty messages may be a DoS attack
2700 * (excessive CPU consumption).
2701 */
2702 if( ssl->nb_zero > 3 )
2703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708 }
2709 else
2710 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002713 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002714 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002715 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002716 }
2717 else
2718#endif
2719 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002721 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2722 if( ++ssl->in_ctr[i - 1] != 0 )
2723 break;
2724
2725 /* The loop goes to its end iff the counter is wrapping */
2726 if( i == ssl_ep_len( ssl ) )
2727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2729 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002730 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002731 }
2732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
2735 return( 0 );
2736}
2737
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002738#undef MAC_NONE
2739#undef MAC_PLAINTEXT
2740#undef MAC_CIPHERTEXT
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002743/*
2744 * Compression/decompression functions
2745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002747{
2748 int ret;
2749 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002750 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002751 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002752 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002755
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002756 if( len_pre == 0 )
2757 return( 0 );
2758
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759 memcpy( msg_pre, ssl->out_msg, len_pre );
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002762 ssl->out_msglen ) );
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002765 ssl->out_msg, ssl->out_msglen );
2766
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2768 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2769 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002770 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771
Paul Bakker48916f92012-09-16 19:57:18 +00002772 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773 if( ret != Z_OK )
2774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2776 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777 }
2778
Angus Grattond8213d02016-05-25 20:56:48 +10002779 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002780 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783 ssl->out_msglen ) );
2784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002786 ssl->out_msg, ssl->out_msglen );
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789
2790 return( 0 );
2791}
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002794{
2795 int ret;
2796 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002797 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002798 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002799 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002803 if( len_pre == 0 )
2804 return( 0 );
2805
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806 memcpy( msg_pre, ssl->in_msg, len_pre );
2807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002809 ssl->in_msglen ) );
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002812 ssl->in_msg, ssl->in_msglen );
2813
Paul Bakker48916f92012-09-16 19:57:18 +00002814 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2815 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2816 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002817 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002818 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002819
Paul Bakker48916f92012-09-16 19:57:18 +00002820 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002821 if( ret != Z_OK )
2822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2824 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002825 }
2826
Angus Grattond8213d02016-05-25 20:56:48 +10002827 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002828 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002831 ssl->in_msglen ) );
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002834 ssl->in_msg, ssl->in_msglen );
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002837
2838 return( 0 );
2839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2843static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_PROTO_DTLS)
2846static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002847{
2848 /* If renegotiation is not enforced, retransmit until we would reach max
2849 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002850 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002851 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002852 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002853 unsigned char doublings = 1;
2854
2855 while( ratio != 0 )
2856 {
2857 ++doublings;
2858 ratio >>= 1;
2859 }
2860
2861 if( ++ssl->renego_records_seen > doublings )
2862 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002864 return( 0 );
2865 }
2866 }
2867
2868 return( ssl_write_hello_request( ssl ) );
2869}
2870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002872
Paul Bakker5121ce52009-01-03 21:22:43 +00002873/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002874 * Fill the input message buffer by appending data to it.
2875 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002876 *
2877 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2878 * available (from this read and/or a previous one). Otherwise, an error code
2879 * is returned (possibly EOF or WANT_READ).
2880 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002881 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2882 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2883 * since we always read a whole datagram at once.
2884 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002885 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002886 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002889{
Paul Bakker23986e52011-04-24 08:57:21 +00002890 int ret;
2891 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002895 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002898 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002900 }
2901
Angus Grattond8213d02016-05-25 20:56:48 +10002902 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002906 }
2907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002911 uint32_t timeout;
2912
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002913 /* Just to be sure */
2914 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2915 {
2916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2917 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2918 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2919 }
2920
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002921 /*
2922 * The point is, we need to always read a full datagram at once, so we
2923 * sometimes read more then requested, and handle the additional data.
2924 * It could be the rest of the current record (while fetching the
2925 * header) and/or some other records in the same datagram.
2926 */
2927
2928 /*
2929 * Move to the next record in the already read datagram if applicable
2930 */
2931 if( ssl->next_record_offset != 0 )
2932 {
2933 if( ssl->in_left < ssl->next_record_offset )
2934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002937 }
2938
2939 ssl->in_left -= ssl->next_record_offset;
2940
2941 if( ssl->in_left != 0 )
2942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002944 ssl->next_record_offset ) );
2945 memmove( ssl->in_hdr,
2946 ssl->in_hdr + ssl->next_record_offset,
2947 ssl->in_left );
2948 }
2949
2950 ssl->next_record_offset = 0;
2951 }
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002955
2956 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002957 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002958 */
2959 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002962 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002963 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002964
2965 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002966 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002967 * are not at the beginning of a new record, the caller did something
2968 * wrong.
2969 */
2970 if( ssl->in_left != 0 )
2971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002974 }
2975
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002976 /*
2977 * Don't even try to read if time's out already.
2978 * This avoids by-passing the timer when repeatedly receiving messages
2979 * that will end up being dropped.
2980 */
2981 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002982 {
2983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002984 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002985 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002986 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002987 {
Angus Grattond8213d02016-05-25 20:56:48 +10002988 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002991 timeout = ssl->handshake->retransmit_timeout;
2992 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002993 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002996
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002997 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002998 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2999 timeout );
3000 else
3001 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003004
3005 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003007 }
3008
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003009 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003012 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003015 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003016 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003019 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003020 }
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003025 return( ret );
3026 }
3027
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003028 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003031 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003033 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003034 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003037 return( ret );
3038 }
3039
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003040 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003041 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003043 }
3044
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 if( ret < 0 )
3046 return( ret );
3047
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003048 ssl->in_left = ret;
3049 }
3050 else
3051#endif
3052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003054 ssl->in_left, nb_want ) );
3055
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003056 while( ssl->in_left < nb_want )
3057 {
3058 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003059
3060 if( ssl_check_timer( ssl ) != 0 )
3061 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3062 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003063 {
3064 if( ssl->f_recv_timeout != NULL )
3065 {
3066 ret = ssl->f_recv_timeout( ssl->p_bio,
3067 ssl->in_hdr + ssl->in_left, len,
3068 ssl->conf->read_timeout );
3069 }
3070 else
3071 {
3072 ret = ssl->f_recv( ssl->p_bio,
3073 ssl->in_hdr + ssl->in_left, len );
3074 }
3075 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003078 ssl->in_left, nb_want ) );
3079 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080
3081 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003083
3084 if( ret < 0 )
3085 return( ret );
3086
mohammad160352aecb92018-03-28 23:41:40 -07003087 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003088 {
Darryl Green11999bb2018-03-13 15:22:58 +00003089 MBEDTLS_SSL_DEBUG_MSG( 1,
3090 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003091 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3093 }
3094
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 ssl->in_left += ret;
3096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 }
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
3101 return( 0 );
3102}
3103
3104/*
3105 * Flush any data not yet written
3106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003108{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003109 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003110 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003114 if( ssl->f_send == NULL )
3115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003117 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003119 }
3120
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003121 /* Avoid incrementing counter if data is flushed */
3122 if( ssl->out_left == 0 )
3123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003125 return( 0 );
3126 }
3127
Paul Bakker5121ce52009-01-03 21:22:43 +00003128 while( ssl->out_left > 0 )
3129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3131 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Hanno Becker2b1e3542018-08-06 11:19:13 +01003133 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003134 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
3138 if( ret <= 0 )
3139 return( ret );
3140
mohammad160352aecb92018-03-28 23:41:40 -07003141 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003142 {
Darryl Green11999bb2018-03-13 15:22:58 +00003143 MBEDTLS_SSL_DEBUG_MSG( 1,
3144 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003145 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3147 }
3148
Paul Bakker5121ce52009-01-03 21:22:43 +00003149 ssl->out_left -= ret;
3150 }
3151
Hanno Becker2b1e3542018-08-06 11:19:13 +01003152#if defined(MBEDTLS_SSL_PROTO_DTLS)
3153 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003154 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003155 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003156 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003157 else
3158#endif
3159 {
3160 ssl->out_hdr = ssl->out_buf + 8;
3161 }
3162 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
3166 return( 0 );
3167}
3168
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003169/*
3170 * Functions to handle the DTLS retransmission state machine
3171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173/*
3174 * Append current handshake message to current outgoing flight
3175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3180 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3181 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003182
3183 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003184 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003185 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189 }
3190
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003191 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003192 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003195 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003196 }
3197
3198 /* Copy current handshake message with headers */
3199 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3200 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003201 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202 msg->next = NULL;
3203
3204 /* Append to the current flight */
3205 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003206 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003207 else
3208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003210 while( cur->next != NULL )
3211 cur = cur->next;
3212 cur->next = msg;
3213 }
3214
Hanno Becker3b235902018-08-06 09:54:53 +01003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003216 return( 0 );
3217}
3218
3219/*
3220 * Free the current flight of handshake messages
3221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 mbedtls_ssl_flight_item *cur = flight;
3225 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003226
3227 while( cur != NULL )
3228 {
3229 next = cur->next;
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 mbedtls_free( cur->p );
3232 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003233
3234 cur = next;
3235 }
3236}
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3239static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003240#endif
3241
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003242/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003243 * Swap transform_out and out_ctr with the alternative ones
3244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003248 unsigned char tmp_out_ctr[8];
3249
3250 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003253 return;
3254 }
3255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003257
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003258 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003259 tmp_transform = ssl->transform_out;
3260 ssl->transform_out = ssl->handshake->alt_transform_out;
3261 ssl->handshake->alt_transform_out = tmp_transform;
3262
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003263 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003264 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3265 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003266 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003267
3268 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003269 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3272 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3277 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003278 }
3279 }
3280#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003281}
3282
3283/*
3284 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003285 */
3286int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3287{
3288 int ret = 0;
3289
3290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3291
3292 ret = mbedtls_ssl_flight_transmit( ssl );
3293
3294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3295
3296 return( ret );
3297}
3298
3299/*
3300 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301 *
3302 * Need to remember the current message in case flush_output returns
3303 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003304 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003306int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003308 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003312 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003314
3315 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003316 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003317 ssl_swap_epochs( ssl );
3318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003320 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321
3322 while( ssl->handshake->cur_msg != NULL )
3323 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003325 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326
Hanno Beckere1dcb032018-08-17 16:47:58 +01003327 int const is_finished =
3328 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3329 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3330
Hanno Becker04da1892018-08-14 13:22:10 +01003331 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3332 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3333
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003334 /* Swap epochs before sending Finished: we can't do it after
3335 * sending ChangeCipherSpec, in case write returns WANT_READ.
3336 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003337 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003338 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003340 ssl_swap_epochs( ssl );
3341 }
3342
Hanno Becker67bc7c32018-08-06 11:33:50 +01003343 ret = ssl_get_remaining_payload_in_datagram( ssl );
3344 if( ret < 0 )
3345 return( ret );
3346 max_frag_len = (size_t) ret;
3347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003348 /* CCS is copied as is, while HS messages may need fragmentation */
3349 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3350 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003351 if( max_frag_len == 0 )
3352 {
3353 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3354 return( ret );
3355
3356 continue;
3357 }
3358
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003359 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003360 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003361 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003362
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003363 /* Update position inside current message */
3364 ssl->handshake->cur_msg_p += cur->len;
3365 }
3366 else
3367 {
3368 const unsigned char * const p = ssl->handshake->cur_msg_p;
3369 const size_t hs_len = cur->len - 12;
3370 const size_t frag_off = p - ( cur->p + 12 );
3371 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003372 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
Hanno Beckere1dcb032018-08-17 16:47:58 +01003374 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003375 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003376 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003377 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003378
Hanno Becker67bc7c32018-08-06 11:33:50 +01003379 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3380 return( ret );
3381
3382 continue;
3383 }
3384 max_hs_frag_len = max_frag_len - 12;
3385
3386 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3387 max_hs_frag_len : rem_len;
3388
3389 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003390 {
3391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003392 (unsigned) cur_hs_frag_len,
3393 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003394 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003395
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003396 /* Messages are stored with handshake headers as if not fragmented,
3397 * copy beginning of headers then fill fragmentation fields.
3398 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3399 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003401 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3402 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3403 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3404
Hanno Becker67bc7c32018-08-06 11:33:50 +01003405 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3406 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3407 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003408
3409 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3410
Hanno Becker3f7b9732018-08-28 09:53:25 +01003411 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003412 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3413 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003414 ssl->out_msgtype = cur->type;
3415
3416 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003417 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003418 }
3419
3420 /* If done with the current message move to the next one if any */
3421 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3422 {
3423 if( cur->next != NULL )
3424 {
3425 ssl->handshake->cur_msg = cur->next;
3426 ssl->handshake->cur_msg_p = cur->next->p + 12;
3427 }
3428 else
3429 {
3430 ssl->handshake->cur_msg = NULL;
3431 ssl->handshake->cur_msg_p = NULL;
3432 }
3433 }
3434
3435 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003436 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003439 return( ret );
3440 }
3441 }
3442
Hanno Becker67bc7c32018-08-06 11:33:50 +01003443 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3444 return( ret );
3445
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003446 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3448 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003449 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003452 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3453 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003454
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003456
3457 return( 0 );
3458}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459
3460/*
3461 * To be called when the last message of an incoming flight is received.
3462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464{
3465 /* We won't need to resend that one any more */
3466 ssl_flight_free( ssl->handshake->flight );
3467 ssl->handshake->flight = NULL;
3468 ssl->handshake->cur_msg = NULL;
3469
3470 /* The next incoming flight will start with this msg_seq */
3471 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3472
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003473 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003474 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003475
Hanno Becker0271f962018-08-16 13:23:47 +01003476 /* Clear future message buffering structure. */
3477 ssl_buffering_free( ssl );
3478
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003479 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003480 ssl_set_timer( ssl, 0 );
3481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3483 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003486 }
3487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003489}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003490
3491/*
3492 * To be called when the last message of an outgoing flight is send.
3493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003495{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003496 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003497 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3500 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003503 }
3504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003510 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
3513/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003514 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003515 *
3516 * - fill in handshake headers
3517 * - update handshake checksum
3518 * - DTLS: save message for resending
3519 * - then pass to the record layer
3520 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003521 * DTLS: except for HelloRequest, messages are only queued, and will only be
3522 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003524 * Inputs:
3525 * - ssl->out_msglen: 4 + actual handshake message len
3526 * (4 is the size of handshake headers for TLS)
3527 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3528 * - ssl->out_msg + 4: the handshake message body
3529 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003530 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003531 * - ssl->out_msglen: the length of the record contents
3532 * (including handshake headers but excluding record headers)
3533 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003534 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003535int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003536{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003537 int ret;
3538 const size_t hs_len = ssl->out_msglen - 4;
3539 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3542
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003543 /*
3544 * Sanity checks
3545 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003546 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003547 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3548 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003549 /* In SSLv3, the client might send a NoCertificate alert. */
3550#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3551 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3552 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3553 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3554#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3555 {
3556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3558 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003561 /* Whenever we send anything different from a
3562 * HelloRequest we should be in a handshake - double check. */
3563 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3564 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003565 ssl->handshake == NULL )
3566 {
3567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3568 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003580
Hanno Beckerb50a2532018-08-06 11:52:54 +01003581 /* Double-check that we did not exceed the bounds
3582 * of the outgoing record buffer.
3583 * This should never fail as the various message
3584 * writing functions must obey the bounds of the
3585 * outgoing record buffer, but better be safe.
3586 *
3587 * Note: We deliberately do not check for the MTU or MFL here.
3588 */
3589 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3590 {
3591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3592 "size %u, maximum %u",
3593 (unsigned) ssl->out_msglen,
3594 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3596 }
3597
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003598 /*
3599 * Fill handshake headers
3600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003603 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3604 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3605 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003607 /*
3608 * DTLS has additional fields in the Handshake layer,
3609 * between the length field and the actual payload:
3610 * uint16 message_seq;
3611 * uint24 fragment_offset;
3612 * uint24 fragment_length;
3613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003616 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003617 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003618 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003619 {
3620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3621 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003622 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003623 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3625 }
3626
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003627 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003628 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003629
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003630 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003631 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003632 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003633 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3634 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3635 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003636 }
3637 else
3638 {
3639 ssl->out_msg[4] = 0;
3640 ssl->out_msg[5] = 0;
3641 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003642
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003643 /* Handshake hashes are computed without fragmentation,
3644 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003645 memset( ssl->out_msg + 6, 0x00, 3 );
3646 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003649
Hanno Becker0207e532018-08-28 10:28:28 +01003650 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003651 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3652 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 }
3654
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003655 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003658 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3659 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660 {
3661 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003664 return( ret );
3665 }
3666 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003667 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003668#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003669 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003670 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003671 {
3672 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3673 return( ret );
3674 }
3675 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676
3677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3678
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003679 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003680}
3681
3682/*
3683 * Record layer functions
3684 */
3685
3686/*
3687 * Write current record.
3688 *
3689 * Uses:
3690 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3691 * - ssl->out_msglen: length of the record content (excl headers)
3692 * - ssl->out_msg: record content
3693 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003694int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003695{
3696 int ret, done = 0;
3697 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003698 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003699
3700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003703 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003705 {
3706 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003709 return( ret );
3710 }
3711
3712 len = ssl->out_msglen;
3713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3717 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 ret = mbedtls_ssl_hw_record_write( ssl );
3722 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003726 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003727
3728 if( ret == 0 )
3729 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003732 if( !done )
3733 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003734 unsigned i;
3735 size_t protected_record_size;
3736
Paul Bakker05ef8352012-05-08 09:17:57 +00003737 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003739 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003740
Hanno Becker19859472018-08-06 09:40:20 +01003741 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003742 ssl->out_len[0] = (unsigned char)( len >> 8 );
3743 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003744
Paul Bakker48916f92012-09-16 19:57:18 +00003745 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003746 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003747 mbedtls_record rec;
3748
3749 rec.buf = ssl->out_iv;
3750 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3751 ( ssl->out_iv - ssl->out_buf );
3752 rec.data_len = ssl->out_msglen;
3753 rec.data_offset = ssl->out_msg - rec.buf;
3754
3755 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3756 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3757 ssl->conf->transport, rec.ver );
3758 rec.type = ssl->out_msgtype;
3759
3760 if( ( ret = ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
3761 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003764 return( ret );
3765 }
3766
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003767 if( rec.data_offset != 0 )
3768 {
3769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3771 }
3772
3773 ssl->out_msglen = rec.data_len;
3774 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3775 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003776 }
3777
Hanno Becker2b1e3542018-08-06 11:19:13 +01003778 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3779
3780#if defined(MBEDTLS_SSL_PROTO_DTLS)
3781 /* In case of DTLS, double-check that we don't exceed
3782 * the remaining space in the datagram. */
3783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3784 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003785 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003786 if( ret < 0 )
3787 return( ret );
3788
3789 if( protected_record_size > (size_t) ret )
3790 {
3791 /* Should never happen */
3792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3793 }
3794 }
3795#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003798 "version = [%d:%d], msglen = %d",
3799 ssl->out_hdr[0], ssl->out_hdr[1],
3800 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003803 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003804
3805 ssl->out_left += protected_record_size;
3806 ssl->out_hdr += protected_record_size;
3807 ssl_update_out_pointers( ssl, ssl->transform_out );
3808
Hanno Becker04484622018-08-06 09:49:38 +01003809 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3810 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3811 break;
3812
3813 /* The loop goes to its end iff the counter is wrapping */
3814 if( i == ssl_ep_len( ssl ) )
3815 {
3816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3817 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3818 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 }
3820
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3823 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003825 size_t remaining;
3826 ret = ssl_get_remaining_payload_in_datagram( ssl );
3827 if( ret < 0 )
3828 {
3829 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3830 ret );
3831 return( ret );
3832 }
3833
3834 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003836 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003837 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003838 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 else
3840 {
Hanno Becker513815a2018-08-20 11:56:09 +01003841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003842 }
3843 }
3844#endif /* MBEDTLS_SSL_PROTO_DTLS */
3845
3846 if( ( flush == SSL_FORCE_FLUSH ) &&
3847 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 return( ret );
3851 }
3852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
3855 return( 0 );
3856}
3857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003859
3860static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3861{
3862 if( ssl->in_msglen < ssl->in_hslen ||
3863 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3864 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3865 {
3866 return( 1 );
3867 }
3868 return( 0 );
3869}
Hanno Becker44650b72018-08-16 12:51:11 +01003870
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003871static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003872{
3873 return( ( ssl->in_msg[9] << 16 ) |
3874 ( ssl->in_msg[10] << 8 ) |
3875 ssl->in_msg[11] );
3876}
3877
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003878static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003879{
3880 return( ( ssl->in_msg[6] << 16 ) |
3881 ( ssl->in_msg[7] << 8 ) |
3882 ssl->in_msg[8] );
3883}
3884
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003885static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003886{
3887 uint32_t msg_len, frag_off, frag_len;
3888
3889 msg_len = ssl_get_hs_total_len( ssl );
3890 frag_off = ssl_get_hs_frag_off( ssl );
3891 frag_len = ssl_get_hs_frag_len( ssl );
3892
3893 if( frag_off > msg_len )
3894 return( -1 );
3895
3896 if( frag_len > msg_len - frag_off )
3897 return( -1 );
3898
3899 if( frag_len + 12 > ssl->in_msglen )
3900 return( -1 );
3901
3902 return( 0 );
3903}
3904
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003905/*
3906 * Mark bits in bitmask (used for DTLS HS reassembly)
3907 */
3908static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3909{
3910 unsigned int start_bits, end_bits;
3911
3912 start_bits = 8 - ( offset % 8 );
3913 if( start_bits != 8 )
3914 {
3915 size_t first_byte_idx = offset / 8;
3916
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003917 /* Special case */
3918 if( len <= start_bits )
3919 {
3920 for( ; len != 0; len-- )
3921 mask[first_byte_idx] |= 1 << ( start_bits - len );
3922
3923 /* Avoid potential issues with offset or len becoming invalid */
3924 return;
3925 }
3926
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003927 offset += start_bits; /* Now offset % 8 == 0 */
3928 len -= start_bits;
3929
3930 for( ; start_bits != 0; start_bits-- )
3931 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3932 }
3933
3934 end_bits = len % 8;
3935 if( end_bits != 0 )
3936 {
3937 size_t last_byte_idx = ( offset + len ) / 8;
3938
3939 len -= end_bits; /* Now len % 8 == 0 */
3940
3941 for( ; end_bits != 0; end_bits-- )
3942 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3943 }
3944
3945 memset( mask + offset / 8, 0xFF, len / 8 );
3946}
3947
3948/*
3949 * Check that bitmask is full
3950 */
3951static int ssl_bitmask_check( unsigned char *mask, size_t len )
3952{
3953 size_t i;
3954
3955 for( i = 0; i < len / 8; i++ )
3956 if( mask[i] != 0xFF )
3957 return( -1 );
3958
3959 for( i = 0; i < len % 8; i++ )
3960 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3961 return( -1 );
3962
3963 return( 0 );
3964}
3965
Hanno Becker56e205e2018-08-16 09:06:12 +01003966/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003967static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003968 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969{
Hanno Becker56e205e2018-08-16 09:06:12 +01003970 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003971
Hanno Becker56e205e2018-08-16 09:06:12 +01003972 alloc_len = 12; /* Handshake header */
3973 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003974
Hanno Beckerd07df862018-08-16 09:14:58 +01003975 if( add_bitmap )
3976 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003977
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003978 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003979}
Hanno Becker56e205e2018-08-16 09:06:12 +01003980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003982
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003983static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003984{
3985 return( ( ssl->in_msg[1] << 16 ) |
3986 ( ssl->in_msg[2] << 8 ) |
3987 ssl->in_msg[3] );
3988}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003989
Simon Butcher99000142016-10-13 17:21:01 +01003990int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003995 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003997 }
3998
Hanno Becker12555c62018-08-16 12:47:53 +01003999 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004002 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004003 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004007 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004008 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004009 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004010
Hanno Becker44650b72018-08-16 12:51:11 +01004011 if( ssl_check_hs_header( ssl ) != 0 )
4012 {
4013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4014 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4015 }
4016
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004017 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004018 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4019 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4020 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4021 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004022 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004023 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4024 {
4025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4026 recv_msg_seq,
4027 ssl->handshake->in_msg_seq ) );
4028 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4029 }
4030
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004031 /* Retransmit only on last message from previous flight, to avoid
4032 * too many retransmissions.
4033 * Besides, No sane server ever retransmits HelloVerifyRequest */
4034 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004038 "message_seq = %d, start_of_flight = %d",
4039 recv_msg_seq,
4040 ssl->handshake->in_flight_start_seq ) );
4041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004045 return( ret );
4046 }
4047 }
4048 else
4049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004051 "message_seq = %d, expected = %d",
4052 recv_msg_seq,
4053 ssl->handshake->in_msg_seq ) );
4054 }
4055
Hanno Becker90333da2017-10-10 11:27:13 +01004056 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004057 }
4058 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004059
Hanno Becker6d97ef52018-08-16 13:09:04 +01004060 /* Message reassembly is handled alongside buffering of future
4061 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004062 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004063 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004064 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004067 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004068 }
4069 }
4070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004072 /* With TLS we don't handle fragmentation (for now) */
4073 if( ssl->in_msglen < ssl->in_hslen )
4074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4076 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004077 }
4078
Simon Butcher99000142016-10-13 17:21:01 +01004079 return( 0 );
4080}
4081
4082void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4083{
Hanno Becker0271f962018-08-16 13:23:47 +01004084 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004085
Hanno Becker0271f962018-08-16 13:23:47 +01004086 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004087 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004088 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004089 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004090
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004091 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004094 ssl->handshake != NULL )
4095 {
Hanno Becker0271f962018-08-16 13:23:47 +01004096 unsigned offset;
4097 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004098
Hanno Becker0271f962018-08-16 13:23:47 +01004099 /* Increment handshake sequence number */
4100 hs->in_msg_seq++;
4101
4102 /*
4103 * Clear up handshake buffering and reassembly structure.
4104 */
4105
4106 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004107 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004108
4109 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004110 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4111 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004112 offset++, hs_buf++ )
4113 {
4114 *hs_buf = *(hs_buf + 1);
4115 }
4116
4117 /* Create a fresh last entry */
4118 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004119 }
4120#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004121}
4122
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004123/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004124 * DTLS anti-replay: RFC 6347 4.1.2.6
4125 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004126 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4127 * Bit n is set iff record number in_window_top - n has been seen.
4128 *
4129 * Usually, in_window_top is the last record number seen and the lsb of
4130 * in_window is set. The only exception is the initial state (record number 0
4131 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4134static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004135{
4136 ssl->in_window_top = 0;
4137 ssl->in_window = 0;
4138}
4139
4140static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4141{
4142 return( ( (uint64_t) buf[0] << 40 ) |
4143 ( (uint64_t) buf[1] << 32 ) |
4144 ( (uint64_t) buf[2] << 24 ) |
4145 ( (uint64_t) buf[3] << 16 ) |
4146 ( (uint64_t) buf[4] << 8 ) |
4147 ( (uint64_t) buf[5] ) );
4148}
4149
4150/*
4151 * Return 0 if sequence number is acceptable, -1 otherwise
4152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004154{
4155 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4156 uint64_t bit;
4157
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004158 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004159 return( 0 );
4160
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004161 if( rec_seqnum > ssl->in_window_top )
4162 return( 0 );
4163
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004164 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004165
4166 if( bit >= 64 )
4167 return( -1 );
4168
4169 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4170 return( -1 );
4171
4172 return( 0 );
4173}
4174
4175/*
4176 * Update replay window on new validated record
4177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004179{
4180 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4181
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004182 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004183 return;
4184
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004185 if( rec_seqnum > ssl->in_window_top )
4186 {
4187 /* Update window_top and the contents of the window */
4188 uint64_t shift = rec_seqnum - ssl->in_window_top;
4189
4190 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004191 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004192 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004193 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004194 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004195 ssl->in_window |= 1;
4196 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004197
4198 ssl->in_window_top = rec_seqnum;
4199 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004200 else
4201 {
4202 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004203 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004204
4205 if( bit < 64 ) /* Always true, but be extra sure */
4206 ssl->in_window |= (uint64_t) 1 << bit;
4207 }
4208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004210
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004211#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004212/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004213static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4214
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004215/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004216 * Without any SSL context, check if a datagram looks like a ClientHello with
4217 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004218 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004219 *
4220 * - if cookie is valid, return 0
4221 * - if ClientHello looks superficially valid but cookie is not,
4222 * fill obuf and set olen, then
4223 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4224 * - otherwise return a specific error code
4225 */
4226static int ssl_check_dtls_clihlo_cookie(
4227 mbedtls_ssl_cookie_write_t *f_cookie_write,
4228 mbedtls_ssl_cookie_check_t *f_cookie_check,
4229 void *p_cookie,
4230 const unsigned char *cli_id, size_t cli_id_len,
4231 const unsigned char *in, size_t in_len,
4232 unsigned char *obuf, size_t buf_len, size_t *olen )
4233{
4234 size_t sid_len, cookie_len;
4235 unsigned char *p;
4236
4237 if( f_cookie_write == NULL || f_cookie_check == NULL )
4238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4239
4240 /*
4241 * Structure of ClientHello with record and handshake headers,
4242 * and expected values. We don't need to check a lot, more checks will be
4243 * done when actually parsing the ClientHello - skipping those checks
4244 * avoids code duplication and does not make cookie forging any easier.
4245 *
4246 * 0-0 ContentType type; copied, must be handshake
4247 * 1-2 ProtocolVersion version; copied
4248 * 3-4 uint16 epoch; copied, must be 0
4249 * 5-10 uint48 sequence_number; copied
4250 * 11-12 uint16 length; (ignored)
4251 *
4252 * 13-13 HandshakeType msg_type; (ignored)
4253 * 14-16 uint24 length; (ignored)
4254 * 17-18 uint16 message_seq; copied
4255 * 19-21 uint24 fragment_offset; copied, must be 0
4256 * 22-24 uint24 fragment_length; (ignored)
4257 *
4258 * 25-26 ProtocolVersion client_version; (ignored)
4259 * 27-58 Random random; (ignored)
4260 * 59-xx SessionID session_id; 1 byte len + sid_len content
4261 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4262 * ...
4263 *
4264 * Minimum length is 61 bytes.
4265 */
4266 if( in_len < 61 ||
4267 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4268 in[3] != 0 || in[4] != 0 ||
4269 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4270 {
4271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4272 }
4273
4274 sid_len = in[59];
4275 if( sid_len > in_len - 61 )
4276 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4277
4278 cookie_len = in[60 + sid_len];
4279 if( cookie_len > in_len - 60 )
4280 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4281
4282 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4283 cli_id, cli_id_len ) == 0 )
4284 {
4285 /* Valid cookie */
4286 return( 0 );
4287 }
4288
4289 /*
4290 * If we get here, we've got an invalid cookie, let's prepare HVR.
4291 *
4292 * 0-0 ContentType type; copied
4293 * 1-2 ProtocolVersion version; copied
4294 * 3-4 uint16 epoch; copied
4295 * 5-10 uint48 sequence_number; copied
4296 * 11-12 uint16 length; olen - 13
4297 *
4298 * 13-13 HandshakeType msg_type; hello_verify_request
4299 * 14-16 uint24 length; olen - 25
4300 * 17-18 uint16 message_seq; copied
4301 * 19-21 uint24 fragment_offset; copied
4302 * 22-24 uint24 fragment_length; olen - 25
4303 *
4304 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4305 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4306 *
4307 * Minimum length is 28.
4308 */
4309 if( buf_len < 28 )
4310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4311
4312 /* Copy most fields and adapt others */
4313 memcpy( obuf, in, 25 );
4314 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4315 obuf[25] = 0xfe;
4316 obuf[26] = 0xff;
4317
4318 /* Generate and write actual cookie */
4319 p = obuf + 28;
4320 if( f_cookie_write( p_cookie,
4321 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4322 {
4323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4324 }
4325
4326 *olen = p - obuf;
4327
4328 /* Go back and fill length fields */
4329 obuf[27] = (unsigned char)( *olen - 28 );
4330
4331 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4332 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4333 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4334
4335 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4336 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4337
4338 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4339}
4340
4341/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004342 * Handle possible client reconnect with the same UDP quadruplet
4343 * (RFC 6347 Section 4.2.8).
4344 *
4345 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4346 * that looks like a ClientHello.
4347 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004348 * - if the input looks like a ClientHello without cookies,
4349 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004350 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004351 * - if the input looks like a ClientHello with a valid cookie,
4352 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004353 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004354 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004355 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004356 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004357 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4358 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004359 */
4360static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4361{
4362 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004363 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004364
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004365 ret = ssl_check_dtls_clihlo_cookie(
4366 ssl->conf->f_cookie_write,
4367 ssl->conf->f_cookie_check,
4368 ssl->conf->p_cookie,
4369 ssl->cli_id, ssl->cli_id_len,
4370 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004371 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004372
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4374
4375 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004377 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004378 * If the error is permanent we'll catch it later,
4379 * if it's not, then hopefully it'll work next time. */
4380 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4381
4382 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004383 }
4384
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004386 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004387 /* Got a valid cookie, partially reset context */
4388 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4389 {
4390 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4391 return( ret );
4392 }
4393
4394 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004395 }
4396
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004397 return( ret );
4398}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004399#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004400
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004401/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004402 * ContentType type;
4403 * ProtocolVersion version;
4404 * uint16 epoch; // DTLS only
4405 * uint48 sequence_number; // DTLS only
4406 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004407 *
4408 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004409 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004410 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4411 *
4412 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004413 * 1. proceed with the record if this function returns 0
4414 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4415 * 3. return CLIENT_RECONNECT if this function return that value
4416 * 4. drop the whole datagram if this function returns anything else.
4417 * Point 2 is needed when the peer is resending, and we have already received
4418 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004421{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004422 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 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 +02004425
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004427 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004428 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004432 ssl->in_msgtype,
4433 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004435 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4437 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4438 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4439 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004442
4443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004444 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4445 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004446 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4447#endif /* MBEDTLS_SSL_PROTO_DTLS */
4448 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4449 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004452 }
4453
4454 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004455 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4458 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 }
4460
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004461 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4464 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004465 }
4466
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004467 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004468 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004469 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4472 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004473 }
4474
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004475 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004476 * DTLS-related tests.
4477 * Check epoch before checking length constraint because
4478 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4479 * message gets duplicated before the corresponding Finished message,
4480 * the second ChangeCipherSpec should be discarded because it belongs
4481 * to an old epoch, but not because its length is shorter than
4482 * the minimum record length for packets using the new record transform.
4483 * Note that these two kinds of failures are handled differently,
4484 * as an unexpected record is silently skipped but an invalid
4485 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004486 */
4487#if defined(MBEDTLS_SSL_PROTO_DTLS)
4488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4489 {
4490 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4491
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004492 /* Check epoch (and sequence number) with DTLS */
4493 if( rec_epoch != ssl->in_epoch )
4494 {
4495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4496 "expected %d, received %d",
4497 ssl->in_epoch, rec_epoch ) );
4498
4499#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4500 /*
4501 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4502 * access the first byte of record content (handshake type), as we
4503 * have an active transform (possibly iv_len != 0), so use the
4504 * fact that the record header len is 13 instead.
4505 */
4506 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4507 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4508 rec_epoch == 0 &&
4509 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4510 ssl->in_left > 13 &&
4511 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4512 {
4513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4514 "from the same port" ) );
4515 return( ssl_handle_possible_reconnect( ssl ) );
4516 }
4517 else
4518#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004519 {
4520 /* Consider buffering the record. */
4521 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4522 {
4523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4524 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4525 }
4526
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004527 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004528 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004529 }
4530
4531#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4532 /* Replay detection only works for the current epoch */
4533 if( rec_epoch == ssl->in_epoch &&
4534 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4537 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4538 }
4539#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004540
Hanno Becker52c6dc62017-05-26 16:07:36 +01004541 /* Drop unexpected ApplicationData records,
4542 * except at the beginning of renegotiations */
4543 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4544 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4545#if defined(MBEDTLS_SSL_RENEGOTIATION)
4546 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4547 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4548#endif
4549 )
4550 {
4551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4552 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4553 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004554 }
4555#endif /* MBEDTLS_SSL_PROTO_DTLS */
4556
Hanno Becker52c6dc62017-05-26 16:07:36 +01004557
4558 /* Check length against bounds of the current transform and version */
4559 if( ssl->transform_in == NULL )
4560 {
4561 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004562 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004563 {
4564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4565 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4566 }
4567 }
4568 else
4569 {
4570 if( ssl->in_msglen < ssl->transform_in->minlen )
4571 {
4572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4573 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4574 }
4575
4576#if defined(MBEDTLS_SSL_PROTO_SSL3)
4577 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004578 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004579 {
4580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4581 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4582 }
4583#endif
4584#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4585 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4586 /*
4587 * TLS encrypted messages can have up to 256 bytes of padding
4588 */
4589 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4590 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004591 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004592 {
4593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4594 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4595 }
4596#endif
4597 }
4598
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004599 return( 0 );
4600}
Paul Bakker5121ce52009-01-03 21:22:43 +00004601
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004602/*
4603 * If applicable, decrypt (and decompress) record content
4604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004606{
4607 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4610 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4613 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 ret = mbedtls_ssl_hw_record_read( ssl );
4618 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4621 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004622 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004623
4624 if( ret == 0 )
4625 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004628 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 {
4630 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 return( ret );
4634 }
4635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 ssl->in_msg, ssl->in_msglen );
4638
Angus Grattond8213d02016-05-25 20:56:48 +10004639 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4642 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 }
4644 }
4645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004647 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004649 {
4650 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004653 return( ret );
4654 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004662 }
4663#endif
4664
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004665 return( 0 );
4666}
4667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004669
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004670/*
4671 * Read a record.
4672 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004673 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4674 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4675 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004676 */
Hanno Becker1097b342018-08-15 14:09:41 +01004677
4678/* Helper functions for mbedtls_ssl_read_record(). */
4679static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004680static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4681static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004682
Hanno Becker327c93b2018-08-15 13:56:18 +01004683int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004684 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004685{
4686 int ret;
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004689
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004690 if( ssl->keep_current_message == 0 )
4691 {
4692 do {
Simon Butcher99000142016-10-13 17:21:01 +01004693
Hanno Becker26994592018-08-15 14:14:59 +01004694 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004695 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004696 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004697
Hanno Beckere74d5562018-08-15 14:26:08 +01004698 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004699 {
Hanno Becker40f50842018-08-15 14:48:01 +01004700#if defined(MBEDTLS_SSL_PROTO_DTLS)
4701 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004702
Hanno Becker40f50842018-08-15 14:48:01 +01004703 /* We only check for buffered messages if the
4704 * current datagram is fully consumed. */
4705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004706 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004707 {
Hanno Becker40f50842018-08-15 14:48:01 +01004708 if( ssl_load_buffered_message( ssl ) == 0 )
4709 have_buffered = 1;
4710 }
4711
4712 if( have_buffered == 0 )
4713#endif /* MBEDTLS_SSL_PROTO_DTLS */
4714 {
4715 ret = ssl_get_next_record( ssl );
4716 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4717 continue;
4718
4719 if( ret != 0 )
4720 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004721 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004722 return( ret );
4723 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004724 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004725 }
4726
4727 ret = mbedtls_ssl_handle_message_type( ssl );
4728
Hanno Becker40f50842018-08-15 14:48:01 +01004729#if defined(MBEDTLS_SSL_PROTO_DTLS)
4730 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4731 {
4732 /* Buffer future message */
4733 ret = ssl_buffer_message( ssl );
4734 if( ret != 0 )
4735 return( ret );
4736
4737 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4738 }
4739#endif /* MBEDTLS_SSL_PROTO_DTLS */
4740
Hanno Becker90333da2017-10-10 11:27:13 +01004741 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4742 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004743
4744 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004745 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004746 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004747 return( ret );
4748 }
4749
Hanno Becker327c93b2018-08-15 13:56:18 +01004750 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004751 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004752 {
4753 mbedtls_ssl_update_handshake_status( ssl );
4754 }
Simon Butcher99000142016-10-13 17:21:01 +01004755 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004756 else
Simon Butcher99000142016-10-13 17:21:01 +01004757 {
Hanno Becker02f59072018-08-15 14:00:24 +01004758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004759 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004760 }
4761
4762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4763
4764 return( 0 );
4765}
4766
Hanno Becker40f50842018-08-15 14:48:01 +01004767#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004768static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004769{
Hanno Becker40f50842018-08-15 14:48:01 +01004770 if( ssl->in_left > ssl->next_record_offset )
4771 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004772
Hanno Becker40f50842018-08-15 14:48:01 +01004773 return( 0 );
4774}
4775
4776static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4777{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004778 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004779 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004780 int ret = 0;
4781
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004782 if( hs == NULL )
4783 return( -1 );
4784
Hanno Beckere00ae372018-08-20 09:39:42 +01004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4786
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004787 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4788 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4789 {
4790 /* Check if we have seen a ChangeCipherSpec before.
4791 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004792 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4795 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004796 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004797 }
4798
Hanno Becker39b8bc92018-08-28 17:17:13 +01004799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004800 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4801 ssl->in_msglen = 1;
4802 ssl->in_msg[0] = 1;
4803
4804 /* As long as they are equal, the exact value doesn't matter. */
4805 ssl->in_left = 0;
4806 ssl->next_record_offset = 0;
4807
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004808 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004809 goto exit;
4810 }
Hanno Becker37f95322018-08-16 13:55:32 +01004811
Hanno Beckerb8f50142018-08-28 10:01:34 +01004812#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004813 /* Debug only */
4814 {
4815 unsigned offset;
4816 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4817 {
4818 hs_buf = &hs->buffering.hs[offset];
4819 if( hs_buf->is_valid == 1 )
4820 {
4821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4822 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004823 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004824 }
4825 }
4826 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004827#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004828
4829 /* Check if we have buffered and/or fully reassembled the
4830 * next handshake message. */
4831 hs_buf = &hs->buffering.hs[0];
4832 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4833 {
4834 /* Synthesize a record containing the buffered HS message. */
4835 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4836 ( hs_buf->data[2] << 8 ) |
4837 hs_buf->data[3];
4838
4839 /* Double-check that we haven't accidentally buffered
4840 * a message that doesn't fit into the input buffer. */
4841 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4842 {
4843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4845 }
4846
4847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4848 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4849 hs_buf->data, msg_len + 12 );
4850
4851 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4852 ssl->in_hslen = msg_len + 12;
4853 ssl->in_msglen = msg_len + 12;
4854 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4855
4856 ret = 0;
4857 goto exit;
4858 }
4859 else
4860 {
4861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4862 hs->in_msg_seq ) );
4863 }
4864
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004865 ret = -1;
4866
4867exit:
4868
4869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4870 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004871}
4872
Hanno Beckera02b0b42018-08-21 17:20:27 +01004873static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4874 size_t desired )
4875{
4876 int offset;
4877 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4879 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004880
Hanno Becker01315ea2018-08-21 17:22:17 +01004881 /* Get rid of future records epoch first, if such exist. */
4882 ssl_free_buffered_record( ssl );
4883
4884 /* Check if we have enough space available now. */
4885 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4886 hs->buffering.total_bytes_buffered ) )
4887 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004889 return( 0 );
4890 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004891
Hanno Becker4f432ad2018-08-28 10:02:32 +01004892 /* We don't have enough space to buffer the next expected handshake
4893 * message. Remove buffers used for future messages to gain space,
4894 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004895 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4896 offset >= 0; offset-- )
4897 {
4898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4899 offset ) );
4900
Hanno Beckerb309b922018-08-23 13:18:05 +01004901 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004902
4903 /* Check if we have enough space available now. */
4904 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4905 hs->buffering.total_bytes_buffered ) )
4906 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004908 return( 0 );
4909 }
4910 }
4911
4912 return( -1 );
4913}
4914
Hanno Becker40f50842018-08-15 14:48:01 +01004915static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4916{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004917 int ret = 0;
4918 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4919
4920 if( hs == NULL )
4921 return( 0 );
4922
4923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4924
4925 switch( ssl->in_msgtype )
4926 {
4927 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004929
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004930 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004931 break;
4932
4933 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004934 {
4935 unsigned recv_msg_seq_offset;
4936 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4937 mbedtls_ssl_hs_buffer *hs_buf;
4938 size_t msg_len = ssl->in_hslen - 12;
4939
4940 /* We should never receive an old handshake
4941 * message - double-check nonetheless. */
4942 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4943 {
4944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4946 }
4947
4948 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4949 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4950 {
4951 /* Silently ignore -- message too far in the future */
4952 MBEDTLS_SSL_DEBUG_MSG( 2,
4953 ( "Ignore future HS message with sequence number %u, "
4954 "buffering window %u - %u",
4955 recv_msg_seq, ssl->handshake->in_msg_seq,
4956 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4957
4958 goto exit;
4959 }
4960
4961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4962 recv_msg_seq, recv_msg_seq_offset ) );
4963
4964 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4965
4966 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004967 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004968 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004969 size_t reassembly_buf_sz;
4970
Hanno Becker37f95322018-08-16 13:55:32 +01004971 hs_buf->is_fragmented =
4972 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4973
4974 /* We copy the message back into the input buffer
4975 * after reassembly, so check that it's not too large.
4976 * This is an implementation-specific limitation
4977 * and not one from the standard, hence it is not
4978 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004979 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004980 {
4981 /* Ignore message */
4982 goto exit;
4983 }
4984
Hanno Beckere0b150f2018-08-21 15:51:03 +01004985 /* Check if we have enough space to buffer the message. */
4986 if( hs->buffering.total_bytes_buffered >
4987 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4988 {
4989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4991 }
4992
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004993 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4994 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004995
4996 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4997 hs->buffering.total_bytes_buffered ) )
4998 {
4999 if( recv_msg_seq_offset > 0 )
5000 {
5001 /* If we can't buffer a future message because
5002 * of space limitations -- ignore. */
5003 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",
5004 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5005 (unsigned) hs->buffering.total_bytes_buffered ) );
5006 goto exit;
5007 }
Hanno Beckere1801392018-08-21 16:51:05 +01005008 else
5009 {
5010 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",
5011 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5012 (unsigned) hs->buffering.total_bytes_buffered ) );
5013 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005014
Hanno Beckera02b0b42018-08-21 17:20:27 +01005015 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005016 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005017 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",
5018 (unsigned) msg_len,
5019 (unsigned) reassembly_buf_sz,
5020 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005021 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005022 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5023 goto exit;
5024 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005025 }
5026
5027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5028 msg_len ) );
5029
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005030 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5031 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005032 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005033 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005034 goto exit;
5035 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005036 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005037
5038 /* Prepare final header: copy msg_type, length and message_seq,
5039 * then add standardised fragment_offset and fragment_length */
5040 memcpy( hs_buf->data, ssl->in_msg, 6 );
5041 memset( hs_buf->data + 6, 0, 3 );
5042 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5043
5044 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005045
5046 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005047 }
5048 else
5049 {
5050 /* Make sure msg_type and length are consistent */
5051 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5052 {
5053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5054 /* Ignore */
5055 goto exit;
5056 }
5057 }
5058
Hanno Becker4422bbb2018-08-20 09:40:19 +01005059 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005060 {
5061 size_t frag_len, frag_off;
5062 unsigned char * const msg = hs_buf->data + 12;
5063
5064 /*
5065 * Check and copy current fragment
5066 */
5067
5068 /* Validation of header fields already done in
5069 * mbedtls_ssl_prepare_handshake_record(). */
5070 frag_off = ssl_get_hs_frag_off( ssl );
5071 frag_len = ssl_get_hs_frag_len( ssl );
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5074 frag_off, frag_len ) );
5075 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5076
5077 if( hs_buf->is_fragmented )
5078 {
5079 unsigned char * const bitmask = msg + msg_len;
5080 ssl_bitmask_set( bitmask, frag_off, frag_len );
5081 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5082 msg_len ) == 0 );
5083 }
5084 else
5085 {
5086 hs_buf->is_complete = 1;
5087 }
5088
5089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5090 hs_buf->is_complete ? "" : "not yet " ) );
5091 }
5092
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005094 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095
5096 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005097 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 break;
5099 }
5100
5101exit:
5102
5103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5104 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005105}
5106#endif /* MBEDTLS_SSL_PROTO_DTLS */
5107
Hanno Becker1097b342018-08-15 14:09:41 +01005108static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005109{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005110 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005111 * Consume last content-layer message and potentially
5112 * update in_msglen which keeps track of the contents'
5113 * consumption state.
5114 *
5115 * (1) Handshake messages:
5116 * Remove last handshake message, move content
5117 * and adapt in_msglen.
5118 *
5119 * (2) Alert messages:
5120 * Consume whole record content, in_msglen = 0.
5121 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005122 * (3) Change cipher spec:
5123 * Consume whole record content, in_msglen = 0.
5124 *
5125 * (4) Application data:
5126 * Don't do anything - the record layer provides
5127 * the application data as a stream transport
5128 * and consumes through mbedtls_ssl_read only.
5129 *
5130 */
5131
5132 /* Case (1): Handshake messages */
5133 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005134 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005135 /* Hard assertion to be sure that no application data
5136 * is in flight, as corrupting ssl->in_msglen during
5137 * ssl->in_offt != NULL is fatal. */
5138 if( ssl->in_offt != NULL )
5139 {
5140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5142 }
5143
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005144 /*
5145 * Get next Handshake message in the current record
5146 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005147
Hanno Becker4a810fb2017-05-24 16:27:30 +01005148 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005149 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005150 * current handshake content: If DTLS handshake
5151 * fragmentation is used, that's the fragment
5152 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005153 * size here is faulty and should be changed at
5154 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005155 * (2) While it doesn't seem to cause problems, one
5156 * has to be very careful not to assume that in_hslen
5157 * is always <= in_msglen in a sensible communication.
5158 * Again, it's wrong for DTLS handshake fragmentation.
5159 * The following check is therefore mandatory, and
5160 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005161 * Additionally, ssl->in_hslen might be arbitrarily out of
5162 * bounds after handling a DTLS message with an unexpected
5163 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005164 */
5165 if( ssl->in_hslen < ssl->in_msglen )
5166 {
5167 ssl->in_msglen -= ssl->in_hslen;
5168 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5169 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005170
Hanno Becker4a810fb2017-05-24 16:27:30 +01005171 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5172 ssl->in_msg, ssl->in_msglen );
5173 }
5174 else
5175 {
5176 ssl->in_msglen = 0;
5177 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005178
Hanno Becker4a810fb2017-05-24 16:27:30 +01005179 ssl->in_hslen = 0;
5180 }
5181 /* Case (4): Application data */
5182 else if( ssl->in_offt != NULL )
5183 {
5184 return( 0 );
5185 }
5186 /* Everything else (CCS & Alerts) */
5187 else
5188 {
5189 ssl->in_msglen = 0;
5190 }
5191
Hanno Becker1097b342018-08-15 14:09:41 +01005192 return( 0 );
5193}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005194
Hanno Beckere74d5562018-08-15 14:26:08 +01005195static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5196{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005197 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005198 return( 1 );
5199
5200 return( 0 );
5201}
5202
Hanno Becker5f066e72018-08-16 14:56:31 +01005203#if defined(MBEDTLS_SSL_PROTO_DTLS)
5204
5205static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5206{
5207 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5208 if( hs == NULL )
5209 return;
5210
Hanno Becker01315ea2018-08-21 17:22:17 +01005211 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005212 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005213 hs->buffering.total_bytes_buffered -=
5214 hs->buffering.future_record.len;
5215
5216 mbedtls_free( hs->buffering.future_record.data );
5217 hs->buffering.future_record.data = NULL;
5218 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005219}
5220
5221static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5222{
5223 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5224 unsigned char * rec;
5225 size_t rec_len;
5226 unsigned rec_epoch;
5227
5228 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5229 return( 0 );
5230
5231 if( hs == NULL )
5232 return( 0 );
5233
Hanno Becker5f066e72018-08-16 14:56:31 +01005234 rec = hs->buffering.future_record.data;
5235 rec_len = hs->buffering.future_record.len;
5236 rec_epoch = hs->buffering.future_record.epoch;
5237
5238 if( rec == NULL )
5239 return( 0 );
5240
Hanno Becker4cb782d2018-08-20 11:19:05 +01005241 /* Only consider loading future records if the
5242 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005243 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005244 return( 0 );
5245
Hanno Becker5f066e72018-08-16 14:56:31 +01005246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5247
5248 if( rec_epoch != ssl->in_epoch )
5249 {
5250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5251 goto exit;
5252 }
5253
5254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5255
5256 /* Double-check that the record is not too large */
5257 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5258 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5262 }
5263
5264 memcpy( ssl->in_hdr, rec, rec_len );
5265 ssl->in_left = rec_len;
5266 ssl->next_record_offset = 0;
5267
5268 ssl_free_buffered_record( ssl );
5269
5270exit:
5271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5272 return( 0 );
5273}
5274
5275static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5276{
5277 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5278 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005279 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005280
5281 /* Don't buffer future records outside handshakes. */
5282 if( hs == NULL )
5283 return( 0 );
5284
5285 /* Only buffer handshake records (we are only interested
5286 * in Finished messages). */
5287 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5288 return( 0 );
5289
5290 /* Don't buffer more than one future epoch record. */
5291 if( hs->buffering.future_record.data != NULL )
5292 return( 0 );
5293
Hanno Becker01315ea2018-08-21 17:22:17 +01005294 /* Don't buffer record if there's not enough buffering space remaining. */
5295 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5296 hs->buffering.total_bytes_buffered ) )
5297 {
5298 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",
5299 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5300 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005301 return( 0 );
5302 }
5303
Hanno Becker5f066e72018-08-16 14:56:31 +01005304 /* Buffer record */
5305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5306 ssl->in_epoch + 1 ) );
5307 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5308 rec_hdr_len + ssl->in_msglen );
5309
5310 /* ssl_parse_record_header() only considers records
5311 * of the next epoch as candidates for buffering. */
5312 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005313 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005314
5315 hs->buffering.future_record.data =
5316 mbedtls_calloc( 1, hs->buffering.future_record.len );
5317 if( hs->buffering.future_record.data == NULL )
5318 {
5319 /* If we run out of RAM trying to buffer a
5320 * record from the next epoch, just ignore. */
5321 return( 0 );
5322 }
5323
Hanno Becker01315ea2018-08-21 17:22:17 +01005324 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005325
Hanno Becker01315ea2018-08-21 17:22:17 +01005326 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005327 return( 0 );
5328}
5329
5330#endif /* MBEDTLS_SSL_PROTO_DTLS */
5331
Hanno Beckere74d5562018-08-15 14:26:08 +01005332static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005333{
5334 int ret;
5335
Hanno Becker5f066e72018-08-16 14:56:31 +01005336#if defined(MBEDTLS_SSL_PROTO_DTLS)
5337 /* We might have buffered a future record; if so,
5338 * and if the epoch matches now, load it.
5339 * On success, this call will set ssl->in_left to
5340 * the length of the buffered record, so that
5341 * the calls to ssl_fetch_input() below will
5342 * essentially be no-ops. */
5343 ret = ssl_load_buffered_record( ssl );
5344 if( ret != 0 )
5345 return( ret );
5346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005351 return( ret );
5352 }
5353
5354 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5358 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005359 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005360 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5361 {
5362 ret = ssl_buffer_future_record( ssl );
5363 if( ret != 0 )
5364 return( ret );
5365
5366 /* Fall through to handling of unexpected records */
5367 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5368 }
5369
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005370 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5371 {
5372 /* Skip unexpected record (but not whole datagram) */
5373 ssl->next_record_offset = ssl->in_msglen
5374 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005375
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5377 "(header)" ) );
5378 }
5379 else
5380 {
5381 /* Skip invalid record and the rest of the datagram */
5382 ssl->next_record_offset = 0;
5383 ssl->in_left = 0;
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5386 "(header)" ) );
5387 }
5388
5389 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005390 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005391 }
5392#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005393 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005394 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005395
5396 /*
5397 * Read and optionally decrypt the message contents
5398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5400 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005403 return( ret );
5404 }
5405
5406 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005411 if( ssl->next_record_offset < ssl->in_left )
5412 {
5413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5414 }
5415 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005416 else
5417#endif
5418 ssl->in_left = 0;
5419
5420 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005424 {
5425 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5427 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005428 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005429 /* Except when waiting for Finished as a bad mac here
5430 * probably means something went wrong in the handshake
5431 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5432 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5433 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5434 {
5435#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5436 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5437 {
5438 mbedtls_ssl_send_alert_message( ssl,
5439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5440 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5441 }
5442#endif
5443 return( ret );
5444 }
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005447 if( ssl->conf->badmac_limit != 0 &&
5448 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5451 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005452 }
5453#endif
5454
Hanno Becker4a810fb2017-05-24 16:27:30 +01005455 /* As above, invalid records cause
5456 * dismissal of the whole datagram. */
5457
5458 ssl->next_record_offset = 0;
5459 ssl->in_left = 0;
5460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005462 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005463 }
5464
5465 return( ret );
5466 }
5467 else
5468#endif
5469 {
5470 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5472 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 mbedtls_ssl_send_alert_message( ssl,
5475 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5476 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005477 }
5478#endif
5479 return( ret );
5480 }
5481 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005482
Simon Butcher99000142016-10-13 17:21:01 +01005483 return( 0 );
5484}
5485
5486int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5487{
5488 int ret;
5489
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005490 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005491 * Handle particular types of records
5492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 {
Simon Butcher99000142016-10-13 17:21:01 +01005495 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5496 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005497 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 }
5500
Hanno Beckere678eaa2018-08-21 14:57:46 +01005501 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005502 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005503 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005504 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5506 ssl->in_msglen ) );
5507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005508 }
5509
Hanno Beckere678eaa2018-08-21 14:57:46 +01005510 if( ssl->in_msg[0] != 1 )
5511 {
5512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5513 ssl->in_msg[0] ) );
5514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5515 }
5516
5517#if defined(MBEDTLS_SSL_PROTO_DTLS)
5518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5519 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5520 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5521 {
5522 if( ssl->handshake == NULL )
5523 {
5524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5525 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5526 }
5527
5528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5530 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005531#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005532 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005536 if( ssl->in_msglen != 2 )
5537 {
5538 /* Note: Standard allows for more than one 2 byte alert
5539 to be packed in a single message, but Mbed TLS doesn't
5540 currently support this. */
5541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5542 ssl->in_msglen ) );
5543 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5544 }
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 ssl->in_msg[0], ssl->in_msg[1] ) );
5548
5549 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005550 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005555 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 }
5558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5560 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5563 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005565
5566#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5567 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5568 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5569 {
Hanno Becker90333da2017-10-10 11:27:13 +01005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005571 /* Will be handled when trying to parse ServerHello */
5572 return( 0 );
5573 }
5574#endif
5575
5576#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5577 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5578 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5579 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5580 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5581 {
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5583 /* Will be handled in mbedtls_ssl_parse_certificate() */
5584 return( 0 );
5585 }
5586#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5587
5588 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005589 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 }
5591
Hanno Beckerc76c6192017-06-06 10:03:17 +01005592#if defined(MBEDTLS_SSL_PROTO_DTLS)
5593 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5594 ssl->handshake != NULL &&
5595 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5596 {
5597 ssl_handshake_wrapup_free_hs_transform( ssl );
5598 }
5599#endif
5600
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 return( 0 );
5602}
5603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005605{
5606 int ret;
5607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5609 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5610 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005611 {
5612 return( ret );
5613 }
5614
5615 return( 0 );
5616}
5617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005619 unsigned char level,
5620 unsigned char message )
5621{
5622 int ret;
5623
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005624 if( ssl == NULL || ssl->conf == NULL )
5625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005631 ssl->out_msglen = 2;
5632 ssl->out_msg[0] = level;
5633 ssl->out_msg[1] = message;
5634
Hanno Becker67bc7c32018-08-06 11:33:50 +01005635 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005638 return( ret );
5639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005641
5642 return( 0 );
5643}
5644
Hanno Beckerb9d44792019-02-08 07:19:04 +00005645#if defined(MBEDTLS_X509_CRT_PARSE_C)
5646static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5647{
5648#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5649 if( session->peer_cert != NULL )
5650 {
5651 mbedtls_x509_crt_free( session->peer_cert );
5652 mbedtls_free( session->peer_cert );
5653 session->peer_cert = NULL;
5654 }
5655#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5656 if( session->peer_cert_digest != NULL )
5657 {
5658 /* Zeroization is not necessary. */
5659 mbedtls_free( session->peer_cert_digest );
5660 session->peer_cert_digest = NULL;
5661 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5662 session->peer_cert_digest_len = 0;
5663 }
5664#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5665}
5666#endif /* MBEDTLS_X509_CRT_PARSE_C */
5667
Paul Bakker5121ce52009-01-03 21:22:43 +00005668/*
5669 * Handshake functions
5670 */
Hanno Becker21489932019-02-05 13:20:55 +00005671#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005672/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005674{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005675 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5676 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005679
Hanno Becker7177a882019-02-05 13:36:46 +00005680 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005683 ssl->state++;
5684 return( 0 );
5685 }
5686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005689}
5690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005692{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005693 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5694 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005697
Hanno Becker7177a882019-02-05 13:36:46 +00005698 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005701 ssl->state++;
5702 return( 0 );
5703 }
5704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005707}
Gilles Peskinef9828522017-05-03 12:28:43 +02005708
Hanno Becker21489932019-02-05 13:20:55 +00005709#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005710/* Some certificate support -> implement write and parse */
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005713{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005715 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005717 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5718 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005721
Hanno Becker7177a882019-02-05 13:36:46 +00005722 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005725 ssl->state++;
5726 return( 0 );
5727 }
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005731 {
5732 if( ssl->client_auth == 0 )
5733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 ssl->state++;
5736 return( 0 );
5737 }
5738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 /*
5741 * If using SSLv3 and got no cert, send an Alert message
5742 * (otherwise an empty Certificate message will be sent).
5743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5745 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005746 {
5747 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5749 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5750 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 goto write_msg;
5754 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757#endif /* MBEDTLS_SSL_CLI_C */
5758#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005759 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5764 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 }
5766 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005767#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
5771 /*
5772 * 0 . 0 handshake type
5773 * 1 . 3 handshake length
5774 * 4 . 6 length of all certs
5775 * 7 . 9 length of cert. 1
5776 * 10 . n-1 peer certificate
5777 * n . n+2 length of cert. 2
5778 * n+3 . ... upper level cert, etc.
5779 */
5780 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782
Paul Bakker29087132010-03-21 21:03:34 +00005783 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 {
5785 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005786 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005789 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 }
5792
5793 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5794 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5795 ssl->out_msg[i + 2] = (unsigned char)( n );
5796
5797 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5798 i += n; crt = crt->next;
5799 }
5800
5801 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5802 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5803 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5804
5805 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5807 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005808
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005809#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005810write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005811#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005812
5813 ssl->state++;
5814
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005815 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 return( ret );
5819 }
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005822
Paul Bakkered27a042013-04-18 22:46:23 +02005823 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005824}
5825
Hanno Becker84879e32019-01-31 07:44:03 +00005826#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005827
5828#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005829static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5830 unsigned char *crt_buf,
5831 size_t crt_buf_len )
5832{
5833 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5834
5835 if( peer_crt == NULL )
5836 return( -1 );
5837
5838 if( peer_crt->raw.len != crt_buf_len )
5839 return( -1 );
5840
Hanno Becker46f34d02019-02-08 14:00:04 +00005841 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005842}
Hanno Becker177475a2019-02-05 17:02:46 +00005843#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5844static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5845 unsigned char *crt_buf,
5846 size_t crt_buf_len )
5847{
5848 int ret;
5849 unsigned char const * const peer_cert_digest =
5850 ssl->session->peer_cert_digest;
5851 mbedtls_md_type_t const peer_cert_digest_type =
5852 ssl->session->peer_cert_digest_type;
5853 mbedtls_md_info_t const * const digest_info =
5854 mbedtls_md_info_from_type( peer_cert_digest_type );
5855 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5856 size_t digest_len;
5857
5858 if( peer_cert_digest == NULL || digest_info == NULL )
5859 return( -1 );
5860
5861 digest_len = mbedtls_md_get_size( digest_info );
5862 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5863 return( -1 );
5864
5865 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5866 if( ret != 0 )
5867 return( -1 );
5868
5869 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5870}
5871#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005872#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005873
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005874/*
5875 * Once the certificate message is read, parse it into a cert chain and
5876 * perform basic checks, but leave actual verification to the caller
5877 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005878static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5879 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005880{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005881 int ret;
5882#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5883 int crt_cnt=0;
5884#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005885 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005886 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005891 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5892 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894 }
5895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5897 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5901 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 }
5904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005906
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005910 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005911
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005912 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005916 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5917 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 }
5920
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005921 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5922 i += 3;
5923
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005924 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005925 while( i < ssl->in_hslen )
5926 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005927 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005928 if ( i + 3 > ssl->in_hslen ) {
5929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005930 mbedtls_ssl_send_alert_message( ssl,
5931 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5932 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005933 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5934 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005935 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5936 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 if( ssl->in_msg[i] != 0 )
5938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005940 mbedtls_ssl_send_alert_message( ssl,
5941 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5942 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 }
5945
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005946 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005947 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5948 | (unsigned int) ssl->in_msg[i + 2];
5949 i += 3;
5950
5951 if( n < 128 || i + n > ssl->in_hslen )
5952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005954 mbedtls_ssl_send_alert_message( ssl,
5955 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5956 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005958 }
5959
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005960 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005961#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5962 if( crt_cnt++ == 0 &&
5963 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5964 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005965 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005966 /* During client-side renegotiation, check that the server's
5967 * end-CRTs hasn't changed compared to the initial handshake,
5968 * mitigating the triple handshake attack. On success, reuse
5969 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5971 if( ssl_check_peer_crt_unchanged( ssl,
5972 &ssl->in_msg[i],
5973 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005974 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5976 mbedtls_ssl_send_alert_message( ssl,
5977 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5978 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5979 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005980 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005981
5982 /* Now we can safely free the original chain. */
5983 ssl_clear_peer_cert( ssl->session );
5984 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005985#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5986
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005987 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00005988#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005989 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00005990#else
Hanno Becker353a6f02019-02-26 11:51:34 +00005991 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00005992 * it in-place from the input buffer instead of making a copy. */
5993 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5994#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005995 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005997 case 0: /*ok*/
5998 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5999 /* Ignore certificate with an unknown algorithm: maybe a
6000 prior certificate was already trusted. */
6001 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006002
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006003 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6004 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6005 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006006
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006007 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6008 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6009 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006010
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006011 default:
6012 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6013 crt_parse_der_failed:
6014 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6015 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6016 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 }
6018
6019 i += n;
6020 }
6021
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006022 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006023 return( 0 );
6024}
6025
Hanno Becker4a55f632019-02-05 12:49:06 +00006026#if defined(MBEDTLS_SSL_SRV_C)
6027static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6028{
6029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6030 return( -1 );
6031
6032#if defined(MBEDTLS_SSL_PROTO_SSL3)
6033 /*
6034 * Check if the client sent an empty certificate
6035 */
6036 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6037 {
6038 if( ssl->in_msglen == 2 &&
6039 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6040 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6041 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6042 {
6043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6044 return( 0 );
6045 }
6046
6047 return( -1 );
6048 }
6049#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6050
6051#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6052 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6053 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6054 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6055 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6056 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6057 {
6058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6059 return( 0 );
6060 }
6061
6062 return( -1 );
6063#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6064 MBEDTLS_SSL_PROTO_TLS1_2 */
6065}
6066#endif /* MBEDTLS_SSL_SRV_C */
6067
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006068/* Check if a certificate message is expected.
6069 * Return either
6070 * - SSL_CERTIFICATE_EXPECTED, or
6071 * - SSL_CERTIFICATE_SKIP
6072 * indicating whether a Certificate message is expected or not.
6073 */
6074#define SSL_CERTIFICATE_EXPECTED 0
6075#define SSL_CERTIFICATE_SKIP 1
6076static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6077 int authmode )
6078{
6079 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006080 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006081
6082 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6083 return( SSL_CERTIFICATE_SKIP );
6084
6085#if defined(MBEDTLS_SSL_SRV_C)
6086 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6087 {
6088 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6089 return( SSL_CERTIFICATE_SKIP );
6090
6091 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6092 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006093 ssl->session_negotiate->verify_result =
6094 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6095 return( SSL_CERTIFICATE_SKIP );
6096 }
6097 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006098#else
6099 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006100#endif /* MBEDTLS_SSL_SRV_C */
6101
6102 return( SSL_CERTIFICATE_EXPECTED );
6103}
6104
Hanno Becker68636192019-02-05 14:36:34 +00006105static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6106 int authmode,
6107 mbedtls_x509_crt *chain,
6108 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006109{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006110 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006111 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006112 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006113 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006114
Hanno Becker8927c832019-04-03 12:52:50 +01006115 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6116 void *p_vrfy;
6117
Hanno Becker68636192019-02-05 14:36:34 +00006118 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6119 return( 0 );
6120
Hanno Becker8927c832019-04-03 12:52:50 +01006121 if( ssl->f_vrfy != NULL )
6122 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006124 f_vrfy = ssl->f_vrfy;
6125 p_vrfy = ssl->p_vrfy;
6126 }
6127 else
6128 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006130 f_vrfy = ssl->conf->f_vrfy;
6131 p_vrfy = ssl->conf->p_vrfy;
6132 }
6133
Hanno Becker68636192019-02-05 14:36:34 +00006134 /*
6135 * Main check: verify certificate
6136 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006137#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6138 if( ssl->conf->f_ca_cb != NULL )
6139 {
6140 ((void) rs_ctx);
6141 have_ca_chain = 1;
6142
6143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006144 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006145 chain,
6146 ssl->conf->f_ca_cb,
6147 ssl->conf->p_ca_cb,
6148 ssl->conf->cert_profile,
6149 ssl->hostname,
6150 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006151 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006152 }
6153 else
6154#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6155 {
6156 mbedtls_x509_crt *ca_chain;
6157 mbedtls_x509_crl *ca_crl;
6158
6159#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6160 if( ssl->handshake->sni_ca_chain != NULL )
6161 {
6162 ca_chain = ssl->handshake->sni_ca_chain;
6163 ca_crl = ssl->handshake->sni_ca_crl;
6164 }
6165 else
6166#endif
6167 {
6168 ca_chain = ssl->conf->ca_chain;
6169 ca_crl = ssl->conf->ca_crl;
6170 }
6171
6172 if( ca_chain != NULL )
6173 have_ca_chain = 1;
6174
6175 ret = mbedtls_x509_crt_verify_restartable(
6176 chain,
6177 ca_chain, ca_crl,
6178 ssl->conf->cert_profile,
6179 ssl->hostname,
6180 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006181 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006182 }
Hanno Becker68636192019-02-05 14:36:34 +00006183
6184 if( ret != 0 )
6185 {
6186 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6187 }
6188
6189#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6190 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6191 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6192#endif
6193
6194 /*
6195 * Secondary checks: always done, but change 'ret' only if it was 0
6196 */
6197
6198#if defined(MBEDTLS_ECP_C)
6199 {
6200 const mbedtls_pk_context *pk = &chain->pk;
6201
6202 /* If certificate uses an EC key, make sure the curve is OK */
6203 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6204 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6205 {
6206 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6207
6208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6209 if( ret == 0 )
6210 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6211 }
6212 }
6213#endif /* MBEDTLS_ECP_C */
6214
6215 if( mbedtls_ssl_check_cert_usage( chain,
6216 ciphersuite_info,
6217 ! ssl->conf->endpoint,
6218 &ssl->session_negotiate->verify_result ) != 0 )
6219 {
6220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6221 if( ret == 0 )
6222 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6223 }
6224
6225 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6226 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6227 * with details encoded in the verification flags. All other kinds
6228 * of error codes, including those from the user provided f_vrfy
6229 * functions, are treated as fatal and lead to a failure of
6230 * ssl_parse_certificate even if verification was optional. */
6231 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6232 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6233 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6234 {
6235 ret = 0;
6236 }
6237
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006238 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006239 {
6240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6241 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6242 }
6243
6244 if( ret != 0 )
6245 {
6246 uint8_t alert;
6247
6248 /* The certificate may have been rejected for several reasons.
6249 Pick one and send the corresponding alert. Which alert to send
6250 may be a subject of debate in some cases. */
6251 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6252 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6254 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6256 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6260 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6262 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6264 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6265 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6266 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6267 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6268 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6269 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6270 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6271 else
6272 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6273 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6274 alert );
6275 }
6276
6277#if defined(MBEDTLS_DEBUG_C)
6278 if( ssl->session_negotiate->verify_result != 0 )
6279 {
6280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6281 ssl->session_negotiate->verify_result ) );
6282 }
6283 else
6284 {
6285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6286 }
6287#endif /* MBEDTLS_DEBUG_C */
6288
6289 return( ret );
6290}
6291
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006292#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6293static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6294 unsigned char *start, size_t len )
6295{
6296 int ret;
6297 /* Remember digest of the peer's end-CRT. */
6298 ssl->session_negotiate->peer_cert_digest =
6299 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6300 if( ssl->session_negotiate->peer_cert_digest == NULL )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6303 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6304 mbedtls_ssl_send_alert_message( ssl,
6305 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6306 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6307
6308 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6309 }
6310
6311 ret = mbedtls_md( mbedtls_md_info_from_type(
6312 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6313 start, len,
6314 ssl->session_negotiate->peer_cert_digest );
6315
6316 ssl->session_negotiate->peer_cert_digest_type =
6317 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6318 ssl->session_negotiate->peer_cert_digest_len =
6319 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6320
6321 return( ret );
6322}
6323
6324static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6325 unsigned char *start, size_t len )
6326{
6327 unsigned char *end = start + len;
6328 int ret;
6329
6330 /* Make a copy of the peer's raw public key. */
6331 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6332 ret = mbedtls_pk_parse_subpubkey( &start, end,
6333 &ssl->handshake->peer_pubkey );
6334 if( ret != 0 )
6335 {
6336 /* We should have parsed the public key before. */
6337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6338 }
6339
6340 return( 0 );
6341}
6342#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6343
Hanno Becker68636192019-02-05 14:36:34 +00006344int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6345{
6346 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006347 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006348#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6349 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6350 ? ssl->handshake->sni_authmode
6351 : ssl->conf->authmode;
6352#else
6353 const int authmode = ssl->conf->authmode;
6354#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006355 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006356 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006357
6358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6359
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006360 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6361 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006362 {
6363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006364 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006365 }
6366
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006367#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6368 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006369 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006370 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006371 chain = ssl->handshake->ecrs_peer_cert;
6372 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006373 goto crt_verify;
6374 }
6375#endif
6376
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006378 {
6379 /* mbedtls_ssl_read_record may have sent an alert already. We
6380 let it decide whether to alert. */
6381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006382 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006383 }
6384
Hanno Becker4a55f632019-02-05 12:49:06 +00006385#if defined(MBEDTLS_SSL_SRV_C)
6386 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6387 {
6388 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006389
6390 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006391 ret = 0;
6392 else
6393 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006394
Hanno Becker6bdfab22019-02-05 13:11:17 +00006395 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006396 }
6397#endif /* MBEDTLS_SSL_SRV_C */
6398
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006399 /* Clear existing peer CRT structure in case we tried to
6400 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006401 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006402
6403 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6404 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006405 {
6406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6407 sizeof( mbedtls_x509_crt ) ) );
6408 mbedtls_ssl_send_alert_message( ssl,
6409 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6410 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006411
Hanno Becker3dad3112019-02-05 17:19:52 +00006412 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6413 goto exit;
6414 }
6415 mbedtls_x509_crt_init( chain );
6416
6417 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006418 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006419 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006420
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006421#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6422 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006423 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006424
6425crt_verify:
6426 if( ssl->handshake->ecrs_enabled)
6427 rs_ctx = &ssl->handshake->ecrs_ctx;
6428#endif
6429
Hanno Becker68636192019-02-05 14:36:34 +00006430 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006431 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006432 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006433 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006434
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006435#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006436 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006437 unsigned char *crt_start, *pk_start;
6438 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006439
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006440 /* We parse the CRT chain without copying, so
6441 * these pointers point into the input buffer,
6442 * and are hence still valid after freeing the
6443 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006444
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006445 crt_start = chain->raw.p;
6446 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006447
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006448 pk_start = chain->pk_raw.p;
6449 pk_len = chain->pk_raw.len;
6450
6451 /* Free the CRT structures before computing
6452 * digest and copying the peer's public key. */
6453 mbedtls_x509_crt_free( chain );
6454 mbedtls_free( chain );
6455 chain = NULL;
6456
6457 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006458 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006459 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006460
6461 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6462 if( ret != 0 )
6463 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006464 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006465#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6466 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006467 ssl->session_negotiate->peer_cert = chain;
6468 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006469#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006472
Hanno Becker6bdfab22019-02-05 13:11:17 +00006473exit:
6474
Hanno Becker3dad3112019-02-05 17:19:52 +00006475 if( ret == 0 )
6476 ssl->state++;
6477
6478#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6479 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6480 {
6481 ssl->handshake->ecrs_peer_cert = chain;
6482 chain = NULL;
6483 }
6484#endif
6485
6486 if( chain != NULL )
6487 {
6488 mbedtls_x509_crt_free( chain );
6489 mbedtls_free( chain );
6490 }
6491
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 return( ret );
6493}
Hanno Becker21489932019-02-05 13:20:55 +00006494#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006497{
6498 int ret;
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 ssl->out_msglen = 1;
6504 ssl->out_msg[0] = 1;
6505
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 ssl->state++;
6507
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006508 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 return( ret );
6512 }
6513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
6516 return( 0 );
6517}
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520{
6521 int ret;
6522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006524
Hanno Becker327c93b2018-08-15 13:56:18 +01006525 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528 return( ret );
6529 }
6530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006534 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6535 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 }
6538
Hanno Beckere678eaa2018-08-21 14:57:46 +01006539 /* CCS records are only accepted if they have length 1 and content '1',
6540 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006541
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006542 /*
6543 * Switch to our negotiated transform and session parameters for inbound
6544 * data.
6545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006547 ssl->transform_in = ssl->transform_negotiate;
6548 ssl->session_in = ssl->session_negotiate;
6549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006551 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006554 ssl_dtls_replay_reset( ssl );
6555#endif
6556
6557 /* Increment epoch */
6558 if( ++ssl->in_epoch == 0 )
6559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006561 /* This is highly unlikely to happen for legitimate reasons, so
6562 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006564 }
6565 }
6566 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006568 memset( ssl->in_ctr, 0, 8 );
6569
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006570 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6573 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6579 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006581 }
6582 }
6583#endif
6584
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 ssl->state++;
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
6589 return( 0 );
6590}
6591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6593 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006594{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006595 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6598 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6599 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006600 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006601 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6604#if defined(MBEDTLS_SHA512_C)
6605 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006606 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6607 else
6608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609#if defined(MBEDTLS_SHA256_C)
6610 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006611 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006612 else
6613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006617 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006618 }
Paul Bakker380da532012-04-18 16:10:25 +00006619}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6624 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006625 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6626 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6629#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006630#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006631 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006632 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6633#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006634 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006635#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006638#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006639 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006640 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006641#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006642 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006643#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006646}
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006649 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6652 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006653 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6654 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006655#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6657#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006658#if defined(MBEDTLS_USE_PSA_CRYPTO)
6659 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6660#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006661 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006662#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006665#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006666 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006667#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006668 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006669#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006672}
6673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6676static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006677 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006678{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006679 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6680 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006681}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006682#endif
Paul Bakker380da532012-04-18 16:10:25 +00006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6685#if defined(MBEDTLS_SHA256_C)
6686static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006687 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006688{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006689#if defined(MBEDTLS_USE_PSA_CRYPTO)
6690 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6691#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006692 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006693#endif
Paul Bakker380da532012-04-18 16:10:25 +00006694}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006695#endif
Paul Bakker380da532012-04-18 16:10:25 +00006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#if defined(MBEDTLS_SHA512_C)
6698static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006699 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006700{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006701#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006702 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006703#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006704 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006705#endif
Paul Bakker380da532012-04-18 16:10:25 +00006706}
Paul Bakker769075d2012-11-24 11:26:46 +01006707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006711static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006713{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006714 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006715 mbedtls_md5_context md5;
6716 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006717
Paul Bakker5121ce52009-01-03 21:22:43 +00006718 unsigned char padbuf[48];
6719 unsigned char md5sum[16];
6720 unsigned char sha1sum[20];
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006723 if( !session )
6724 session = ssl->session;
6725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006727
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006728 mbedtls_md5_init( &md5 );
6729 mbedtls_sha1_init( &sha1 );
6730
6731 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6732 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006733
6734 /*
6735 * SSLv3:
6736 * hash =
6737 * MD5( master + pad2 +
6738 * MD5( handshake + sender + master + pad1 ) )
6739 * + SHA1( master + pad2 +
6740 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006741 */
6742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006744 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6745 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006749 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6750 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006754 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006755
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006758 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6759 mbedtls_md5_update_ret( &md5, session->master, 48 );
6760 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6761 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006762
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006763 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6764 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6765 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6766 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006767
Paul Bakker1ef83d62012-04-11 12:09:53 +00006768 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006769
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006770 mbedtls_md5_starts_ret( &md5 );
6771 mbedtls_md5_update_ret( &md5, session->master, 48 );
6772 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6773 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6774 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006776 mbedtls_sha1_starts_ret( &sha1 );
6777 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6778 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6779 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6780 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006784 mbedtls_md5_free( &md5 );
6785 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006787 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6788 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6789 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006796static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006800 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006801 mbedtls_md5_context md5;
6802 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006803 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006806 if( !session )
6807 session = ssl->session;
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006811 mbedtls_md5_init( &md5 );
6812 mbedtls_sha1_init( &sha1 );
6813
6814 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6815 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Paul Bakker1ef83d62012-04-11 12:09:53 +00006817 /*
6818 * TLSv1:
6819 * hash = PRF( master, finished_label,
6820 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6821 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006824 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6825 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006826#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006829 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6830 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006831#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006834 ? "client finished"
6835 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006836
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006837 mbedtls_md5_finish_ret( &md5, padbuf );
6838 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006840 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006841 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006844
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006845 mbedtls_md5_free( &md5 );
6846 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006847
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006848 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6855#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006856static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006858{
6859 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006860 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006861 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006862#if defined(MBEDTLS_USE_PSA_CRYPTO)
6863 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006864 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006865 psa_status_t status;
6866#else
6867 mbedtls_sha256_context sha256;
6868#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006871 if( !session )
6872 session = ssl->session;
6873
Andrzej Kurekeb342242019-01-29 09:14:33 -05006874 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6875 ? "client finished"
6876 : "server finished";
6877
6878#if defined(MBEDTLS_USE_PSA_CRYPTO)
6879 sha256_psa = psa_hash_operation_init();
6880
6881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6882
6883 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6884 if( status != PSA_SUCCESS )
6885 {
6886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6887 return;
6888 }
6889
6890 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6891 if( status != PSA_SUCCESS )
6892 {
6893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6894 return;
6895 }
6896 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6897#else
6898
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006899 mbedtls_sha256_init( &sha256 );
6900
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006902
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006903 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
6905 /*
6906 * TLSv1.2:
6907 * hash = PRF( master, finished_label,
6908 * Hash( handshake ) )[0.11]
6909 */
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if !defined(MBEDTLS_SHA256_ALT)
6912 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006914#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006915
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006916 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006917 mbedtls_sha256_free( &sha256 );
6918#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006920 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006921 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006924
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006925 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006932static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006934{
6935 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006936 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006937 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006938#if defined(MBEDTLS_USE_PSA_CRYPTO)
6939 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006940 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006941 psa_status_t status;
6942#else
6943 mbedtls_sha512_context sha512;
6944#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006947 if( !session )
6948 session = ssl->session;
6949
Andrzej Kurekeb342242019-01-29 09:14:33 -05006950 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6951 ? "client finished"
6952 : "server finished";
6953
6954#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006955 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006956
6957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6958
Andrzej Kurek972fba52019-01-30 03:29:12 -05006959 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006960 if( status != PSA_SUCCESS )
6961 {
6962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6963 return;
6964 }
6965
Andrzej Kurek972fba52019-01-30 03:29:12 -05006966 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006967 if( status != PSA_SUCCESS )
6968 {
6969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6970 return;
6971 }
6972 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6973#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006974 mbedtls_sha512_init( &sha512 );
6975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006978 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006979
6980 /*
6981 * TLSv1.2:
6982 * hash = PRF( master, finished_label,
6983 * Hash( handshake ) )[0.11]
6984 */
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006987 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6988 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006989#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006991 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006992 mbedtls_sha512_free( &sha512 );
6993#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006994
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006995 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006996 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006999
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007000 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#endif /* MBEDTLS_SHA512_C */
7005#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007010
7011 /*
7012 * Free our handshake params
7013 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007014 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007016 ssl->handshake = NULL;
7017
7018 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007019 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007020 */
7021 if( ssl->transform )
7022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 mbedtls_ssl_transform_free( ssl->transform );
7024 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007025 }
7026 ssl->transform = ssl->transform_negotiate;
7027 ssl->transform_negotiate = NULL;
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007030}
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007033{
7034 int resume = ssl->handshake->resume;
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if defined(MBEDTLS_SSL_RENEGOTIATION)
7039 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007042 ssl->renego_records_seen = 0;
7043 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007044#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007045
7046 /*
7047 * Free the previous session and switch in the current one
7048 */
Paul Bakker0a597072012-09-25 21:55:46 +00007049 if( ssl->session )
7050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007052 /* RFC 7366 3.1: keep the EtM state */
7053 ssl->session_negotiate->encrypt_then_mac =
7054 ssl->session->encrypt_then_mac;
7055#endif
7056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 mbedtls_ssl_session_free( ssl->session );
7058 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007059 }
7060 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007061 ssl->session_negotiate = NULL;
7062
Paul Bakker0a597072012-09-25 21:55:46 +00007063 /*
7064 * Add cache entry
7065 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007066 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007067 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007068 resume == 0 )
7069 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007070 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007072 }
Paul Bakker0a597072012-09-25 21:55:46 +00007073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007075 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007076 ssl->handshake->flight != NULL )
7077 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007078 /* Cancel handshake timer */
7079 ssl_set_timer( ssl, 0 );
7080
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007081 /* Keep last flight around in case we need to resend it:
7082 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007084 }
7085 else
7086#endif
7087 ssl_handshake_wrapup_free_hs_transform( ssl );
7088
Paul Bakker48916f92012-09-16 19:57:18 +00007089 ssl->state++;
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007092}
7093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007095{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007096 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007099
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007100 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007101
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007102 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007104 /*
7105 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7106 * may define some other value. Currently (early 2016), no defined
7107 * ciphersuite does this (and this is unlikely to change as activity has
7108 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007113 ssl->verify_data_len = hash_len;
7114 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007115#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007116
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7119 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007120
7121 /*
7122 * In case of session resuming, invert the client and server
7123 * ChangeCipherSpec messages order.
7124 */
Paul Bakker0a597072012-09-25 21:55:46 +00007125 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007128 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007132 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 }
7136 else
7137 ssl->state++;
7138
Paul Bakker48916f92012-09-16 19:57:18 +00007139 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007140 * Switch to our negotiated transform and session parameters for outbound
7141 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007147 {
7148 unsigned char i;
7149
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007150 /* Remember current epoch settings for resending */
7151 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007152 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007153
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007154 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007155 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007156
7157 /* Increment epoch */
7158 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007159 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007160 break;
7161
7162 /* The loop goes to its end iff the counter is wrapping */
7163 if( i == 0 )
7164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7166 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007167 }
7168 }
7169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007171 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007172
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007173 ssl->transform_out = ssl->transform_negotiate;
7174 ssl->session_out = ssl->session_negotiate;
7175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7177 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7182 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007183 }
7184 }
7185#endif
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007190#endif
7191
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007192 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007193 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007195 return( ret );
7196 }
7197
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007198#if defined(MBEDTLS_SSL_PROTO_DTLS)
7199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7200 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7201 {
7202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7203 return( ret );
7204 }
7205#endif
7206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007208
7209 return( 0 );
7210}
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007213#define SSL_MAX_HASH_LEN 36
7214#else
7215#define SSL_MAX_HASH_LEN 12
7216#endif
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007219{
Paul Bakker23986e52011-04-24 08:57:21 +00007220 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007221 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007222 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007226 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007227
Hanno Becker327c93b2018-08-15 13:56:18 +01007228 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007231 return( ret );
7232 }
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7238 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007240 }
7241
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007242 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_PROTO_SSL3)
7244 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007245 hash_len = 36;
7246 else
7247#endif
7248 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7251 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257 }
7258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007260 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007266 }
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007269 ssl->verify_data_len = hash_len;
7270 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007271#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007272
Paul Bakker0a597072012-09-25 21:55:46 +00007273 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 }
7284 else
7285 ssl->state++;
7286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007290#endif
7291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007293
7294 return( 0 );
7295}
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7302 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7303 mbedtls_md5_init( &handshake->fin_md5 );
7304 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007305 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7306 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7309#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007310#if defined(MBEDTLS_USE_PSA_CRYPTO)
7311 handshake->fin_sha256_psa = psa_hash_operation_init();
7312 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7313#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007315 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007316#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007319#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007320 handshake->fin_sha384_psa = psa_hash_operation_init();
7321 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007322#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007324 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007325#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007328
7329 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007330
7331#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7332 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7333 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7334#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_DHM_C)
7337 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if defined(MBEDTLS_ECDH_C)
7340 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007341#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007342#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007343 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007344#if defined(MBEDTLS_SSL_CLI_C)
7345 handshake->ecjpake_cache = NULL;
7346 handshake->ecjpake_cache_len = 0;
7347#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007348#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007349
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007350#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007351 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007352#endif
7353
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007354#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7355 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7356#endif
Hanno Becker75173122019-02-06 16:18:31 +00007357
7358#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7359 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7360 mbedtls_pk_init( &handshake->peer_pubkey );
7361#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007362}
7363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7369 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 mbedtls_md_init( &transform->md_ctx_enc );
7372 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007373}
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007378}
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007381{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007383 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007385 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007387 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007388 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007389
7390 /*
7391 * Either the pointers are now NULL or cleared properly and can be freed.
7392 * Now allocate missing structures.
7393 */
7394 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007396 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007397 }
Paul Bakker48916f92012-09-16 19:57:18 +00007398
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007399 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007401 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007402 }
Paul Bakker48916f92012-09-16 19:57:18 +00007403
Paul Bakker82788fb2014-10-20 13:59:19 +02007404 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007405 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007406 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007407 }
Paul Bakker48916f92012-09-16 19:57:18 +00007408
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007409 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007410 if( ssl->handshake == NULL ||
7411 ssl->transform_negotiate == NULL ||
7412 ssl->session_negotiate == NULL )
7413 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 mbedtls_free( ssl->handshake );
7417 mbedtls_free( ssl->transform_negotiate );
7418 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007419
7420 ssl->handshake = NULL;
7421 ssl->transform_negotiate = NULL;
7422 ssl->session_negotiate = NULL;
7423
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007424 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007425 }
7426
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007427 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007429 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007430 ssl_handshake_params_init( ssl->handshake );
7431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7434 {
7435 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007436
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007437 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7438 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7439 else
7440 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007441
7442 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007443 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007444#endif
7445
Paul Bakker48916f92012-09-16 19:57:18 +00007446 return( 0 );
7447}
7448
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007449#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007450/* Dummy cookie callbacks for defaults */
7451static int ssl_cookie_write_dummy( void *ctx,
7452 unsigned char **p, unsigned char *end,
7453 const unsigned char *cli_id, size_t cli_id_len )
7454{
7455 ((void) ctx);
7456 ((void) p);
7457 ((void) end);
7458 ((void) cli_id);
7459 ((void) cli_id_len);
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007462}
7463
7464static int ssl_cookie_check_dummy( void *ctx,
7465 const unsigned char *cookie, size_t cookie_len,
7466 const unsigned char *cli_id, size_t cli_id_len )
7467{
7468 ((void) ctx);
7469 ((void) cookie);
7470 ((void) cookie_len);
7471 ((void) cli_id);
7472 ((void) cli_id_len);
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007475}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007476#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007477
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007478/* Once ssl->out_hdr as the address of the beginning of the
7479 * next outgoing record is set, deduce the other pointers.
7480 *
7481 * Note: For TLS, we save the implicit record sequence number
7482 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7483 * and the caller has to make sure there's space for this.
7484 */
7485
7486static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7487 mbedtls_ssl_transform *transform )
7488{
7489#if defined(MBEDTLS_SSL_PROTO_DTLS)
7490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7491 {
7492 ssl->out_ctr = ssl->out_hdr + 3;
7493 ssl->out_len = ssl->out_hdr + 11;
7494 ssl->out_iv = ssl->out_hdr + 13;
7495 }
7496 else
7497#endif
7498 {
7499 ssl->out_ctr = ssl->out_hdr - 8;
7500 ssl->out_len = ssl->out_hdr + 3;
7501 ssl->out_iv = ssl->out_hdr + 5;
7502 }
7503
7504 /* Adjust out_msg to make space for explicit IV, if used. */
7505 if( transform != NULL &&
7506 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7507 {
7508 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7509 }
7510 else
7511 ssl->out_msg = ssl->out_iv;
7512}
7513
7514/* Once ssl->in_hdr as the address of the beginning of the
7515 * next incoming record is set, deduce the other pointers.
7516 *
7517 * Note: For TLS, we save the implicit record sequence number
7518 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7519 * and the caller has to make sure there's space for this.
7520 */
7521
7522static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7523 mbedtls_ssl_transform *transform )
7524{
7525#if defined(MBEDTLS_SSL_PROTO_DTLS)
7526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7527 {
7528 ssl->in_ctr = ssl->in_hdr + 3;
7529 ssl->in_len = ssl->in_hdr + 11;
7530 ssl->in_iv = ssl->in_hdr + 13;
7531 }
7532 else
7533#endif
7534 {
7535 ssl->in_ctr = ssl->in_hdr - 8;
7536 ssl->in_len = ssl->in_hdr + 3;
7537 ssl->in_iv = ssl->in_hdr + 5;
7538 }
7539
7540 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7541 if( transform != NULL &&
7542 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7543 {
7544 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7545 }
7546 else
7547 ssl->in_msg = ssl->in_iv;
7548}
7549
Paul Bakker5121ce52009-01-03 21:22:43 +00007550/*
7551 * Initialize an SSL context
7552 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007553void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7554{
7555 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7556}
7557
7558/*
7559 * Setup an SSL context
7560 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007561
7562static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7563{
7564 /* Set the incoming and outgoing record pointers. */
7565#if defined(MBEDTLS_SSL_PROTO_DTLS)
7566 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7567 {
7568 ssl->out_hdr = ssl->out_buf;
7569 ssl->in_hdr = ssl->in_buf;
7570 }
7571 else
7572#endif /* MBEDTLS_SSL_PROTO_DTLS */
7573 {
7574 ssl->out_hdr = ssl->out_buf + 8;
7575 ssl->in_hdr = ssl->in_buf + 8;
7576 }
7577
7578 /* Derive other internal pointers. */
7579 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7580 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7581}
7582
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007583int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007584 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007585{
Paul Bakker48916f92012-09-16 19:57:18 +00007586 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007588 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007589
7590 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007591 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007592 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007593
7594 /* Set to NULL in case of an error condition */
7595 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007596
Angus Grattond8213d02016-05-25 20:56:48 +10007597 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7598 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007599 {
Angus Grattond8213d02016-05-25 20:56:48 +10007600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007601 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007602 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007603 }
7604
7605 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7606 if( ssl->out_buf == NULL )
7607 {
7608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007609 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007610 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007611 }
7612
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007613 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007614
Paul Bakker48916f92012-09-16 19:57:18 +00007615 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007616 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007617
7618 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007619
7620error:
7621 mbedtls_free( ssl->in_buf );
7622 mbedtls_free( ssl->out_buf );
7623
7624 ssl->conf = NULL;
7625
7626 ssl->in_buf = NULL;
7627 ssl->out_buf = NULL;
7628
7629 ssl->in_hdr = NULL;
7630 ssl->in_ctr = NULL;
7631 ssl->in_len = NULL;
7632 ssl->in_iv = NULL;
7633 ssl->in_msg = NULL;
7634
7635 ssl->out_hdr = NULL;
7636 ssl->out_ctr = NULL;
7637 ssl->out_len = NULL;
7638 ssl->out_iv = NULL;
7639 ssl->out_msg = NULL;
7640
k-stachowiak9f7798e2018-07-31 16:52:32 +02007641 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007642}
7643
7644/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007645 * Reset an initialized and used SSL context for re-use while retaining
7646 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007647 *
7648 * If partial is non-zero, keep data in the input buffer and client ID.
7649 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007650 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007651static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007652{
Paul Bakker48916f92012-09-16 19:57:18 +00007653 int ret;
7654
Hanno Becker7e772132018-08-10 12:38:21 +01007655#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7656 !defined(MBEDTLS_SSL_SRV_C)
7657 ((void) partial);
7658#endif
7659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007661
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007662 /* Cancel any possibly running timer */
7663 ssl_set_timer( ssl, 0 );
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if defined(MBEDTLS_SSL_RENEGOTIATION)
7666 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007667 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007668
7669 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7671 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007674
Paul Bakker7eb013f2011-10-06 12:37:39 +00007675 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007676 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007677
7678 ssl->in_msgtype = 0;
7679 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007681 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007682 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007685 ssl_dtls_replay_reset( ssl );
7686#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007687
7688 ssl->in_hslen = 0;
7689 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007690
7691 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007692
7693 ssl->out_msgtype = 0;
7694 ssl->out_msglen = 0;
7695 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7697 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007698 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007699#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007700
Hanno Becker19859472018-08-06 09:40:20 +01007701 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7702
Paul Bakker48916f92012-09-16 19:57:18 +00007703 ssl->transform_in = NULL;
7704 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007705
Hanno Becker78640902018-08-13 16:35:15 +01007706 ssl->session_in = NULL;
7707 ssl->session_out = NULL;
7708
Angus Grattond8213d02016-05-25 20:56:48 +10007709 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007710
7711#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007712 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007713#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7714 {
7715 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007716 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007717 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7720 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7723 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7726 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007727 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007728 }
7729#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007730
Paul Bakker48916f92012-09-16 19:57:18 +00007731 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733 mbedtls_ssl_transform_free( ssl->transform );
7734 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007735 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007736 }
Paul Bakker48916f92012-09-16 19:57:18 +00007737
Paul Bakkerc0463502013-02-14 11:19:38 +01007738 if( ssl->session )
7739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 mbedtls_ssl_session_free( ssl->session );
7741 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007742 ssl->session = NULL;
7743 }
7744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007746 ssl->alpn_chosen = NULL;
7747#endif
7748
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007749#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007750#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007751 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007752#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007753 {
7754 mbedtls_free( ssl->cli_id );
7755 ssl->cli_id = NULL;
7756 ssl->cli_id_len = 0;
7757 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007758#endif
7759
Paul Bakker48916f92012-09-16 19:57:18 +00007760 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7761 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007762
7763 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007764}
7765
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007766/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007767 * Reset an initialized and used SSL context for re-use while retaining
7768 * all application-set variables, function pointers and data.
7769 */
7770int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7771{
7772 return( ssl_session_reset_int( ssl, 0 ) );
7773}
7774
7775/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007776 * SSL set accessors
7777 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007778void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007780 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007781}
7782
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007783void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007784{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007785 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007786}
7787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007789void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007791 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007792}
7793#endif
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007796void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007798 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007799}
7800#endif
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007803
Hanno Becker1841b0a2018-08-24 11:13:57 +01007804void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7805 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007806{
7807 ssl->disable_datagram_packing = !allow_packing;
7808}
7809
7810void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7811 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007812{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007813 conf->hs_timeout_min = min;
7814 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007815}
7816#endif
7817
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007821}
7822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007824void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007825 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007826 void *p_vrfy )
7827{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007828 conf->f_vrfy = f_vrfy;
7829 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007832
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007833void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007834 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 void *p_rng )
7836{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007837 conf->f_rng = f_rng;
7838 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007839}
7840
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007841void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007842 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 void *p_dbg )
7844{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007845 conf->f_dbg = f_dbg;
7846 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007847}
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007850 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007851 mbedtls_ssl_send_t *f_send,
7852 mbedtls_ssl_recv_t *f_recv,
7853 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007854{
7855 ssl->p_bio = p_bio;
7856 ssl->f_send = f_send;
7857 ssl->f_recv = f_recv;
7858 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007859}
7860
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007861#if defined(MBEDTLS_SSL_PROTO_DTLS)
7862void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7863{
7864 ssl->mtu = mtu;
7865}
7866#endif
7867
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007868void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007869{
7870 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007871}
7872
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007873void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7874 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007875 mbedtls_ssl_set_timer_t *f_set_timer,
7876 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007877{
7878 ssl->p_timer = p_timer;
7879 ssl->f_set_timer = f_set_timer;
7880 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007881
7882 /* Make sure we start with no timer running */
7883 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007884}
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007887void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007888 void *p_cache,
7889 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7890 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007891{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007892 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007893 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007894 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_CLI_C)
7899int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007900{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007901 int ret;
7902
7903 if( ssl == NULL ||
7904 session == NULL ||
7905 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007906 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007909 }
7910
Hanno Becker52055ae2019-02-06 14:30:46 +00007911 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7912 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007913 return( ret );
7914
Paul Bakker0a597072012-09-25 21:55:46 +00007915 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007916
7917 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007920
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007921void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007924 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7927 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007928}
7929
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007930void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007931 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007932 int major, int minor )
7933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007935 return;
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007938 return;
7939
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007940 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007941}
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007944void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007945 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007946{
7947 conf->cert_profile = profile;
7948}
7949
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007950/* Append a new keycert entry to a (possibly empty) list */
7951static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7952 mbedtls_x509_crt *cert,
7953 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007954{
niisato8ee24222018-06-25 19:05:48 +09007955 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007956
niisato8ee24222018-06-25 19:05:48 +09007957 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7958 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007959 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007960
niisato8ee24222018-06-25 19:05:48 +09007961 new_cert->cert = cert;
7962 new_cert->key = key;
7963 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007964
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007965 /* Update head is the list was null, else add to the end */
7966 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007967 {
niisato8ee24222018-06-25 19:05:48 +09007968 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007969 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007970 else
7971 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007972 mbedtls_ssl_key_cert *cur = *head;
7973 while( cur->next != NULL )
7974 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007975 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007976 }
7977
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007978 return( 0 );
7979}
7980
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007981int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007982 mbedtls_x509_crt *own_cert,
7983 mbedtls_pk_context *pk_key )
7984{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007985 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007986}
7987
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007988void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007989 mbedtls_x509_crt *ca_chain,
7990 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007991{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007992 conf->ca_chain = ca_chain;
7993 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00007994
7995#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7996 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7997 * cannot be used together. */
7998 conf->f_ca_cb = NULL;
7999 conf->p_ca_cb = NULL;
8000#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008001}
Hanno Becker5adaad92019-03-27 16:54:37 +00008002
8003#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8004void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008005 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008006 void *p_ca_cb )
8007{
8008 conf->f_ca_cb = f_ca_cb;
8009 conf->p_ca_cb = p_ca_cb;
8010
8011 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8012 * cannot be used together. */
8013 conf->ca_chain = NULL;
8014 conf->ca_crl = NULL;
8015}
8016#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008018
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008019#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8020int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8021 mbedtls_x509_crt *own_cert,
8022 mbedtls_pk_context *pk_key )
8023{
8024 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8025 own_cert, pk_key ) );
8026}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008027
8028void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8029 mbedtls_x509_crt *ca_chain,
8030 mbedtls_x509_crl *ca_crl )
8031{
8032 ssl->handshake->sni_ca_chain = ca_chain;
8033 ssl->handshake->sni_ca_crl = ca_crl;
8034}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008035
8036void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8037 int authmode )
8038{
8039 ssl->handshake->sni_authmode = authmode;
8040}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008041#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8042
Hanno Becker8927c832019-04-03 12:52:50 +01008043#if defined(MBEDTLS_X509_CRT_PARSE_C)
8044void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8045 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8046 void *p_vrfy )
8047{
8048 ssl->f_vrfy = f_vrfy;
8049 ssl->p_vrfy = p_vrfy;
8050}
8051#endif
8052
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008053#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008054/*
8055 * Set EC J-PAKE password for current handshake
8056 */
8057int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8058 const unsigned char *pw,
8059 size_t pw_len )
8060{
8061 mbedtls_ecjpake_role role;
8062
Janos Follath8eb64132016-06-03 15:40:57 +01008063 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8065
8066 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8067 role = MBEDTLS_ECJPAKE_SERVER;
8068 else
8069 role = MBEDTLS_ECJPAKE_CLIENT;
8070
8071 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8072 role,
8073 MBEDTLS_MD_SHA256,
8074 MBEDTLS_ECP_DP_SECP256R1,
8075 pw, pw_len ) );
8076}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008077#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008080
8081static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8082{
8083 /* Remove reference to existing PSK, if any. */
8084#if defined(MBEDTLS_USE_PSA_CRYPTO)
8085 if( conf->psk_opaque != 0 )
8086 {
8087 /* The maintenance of the PSK key slot is the
8088 * user's responsibility. */
8089 conf->psk_opaque = 0;
8090 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008091 /* This and the following branch should never
8092 * be taken simultaenously as we maintain the
8093 * invariant that raw and opaque PSKs are never
8094 * configured simultaneously. As a safeguard,
8095 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008096#endif /* MBEDTLS_USE_PSA_CRYPTO */
8097 if( conf->psk != NULL )
8098 {
8099 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8100
8101 mbedtls_free( conf->psk );
8102 conf->psk = NULL;
8103 conf->psk_len = 0;
8104 }
8105
8106 /* Remove reference to PSK identity, if any. */
8107 if( conf->psk_identity != NULL )
8108 {
8109 mbedtls_free( conf->psk_identity );
8110 conf->psk_identity = NULL;
8111 conf->psk_identity_len = 0;
8112 }
8113}
8114
Hanno Becker7390c712018-11-15 13:33:04 +00008115/* This function assumes that PSK identity in the SSL config is unset.
8116 * It checks that the provided identity is well-formed and attempts
8117 * to make a copy of it in the SSL config.
8118 * On failure, the PSK identity in the config remains unset. */
8119static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8120 unsigned char const *psk_identity,
8121 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008122{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008123 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008124 if( psk_identity == NULL ||
8125 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008126 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008127 {
8128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8129 }
8130
Hanno Becker7390c712018-11-15 13:33:04 +00008131 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8132 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008133 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008134
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008135 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008136 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008137
8138 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008139}
8140
Hanno Becker7390c712018-11-15 13:33:04 +00008141int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8142 const unsigned char *psk, size_t psk_len,
8143 const unsigned char *psk_identity, size_t psk_identity_len )
8144{
8145 int ret;
8146 /* Remove opaque/raw PSK + PSK Identity */
8147 ssl_conf_remove_psk( conf );
8148
8149 /* Check and set raw PSK */
8150 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8152 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8153 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8154 conf->psk_len = psk_len;
8155 memcpy( conf->psk, psk, conf->psk_len );
8156
8157 /* Check and set PSK Identity */
8158 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8159 if( ret != 0 )
8160 ssl_conf_remove_psk( conf );
8161
8162 return( ret );
8163}
8164
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008165static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8166{
8167#if defined(MBEDTLS_USE_PSA_CRYPTO)
8168 if( ssl->handshake->psk_opaque != 0 )
8169 {
8170 ssl->handshake->psk_opaque = 0;
8171 }
8172 else
8173#endif /* MBEDTLS_USE_PSA_CRYPTO */
8174 if( ssl->handshake->psk != NULL )
8175 {
8176 mbedtls_platform_zeroize( ssl->handshake->psk,
8177 ssl->handshake->psk_len );
8178 mbedtls_free( ssl->handshake->psk );
8179 ssl->handshake->psk_len = 0;
8180 }
8181}
8182
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008183int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8184 const unsigned char *psk, size_t psk_len )
8185{
8186 if( psk == NULL || ssl->handshake == NULL )
8187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8188
8189 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8191
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008192 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008193
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008194 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008195 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008196
8197 ssl->handshake->psk_len = psk_len;
8198 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8199
8200 return( 0 );
8201}
8202
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008203#if defined(MBEDTLS_USE_PSA_CRYPTO)
8204int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008205 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008206 const unsigned char *psk_identity,
8207 size_t psk_identity_len )
8208{
Hanno Becker7390c712018-11-15 13:33:04 +00008209 int ret;
8210 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008211 ssl_conf_remove_psk( conf );
8212
Hanno Becker7390c712018-11-15 13:33:04 +00008213 /* Check and set opaque PSK */
8214 if( psk_slot == 0 )
8215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008216 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008217
8218 /* Check and set PSK Identity */
8219 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8220 psk_identity_len );
8221 if( ret != 0 )
8222 ssl_conf_remove_psk( conf );
8223
8224 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008225}
8226
8227int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008228 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008229{
8230 if( psk_slot == 0 || ssl->handshake == NULL )
8231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8232
8233 ssl_remove_psk( ssl );
8234 ssl->handshake->psk_opaque = psk_slot;
8235 return( 0 );
8236}
8237#endif /* MBEDTLS_USE_PSA_CRYPTO */
8238
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008239void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008241 size_t),
8242 void *p_psk )
8243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008244 conf->f_psk = f_psk;
8245 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008248
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008249#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008250
8251#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008252int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008253{
8254 int ret;
8255
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008256 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8257 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8258 {
8259 mbedtls_mpi_free( &conf->dhm_P );
8260 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008261 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
8264 return( 0 );
8265}
Hanno Becker470a8c42017-10-04 15:28:46 +01008266#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008267
Hanno Beckera90658f2017-10-04 15:29:08 +01008268int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8269 const unsigned char *dhm_P, size_t P_len,
8270 const unsigned char *dhm_G, size_t G_len )
8271{
8272 int ret;
8273
8274 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8275 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8276 {
8277 mbedtls_mpi_free( &conf->dhm_P );
8278 mbedtls_mpi_free( &conf->dhm_G );
8279 return( ret );
8280 }
8281
8282 return( 0 );
8283}
Paul Bakker5121ce52009-01-03 21:22:43 +00008284
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008285int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008286{
8287 int ret;
8288
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008289 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8290 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8291 {
8292 mbedtls_mpi_free( &conf->dhm_P );
8293 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008294 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008295 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008296
8297 return( 0 );
8298}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008299#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008300
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008301#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8302/*
8303 * Set the minimum length for Diffie-Hellman parameters
8304 */
8305void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8306 unsigned int bitlen )
8307{
8308 conf->dhm_min_bitlen = bitlen;
8309}
8310#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8311
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008312#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008313/*
8314 * Set allowed/preferred hashes for handshake signatures
8315 */
8316void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8317 const int *hashes )
8318{
8319 conf->sig_hashes = hashes;
8320}
Hanno Becker947194e2017-04-07 13:25:49 +01008321#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008322
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008323#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008324/*
8325 * Set the allowed elliptic curves
8326 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008327void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008328 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008329{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008330 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008331}
Hanno Becker947194e2017-04-07 13:25:49 +01008332#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008333
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008336{
Hanno Becker947194e2017-04-07 13:25:49 +01008337 /* Initialize to suppress unnecessary compiler warning */
8338 size_t hostname_len = 0;
8339
8340 /* Check if new hostname is valid before
8341 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008342 if( hostname != NULL )
8343 {
8344 hostname_len = strlen( hostname );
8345
8346 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8348 }
8349
8350 /* Now it's clear that we will overwrite the old hostname,
8351 * so we can free it safely */
8352
8353 if( ssl->hostname != NULL )
8354 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008355 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008356 mbedtls_free( ssl->hostname );
8357 }
8358
8359 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008360
Paul Bakker5121ce52009-01-03 21:22:43 +00008361 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008362 {
8363 ssl->hostname = NULL;
8364 }
8365 else
8366 {
8367 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008368 if( ssl->hostname == NULL )
8369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008370
Hanno Becker947194e2017-04-07 13:25:49 +01008371 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008372
Hanno Becker947194e2017-04-07 13:25:49 +01008373 ssl->hostname[hostname_len] = '\0';
8374 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008375
8376 return( 0 );
8377}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008378#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008379
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008380#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008381void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008383 const unsigned char *, size_t),
8384 void *p_sni )
8385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008386 conf->f_sni = f_sni;
8387 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008392int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008393{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008394 size_t cur_len, tot_len;
8395 const char **p;
8396
8397 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008398 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8399 * MUST NOT be truncated."
8400 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008401 */
8402 tot_len = 0;
8403 for( p = protos; *p != NULL; p++ )
8404 {
8405 cur_len = strlen( *p );
8406 tot_len += cur_len;
8407
8408 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008410 }
8411
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008413
8414 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008415}
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008418{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008419 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008422
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008423void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008425 conf->max_major_ver = major;
8426 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008427}
8428
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008429void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008431 conf->min_major_ver = major;
8432 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008433}
8434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008436void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008437{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008438 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008439}
8440#endif
8441
Janos Follath088ce432017-04-10 12:42:31 +01008442#if defined(MBEDTLS_SSL_SRV_C)
8443void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8444 char cert_req_ca_list )
8445{
8446 conf->cert_req_ca_list = cert_req_ca_list;
8447}
8448#endif
8449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008453 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008454}
8455#endif
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008460 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008461}
8462#endif
8463
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008464#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008465void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008467 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008468}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008469#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008472int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008475 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008478 }
8479
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008480 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008481
8482 return( 0 );
8483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008487void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008488{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008489 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008494void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008495{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008496 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008497}
8498#endif
8499
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008500void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008503}
8504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008507{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008508 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008509}
8510
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008511void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008512{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008513 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008514}
8515
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008517 const unsigned char period[8] )
8518{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008519 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008524#if defined(MBEDTLS_SSL_CLI_C)
8525void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008526{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008527 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008528}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008529#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008530
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008532void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8533 mbedtls_ssl_ticket_write_t *f_ticket_write,
8534 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8535 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008536{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008537 conf->f_ticket_write = f_ticket_write;
8538 conf->f_ticket_parse = f_ticket_parse;
8539 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008540}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008543
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008544#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8545void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8546 mbedtls_ssl_export_keys_t *f_export_keys,
8547 void *p_export_keys )
8548{
8549 conf->f_export_keys = f_export_keys;
8550 conf->p_export_keys = p_export_keys;
8551}
8552#endif
8553
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008554#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008555void mbedtls_ssl_conf_async_private_cb(
8556 mbedtls_ssl_config *conf,
8557 mbedtls_ssl_async_sign_t *f_async_sign,
8558 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8559 mbedtls_ssl_async_resume_t *f_async_resume,
8560 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008561 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008562{
8563 conf->f_async_sign_start = f_async_sign;
8564 conf->f_async_decrypt_start = f_async_decrypt;
8565 conf->f_async_resume = f_async_resume;
8566 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008567 conf->p_async_config_data = async_config_data;
8568}
8569
Gilles Peskine8f97af72018-04-26 11:46:10 +02008570void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8571{
8572 return( conf->p_async_config_data );
8573}
8574
Gilles Peskine1febfef2018-04-30 11:54:39 +02008575void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008576{
8577 if( ssl->handshake == NULL )
8578 return( NULL );
8579 else
8580 return( ssl->handshake->user_async_ctx );
8581}
8582
Gilles Peskine1febfef2018-04-30 11:54:39 +02008583void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008584 void *ctx )
8585{
8586 if( ssl->handshake != NULL )
8587 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008588}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008589#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008590
Paul Bakker5121ce52009-01-03 21:22:43 +00008591/*
8592 * SSL get accessors
8593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008595{
8596 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8597}
8598
Hanno Becker8b170a02017-10-10 11:51:19 +01008599int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8600{
8601 /*
8602 * Case A: We're currently holding back
8603 * a message for further processing.
8604 */
8605
8606 if( ssl->keep_current_message == 1 )
8607 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008609 return( 1 );
8610 }
8611
8612 /*
8613 * Case B: Further records are pending in the current datagram.
8614 */
8615
8616#if defined(MBEDTLS_SSL_PROTO_DTLS)
8617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8618 ssl->in_left > ssl->next_record_offset )
8619 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008621 return( 1 );
8622 }
8623#endif /* MBEDTLS_SSL_PROTO_DTLS */
8624
8625 /*
8626 * Case C: A handshake message is being processed.
8627 */
8628
Hanno Becker8b170a02017-10-10 11:51:19 +01008629 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8630 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008632 return( 1 );
8633 }
8634
8635 /*
8636 * Case D: An application data message is being processed
8637 */
8638 if( ssl->in_offt != NULL )
8639 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008641 return( 1 );
8642 }
8643
8644 /*
8645 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008646 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008647 * we implement support for multiple alerts in single records.
8648 */
8649
8650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8651 return( 0 );
8652}
8653
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008654uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008655{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008656 if( ssl->session != NULL )
8657 return( ssl->session->verify_result );
8658
8659 if( ssl->session_negotiate != NULL )
8660 return( ssl->session_negotiate->verify_result );
8661
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008662 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008663}
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008666{
Paul Bakker926c8e42013-03-06 10:23:34 +01008667 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008668 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008671}
8672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008677 {
8678 switch( ssl->minor_ver )
8679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008681 return( "DTLSv1.0" );
8682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008684 return( "DTLSv1.2" );
8685
8686 default:
8687 return( "unknown (DTLS)" );
8688 }
8689 }
8690#endif
8691
Paul Bakker43ca69c2011-01-15 17:35:19 +00008692 switch( ssl->minor_ver )
8693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008695 return( "SSLv3.0" );
8696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008698 return( "TLSv1.0" );
8699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008701 return( "TLSv1.1" );
8702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008704 return( "TLSv1.2" );
8705
Paul Bakker43ca69c2011-01-15 17:35:19 +00008706 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008707 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008708 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008709}
8710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008712{
Hanno Becker3136ede2018-08-17 15:28:19 +01008713 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008715 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008716
Hanno Becker78640902018-08-13 16:35:15 +01008717 if( transform == NULL )
8718 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720#if defined(MBEDTLS_ZLIB_SUPPORT)
8721 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8722 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008723#endif
8724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 case MBEDTLS_MODE_GCM:
8728 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008729 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008731 transform_expansion = transform->minlen;
8732 break;
8733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008735
8736 block_size = mbedtls_cipher_get_block_size(
8737 &transform->cipher_ctx_enc );
8738
Hanno Becker3136ede2018-08-17 15:28:19 +01008739 /* Expansion due to the addition of the MAC. */
8740 transform_expansion += transform->maclen;
8741
8742 /* Expansion due to the addition of CBC padding;
8743 * Theoretically up to 256 bytes, but we never use
8744 * more than the block size of the underlying cipher. */
8745 transform_expansion += block_size;
8746
8747 /* For TLS 1.1 or higher, an explicit IV is added
8748 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008749#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8750 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008751 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008752#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008753
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008754 break;
8755
8756 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008759 }
8760
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008761 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008762}
8763
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008764#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8765size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8766{
8767 size_t max_len;
8768
8769 /*
8770 * Assume mfl_code is correct since it was checked when set
8771 */
Angus Grattond8213d02016-05-25 20:56:48 +10008772 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008773
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008774 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008775 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008776 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008777 {
Angus Grattond8213d02016-05-25 20:56:48 +10008778 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008779 }
8780
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008781 /* During a handshake, use the value being negotiated */
8782 if( ssl->session_negotiate != NULL &&
8783 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8784 {
8785 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8786 }
8787
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008788 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008789}
8790#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8791
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008792#if defined(MBEDTLS_SSL_PROTO_DTLS)
8793static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8794{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008795 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8797 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8798 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8799 return ( 0 );
8800
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008801 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8802 return( ssl->mtu );
8803
8804 if( ssl->mtu == 0 )
8805 return( ssl->handshake->mtu );
8806
8807 return( ssl->mtu < ssl->handshake->mtu ?
8808 ssl->mtu : ssl->handshake->mtu );
8809}
8810#endif /* MBEDTLS_SSL_PROTO_DTLS */
8811
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008812int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8813{
8814 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8815
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008816#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8817 !defined(MBEDTLS_SSL_PROTO_DTLS)
8818 (void) ssl;
8819#endif
8820
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008821#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8822 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8823
8824 if( max_len > mfl )
8825 max_len = mfl;
8826#endif
8827
8828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008829 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008830 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008831 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008832 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8833 const size_t overhead = (size_t) ret;
8834
8835 if( ret < 0 )
8836 return( ret );
8837
8838 if( mtu <= overhead )
8839 {
8840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8841 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8842 }
8843
8844 if( max_len > mtu - overhead )
8845 max_len = mtu - overhead;
8846 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008848
Hanno Becker0defedb2018-08-10 12:35:02 +01008849#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8850 !defined(MBEDTLS_SSL_PROTO_DTLS)
8851 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008852#endif
8853
8854 return( (int) max_len );
8855}
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#if defined(MBEDTLS_X509_CRT_PARSE_C)
8858const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008859{
8860 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008861 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008862
Hanno Beckere6824572019-02-07 13:18:46 +00008863#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008864 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008865#else
8866 return( NULL );
8867#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008872int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8873 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008874{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008875 if( ssl == NULL ||
8876 dst == NULL ||
8877 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008878 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008881 }
8882
Hanno Becker52055ae2019-02-06 14:30:46 +00008883 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008886
Paul Bakker5121ce52009-01-03 21:22:43 +00008887/*
Paul Bakker1961b702013-01-25 14:49:24 +01008888 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008893
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008894 if( ssl == NULL || ssl->conf == NULL )
8895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008898 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008904#endif
8905
Paul Bakker1961b702013-01-25 14:49:24 +01008906 return( ret );
8907}
8908
8909/*
8910 * Perform the SSL handshake
8911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008913{
8914 int ret = 0;
8915
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008916 if( ssl == NULL || ssl->conf == NULL )
8917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008924
8925 if( ret != 0 )
8926 break;
8927 }
8928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008930
8931 return( ret );
8932}
8933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934#if defined(MBEDTLS_SSL_RENEGOTIATION)
8935#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008936/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008937 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008940{
8941 int ret;
8942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008944
8945 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8947 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008948
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008949 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008950 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008952 return( ret );
8953 }
8954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008956
8957 return( 0 );
8958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008960
8961/*
8962 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963 * - any side: calling mbedtls_ssl_renegotiate(),
8964 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8965 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008966 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008967 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008971{
8972 int ret;
8973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008975
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008976 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8977 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008978
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008979 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8980 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008984 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008986 ssl->handshake->out_msg_seq = 1;
8987 else
8988 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008989 }
8990#endif
8991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8993 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008998 return( ret );
8999 }
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009002
9003 return( 0 );
9004}
9005
9006/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009007 * Renegotiate current connection on client,
9008 * or request renegotiation on server
9009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009013
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009014 if( ssl == NULL || ssl->conf == NULL )
9015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009018 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009019 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009025
9026 /* Did we already try/start sending HelloRequest? */
9027 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009029
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009030 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009035 /*
9036 * On client, either start the renegotiation process or,
9037 * if already in progress, continue the handshake
9038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009043
9044 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009047 return( ret );
9048 }
9049 }
9050 else
9051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009055 return( ret );
9056 }
9057 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009059
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009060 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009061}
9062
9063/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009064 * Check record counters and renegotiate if they're above the limit.
9065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009067{
Andres AG2196c7f2016-12-15 17:01:16 +00009068 size_t ep_len = ssl_ep_len( ssl );
9069 int in_ctr_cmp;
9070 int out_ctr_cmp;
9071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9073 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009074 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009075 {
9076 return( 0 );
9077 }
9078
Andres AG2196c7f2016-12-15 17:01:16 +00009079 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9080 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009081 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009082 ssl->conf->renego_period + ep_len, 8 - ep_len );
9083
9084 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009085 {
9086 return( 0 );
9087 }
9088
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009093
9094/*
9095 * Receive application data decrypted from the SSL layer
9096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009098{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009099 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009100 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009101
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009102 if( ssl == NULL || ssl->conf == NULL )
9103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009111 return( ret );
9112
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009113 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009115 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009116 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009117 return( ret );
9118 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009119 }
9120#endif
9121
Hanno Becker4a810fb2017-05-24 16:27:30 +01009122 /*
9123 * Check if renegotiation is necessary and/or handshake is
9124 * in process. If yes, perform/continue, and fall through
9125 * if an unexpected packet is received while the client
9126 * is waiting for the ServerHello.
9127 *
9128 * (There is no equivalent to the last condition on
9129 * the server-side as it is not treated as within
9130 * a handshake while waiting for the ClientHello
9131 * after a renegotiation request.)
9132 */
9133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009135 ret = ssl_check_ctr_renegotiate( ssl );
9136 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9137 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009140 return( ret );
9141 }
9142#endif
9143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009147 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9148 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009151 return( ret );
9152 }
9153 }
9154
Hanno Beckere41158b2017-10-23 13:30:32 +01009155 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009156 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009157 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009158 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009159 if( ssl->f_get_timer != NULL &&
9160 ssl->f_get_timer( ssl->p_timer ) == -1 )
9161 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009162 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009163 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009164
Hanno Becker327c93b2018-08-15 13:56:18 +01009165 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009166 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009167 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9168 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009169
Hanno Becker4a810fb2017-05-24 16:27:30 +01009170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9171 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009172 }
9173
9174 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009176 {
9177 /*
9178 * OpenSSL sends empty messages to randomize the IV
9179 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009180 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009183 return( 0 );
9184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009186 return( ret );
9187 }
9188 }
9189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009193
Hanno Becker4a810fb2017-05-24 16:27:30 +01009194 /*
9195 * - For client-side, expect SERVER_HELLO_REQUEST.
9196 * - For server-side, expect CLIENT_HELLO.
9197 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9198 */
9199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009201 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009203 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009206
9207 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009210 {
9211 continue;
9212 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009215 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009216#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009217
Hanno Becker4a810fb2017-05-24 16:27:30 +01009218#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009223
9224 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009226 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009227 {
9228 continue;
9229 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009232 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009233#endif /* MBEDTLS_SSL_SRV_C */
9234
Hanno Becker21df7f92017-10-17 11:03:26 +01009235#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009236 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009237 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9238 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9239 ssl->conf->allow_legacy_renegotiation ==
9240 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9241 {
9242 /*
9243 * Accept renegotiation request
9244 */
Paul Bakker48916f92012-09-16 19:57:18 +00009245
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009246 /* DTLS clients need to know renego is server-initiated */
9247#if defined(MBEDTLS_SSL_PROTO_DTLS)
9248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9249 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9250 {
9251 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9252 }
9253#endif
9254 ret = ssl_start_renegotiation( ssl );
9255 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9256 ret != 0 )
9257 {
9258 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9259 return( ret );
9260 }
9261 }
9262 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009263#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009264 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009265 /*
9266 * Refuse renegotiation
9267 */
9268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271#if defined(MBEDTLS_SSL_PROTO_SSL3)
9272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009273 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009274 /* SSLv3 does not have a "no_renegotiation" warning, so
9275 we send a fatal alert and abort the connection. */
9276 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9277 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9278 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009279 }
9280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9282#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9283 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9284 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9287 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9288 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009289 {
9290 return( ret );
9291 }
Paul Bakker48916f92012-09-16 19:57:18 +00009292 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9295 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009299 }
Paul Bakker48916f92012-09-16 19:57:18 +00009300 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009301
Hanno Becker90333da2017-10-10 11:27:13 +01009302 /* At this point, we don't know whether the renegotiation has been
9303 * completed or not. The cases to consider are the following:
9304 * 1) The renegotiation is complete. In this case, no new record
9305 * has been read yet.
9306 * 2) The renegotiation is incomplete because the client received
9307 * an application data record while awaiting the ServerHello.
9308 * 3) The renegotiation is incomplete because the client received
9309 * a non-handshake, non-application data message while awaiting
9310 * the ServerHello.
9311 * In each of these case, looping will be the proper action:
9312 * - For 1), the next iteration will read a new record and check
9313 * if it's application data.
9314 * - For 2), the loop condition isn't satisfied as application data
9315 * is present, hence continue is the same as break
9316 * - For 3), the loop condition is satisfied and read_record
9317 * will re-deliver the message that was held back by the client
9318 * when expecting the ServerHello.
9319 */
9320 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009321 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009322#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009324 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009325 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009326 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009327 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009330 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009332 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009333 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9338 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009341 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009342 }
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9347 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009348 }
9349
9350 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009351
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009352 /* We're going to return something now, cancel timer,
9353 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009355 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009356
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009358 /* If we requested renego but received AppData, resend HelloRequest.
9359 * Do it now, after setting in_offt, to avoid taking this branch
9360 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009364 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009365 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009368 return( ret );
9369 }
9370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009372#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009373 }
9374
9375 n = ( len < ssl->in_msglen )
9376 ? len : ssl->in_msglen;
9377
9378 memcpy( buf, ssl->in_offt, n );
9379 ssl->in_msglen -= n;
9380
9381 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009382 {
9383 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009384 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009385 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009387 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009388 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009389 /* more data available */
9390 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009394
Paul Bakker23986e52011-04-24 08:57:21 +00009395 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009396}
9397
9398/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009399 * Send application data to be encrypted by the SSL layer, taking care of max
9400 * fragment length and buffer size.
9401 *
9402 * According to RFC 5246 Section 6.2.1:
9403 *
9404 * Zero-length fragments of Application data MAY be sent as they are
9405 * potentially useful as a traffic analysis countermeasure.
9406 *
9407 * Therefore, it is possible that the input message length is 0 and the
9408 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009409 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009410static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009411 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009412{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009413 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9414 const size_t max_len = (size_t) ret;
9415
9416 if( ret < 0 )
9417 {
9418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9419 return( ret );
9420 }
9421
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009422 if( len > max_len )
9423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009428 "maximum fragment length: %d > %d",
9429 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009431 }
9432 else
9433#endif
9434 len = max_len;
9435 }
Paul Bakker887bd502011-06-08 13:10:54 +00009436
Paul Bakker5121ce52009-01-03 21:22:43 +00009437 if( ssl->out_left != 0 )
9438 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009439 /*
9440 * The user has previously tried to send the data and
9441 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9442 * written. In this case, we expect the high-level write function
9443 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009448 return( ret );
9449 }
9450 }
Paul Bakker887bd502011-06-08 13:10:54 +00009451 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009452 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009453 /*
9454 * The user is trying to send a message the first time, so we need to
9455 * copy the data into the internal buffers and setup the data structure
9456 * to keep track of partial writes
9457 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009458 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009460 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009461
Hanno Becker67bc7c32018-08-06 11:33:50 +01009462 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009465 return( ret );
9466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009467 }
9468
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009469 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009470}
9471
9472/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009473 * Write application data, doing 1/n-1 splitting if necessary.
9474 *
9475 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009476 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009477 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009480static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009481 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009482{
9483 int ret;
9484
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009485 if( ssl->conf->cbc_record_splitting ==
9486 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009487 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9489 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9490 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009491 {
9492 return( ssl_write_real( ssl, buf, len ) );
9493 }
9494
9495 if( ssl->split_done == 0 )
9496 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009497 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009498 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009499 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009500 }
9501
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009502 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9503 return( ret );
9504 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009505
9506 return( ret + 1 );
9507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009509
9510/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009511 * Write application data (public-facing wrapper)
9512 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009513int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009514{
9515 int ret;
9516
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009518
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009519 if( ssl == NULL || ssl->conf == NULL )
9520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9521
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009522#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009523 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9524 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009525 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009526 return( ret );
9527 }
9528#endif
9529
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009530 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009531 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009532 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009533 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009535 return( ret );
9536 }
9537 }
9538
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009539#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009540 ret = ssl_write_split( ssl, buf, len );
9541#else
9542 ret = ssl_write_real( ssl, buf, len );
9543#endif
9544
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009546
9547 return( ret );
9548}
9549
9550/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009551 * Notify the peer that the connection is being closed
9552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009554{
9555 int ret;
9556
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009557 if( ssl == NULL || ssl->conf == NULL )
9558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009561
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009562 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9568 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9569 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009572 return( ret );
9573 }
9574 }
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009577
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009578 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009579}
9580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009582{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009583 if( transform == NULL )
9584 return;
9585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009587 deflateEnd( &transform->ctx_deflate );
9588 inflateEnd( &transform->ctx_inflate );
9589#endif
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9592 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 mbedtls_md_free( &transform->md_ctx_enc );
9595 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009596
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009597 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009598}
9599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_X509_CRT_PARSE_C)
9601static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009604
9605 while( cur != NULL )
9606 {
9607 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009609 cur = next;
9610 }
9611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009613
Hanno Becker0271f962018-08-16 13:23:47 +01009614#if defined(MBEDTLS_SSL_PROTO_DTLS)
9615
9616static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9617{
9618 unsigned offset;
9619 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9620
9621 if( hs == NULL )
9622 return;
9623
Hanno Becker283f5ef2018-08-24 09:34:47 +01009624 ssl_free_buffered_record( ssl );
9625
Hanno Becker0271f962018-08-16 13:23:47 +01009626 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009627 ssl_buffering_free_slot( ssl, offset );
9628}
9629
9630static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9631 uint8_t slot )
9632{
9633 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9634 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009635
9636 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9637 return;
9638
Hanno Beckere605b192018-08-21 15:59:07 +01009639 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009640 {
Hanno Beckere605b192018-08-21 15:59:07 +01009641 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009642 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009643 mbedtls_free( hs_buf->data );
9644 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009645 }
9646}
9647
9648#endif /* MBEDTLS_SSL_PROTO_DTLS */
9649
Gilles Peskine9b562d52018-04-25 20:32:43 +02009650void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009651{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009652 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9653
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009654 if( handshake == NULL )
9655 return;
9656
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009657#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9658 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9659 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009660 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009661 handshake->async_in_progress = 0;
9662 }
9663#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9664
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009665#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9666 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9667 mbedtls_md5_free( &handshake->fin_md5 );
9668 mbedtls_sha1_free( &handshake->fin_sha1 );
9669#endif
9670#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9671#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009672#if defined(MBEDTLS_USE_PSA_CRYPTO)
9673 psa_hash_abort( &handshake->fin_sha256_psa );
9674#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009675 mbedtls_sha256_free( &handshake->fin_sha256 );
9676#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009677#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009678#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009679#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009680 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009681#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009682 mbedtls_sha512_free( &handshake->fin_sha512 );
9683#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009684#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009685#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687#if defined(MBEDTLS_DHM_C)
9688 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690#if defined(MBEDTLS_ECDH_C)
9691 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009692#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009693#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009694 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009695#if defined(MBEDTLS_SSL_CLI_C)
9696 mbedtls_free( handshake->ecjpake_cache );
9697 handshake->ecjpake_cache = NULL;
9698 handshake->ecjpake_cache_len = 0;
9699#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009700#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009701
Janos Follath4ae5c292016-02-10 11:27:43 +00009702#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9703 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009704 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009706#endif
9707
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009708#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9709 if( handshake->psk != NULL )
9710 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009711 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009712 mbedtls_free( handshake->psk );
9713 }
9714#endif
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9717 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009718 /*
9719 * Free only the linked list wrapper, not the keys themselves
9720 * since the belong to the SNI callback
9721 */
9722 if( handshake->sni_key_cert != NULL )
9723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009725
9726 while( cur != NULL )
9727 {
9728 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009730 cur = next;
9731 }
9732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009734
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009735#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009736 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009737 if( handshake->ecrs_peer_cert != NULL )
9738 {
9739 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9740 mbedtls_free( handshake->ecrs_peer_cert );
9741 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009742#endif
9743
Hanno Becker75173122019-02-06 16:18:31 +00009744#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9745 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9746 mbedtls_pk_free( &handshake->peer_pubkey );
9747#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#if defined(MBEDTLS_SSL_PROTO_DTLS)
9750 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009751 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009752 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009753#endif
9754
Hanno Becker4a63ed42019-01-08 11:39:35 +00009755#if defined(MBEDTLS_ECDH_C) && \
9756 defined(MBEDTLS_USE_PSA_CRYPTO)
9757 psa_destroy_key( handshake->ecdh_psa_privkey );
9758#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9759
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009760 mbedtls_platform_zeroize( handshake,
9761 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009762}
9763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009765{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009766 if( session == NULL )
9767 return;
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009770 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009771#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009772
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009773#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009775#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009776
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009777 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009778}
9779
Paul Bakker5121ce52009-01-03 21:22:43 +00009780/*
9781 * Free an SSL context
9782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009784{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009785 if( ssl == NULL )
9786 return;
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009789
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009790 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 {
Angus Grattond8213d02016-05-25 20:56:48 +10009792 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009794 }
9795
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009796 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009797 {
Angus Grattond8213d02016-05-25 20:56:48 +10009798 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009800 }
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009803 if( ssl->compress_buf != NULL )
9804 {
Angus Grattond8213d02016-05-25 20:56:48 +10009805 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009807 }
9808#endif
9809
Paul Bakker48916f92012-09-16 19:57:18 +00009810 if( ssl->transform )
9811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 mbedtls_ssl_transform_free( ssl->transform );
9813 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009814 }
9815
9816 if( ssl->handshake )
9817 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009818 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9820 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 mbedtls_free( ssl->handshake );
9823 mbedtls_free( ssl->transform_negotiate );
9824 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009825 }
9826
Paul Bakkerc0463502013-02-14 11:19:38 +01009827 if( ssl->session )
9828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 mbedtls_ssl_session_free( ssl->session );
9830 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009831 }
9832
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009833#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009834 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009835 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009836 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9842 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9845 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009846 }
9847#endif
9848
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009849#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009851#endif
9852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009854
Paul Bakker86f04f42013-02-14 11:20:09 +01009855 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009856 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009857}
9858
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009859/*
9860 * Initialze mbedtls_ssl_config
9861 */
9862void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9863{
9864 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9865}
9866
Simon Butcherc97b6972015-12-27 23:48:17 +00009867#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009868static int ssl_preset_default_hashes[] = {
9869#if defined(MBEDTLS_SHA512_C)
9870 MBEDTLS_MD_SHA512,
9871 MBEDTLS_MD_SHA384,
9872#endif
9873#if defined(MBEDTLS_SHA256_C)
9874 MBEDTLS_MD_SHA256,
9875 MBEDTLS_MD_SHA224,
9876#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009877#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009878 MBEDTLS_MD_SHA1,
9879#endif
9880 MBEDTLS_MD_NONE
9881};
Simon Butcherc97b6972015-12-27 23:48:17 +00009882#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009883
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009884static int ssl_preset_suiteb_ciphersuites[] = {
9885 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9886 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9887 0
9888};
9889
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009890#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009891static int ssl_preset_suiteb_hashes[] = {
9892 MBEDTLS_MD_SHA256,
9893 MBEDTLS_MD_SHA384,
9894 MBEDTLS_MD_NONE
9895};
9896#endif
9897
9898#if defined(MBEDTLS_ECP_C)
9899static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9900 MBEDTLS_ECP_DP_SECP256R1,
9901 MBEDTLS_ECP_DP_SECP384R1,
9902 MBEDTLS_ECP_DP_NONE
9903};
9904#endif
9905
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009906/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009907 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009908 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009909int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009910 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009911{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009912#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009913 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009914#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009915
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009916 /* Use the functions here so that they are covered in tests,
9917 * but otherwise access member directly for efficiency */
9918 mbedtls_ssl_conf_endpoint( conf, endpoint );
9919 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009920
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009921 /*
9922 * Things that are common to all presets
9923 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009924#if defined(MBEDTLS_SSL_CLI_C)
9925 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9926 {
9927 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9928#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9929 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9930#endif
9931 }
9932#endif
9933
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009934#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009935 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009936#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009937
9938#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9939 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9940#endif
9941
9942#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9943 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9944#endif
9945
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009946#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9947 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9948#endif
9949
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009950#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009951 conf->f_cookie_write = ssl_cookie_write_dummy;
9952 conf->f_cookie_check = ssl_cookie_check_dummy;
9953#endif
9954
9955#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9956 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9957#endif
9958
Janos Follath088ce432017-04-10 12:42:31 +01009959#if defined(MBEDTLS_SSL_SRV_C)
9960 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9961#endif
9962
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009963#if defined(MBEDTLS_SSL_PROTO_DTLS)
9964 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9965 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9966#endif
9967
9968#if defined(MBEDTLS_SSL_RENEGOTIATION)
9969 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009970 memset( conf->renego_period, 0x00, 2 );
9971 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009972#endif
9973
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009974#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9975 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9976 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009977 const unsigned char dhm_p[] =
9978 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9979 const unsigned char dhm_g[] =
9980 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9981
Hanno Beckera90658f2017-10-04 15:29:08 +01009982 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9983 dhm_p, sizeof( dhm_p ),
9984 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009985 {
9986 return( ret );
9987 }
9988 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009989#endif
9990
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009991 /*
9992 * Preset-specific defaults
9993 */
9994 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009995 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009996 /*
9997 * NSA Suite B
9998 */
9999 case MBEDTLS_SSL_PRESET_SUITEB:
10000 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10001 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10002 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10003 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10004
10005 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10006 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10007 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10008 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10009 ssl_preset_suiteb_ciphersuites;
10010
10011#if defined(MBEDTLS_X509_CRT_PARSE_C)
10012 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010013#endif
10014
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010015#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010016 conf->sig_hashes = ssl_preset_suiteb_hashes;
10017#endif
10018
10019#if defined(MBEDTLS_ECP_C)
10020 conf->curve_list = ssl_preset_suiteb_curves;
10021#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010022 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010023
10024 /*
10025 * Default
10026 */
10027 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010028 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10029 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10030 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10031 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10032 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10033 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10034 MBEDTLS_SSL_MIN_MINOR_VERSION :
10035 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010036 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10037 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10038
10039#if defined(MBEDTLS_SSL_PROTO_DTLS)
10040 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10041 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10042#endif
10043
10044 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10045 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10046 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10047 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10048 mbedtls_ssl_list_ciphersuites();
10049
10050#if defined(MBEDTLS_X509_CRT_PARSE_C)
10051 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10052#endif
10053
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010054#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010055 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010056#endif
10057
10058#if defined(MBEDTLS_ECP_C)
10059 conf->curve_list = mbedtls_ecp_grp_id_list();
10060#endif
10061
10062#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10063 conf->dhm_min_bitlen = 1024;
10064#endif
10065 }
10066
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010067 return( 0 );
10068}
10069
10070/*
10071 * Free mbedtls_ssl_config
10072 */
10073void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10074{
10075#if defined(MBEDTLS_DHM_C)
10076 mbedtls_mpi_free( &conf->dhm_P );
10077 mbedtls_mpi_free( &conf->dhm_G );
10078#endif
10079
10080#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10081 if( conf->psk != NULL )
10082 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010083 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010084 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010085 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010086 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010087 }
10088
10089 if( conf->psk_identity != NULL )
10090 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010091 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010092 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010093 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010094 conf->psk_identity_len = 0;
10095 }
10096#endif
10097
10098#if defined(MBEDTLS_X509_CRT_PARSE_C)
10099 ssl_key_cert_free( conf->key_cert );
10100#endif
10101
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010102 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010103}
10104
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010105#if defined(MBEDTLS_PK_C) && \
10106 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010107/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112#if defined(MBEDTLS_RSA_C)
10113 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10114 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116#if defined(MBEDTLS_ECDSA_C)
10117 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10118 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010121}
10122
Hanno Becker7e5437a2017-04-28 17:15:26 +010010123unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10124{
10125 switch( type ) {
10126 case MBEDTLS_PK_RSA:
10127 return( MBEDTLS_SSL_SIG_RSA );
10128 case MBEDTLS_PK_ECDSA:
10129 case MBEDTLS_PK_ECKEY:
10130 return( MBEDTLS_SSL_SIG_ECDSA );
10131 default:
10132 return( MBEDTLS_SSL_SIG_ANON );
10133 }
10134}
10135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010137{
10138 switch( sig )
10139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140#if defined(MBEDTLS_RSA_C)
10141 case MBEDTLS_SSL_SIG_RSA:
10142 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#if defined(MBEDTLS_ECDSA_C)
10145 case MBEDTLS_SSL_SIG_ECDSA:
10146 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010147#endif
10148 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010150 }
10151}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010152#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010153
Hanno Becker7e5437a2017-04-28 17:15:26 +010010154#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10155 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10156
10157/* Find an entry in a signature-hash set matching a given hash algorithm. */
10158mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10159 mbedtls_pk_type_t sig_alg )
10160{
10161 switch( sig_alg )
10162 {
10163 case MBEDTLS_PK_RSA:
10164 return( set->rsa );
10165 case MBEDTLS_PK_ECDSA:
10166 return( set->ecdsa );
10167 default:
10168 return( MBEDTLS_MD_NONE );
10169 }
10170}
10171
10172/* Add a signature-hash-pair to a signature-hash set */
10173void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10174 mbedtls_pk_type_t sig_alg,
10175 mbedtls_md_type_t md_alg )
10176{
10177 switch( sig_alg )
10178 {
10179 case MBEDTLS_PK_RSA:
10180 if( set->rsa == MBEDTLS_MD_NONE )
10181 set->rsa = md_alg;
10182 break;
10183
10184 case MBEDTLS_PK_ECDSA:
10185 if( set->ecdsa == MBEDTLS_MD_NONE )
10186 set->ecdsa = md_alg;
10187 break;
10188
10189 default:
10190 break;
10191 }
10192}
10193
10194/* Allow exactly one hash algorithm for each signature. */
10195void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10196 mbedtls_md_type_t md_alg )
10197{
10198 set->rsa = md_alg;
10199 set->ecdsa = md_alg;
10200}
10201
10202#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10203 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10204
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010205/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010206 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010209{
10210 switch( hash )
10211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212#if defined(MBEDTLS_MD5_C)
10213 case MBEDTLS_SSL_HASH_MD5:
10214 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_SHA1_C)
10217 case MBEDTLS_SSL_HASH_SHA1:
10218 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_SHA256_C)
10221 case MBEDTLS_SSL_HASH_SHA224:
10222 return( MBEDTLS_MD_SHA224 );
10223 case MBEDTLS_SSL_HASH_SHA256:
10224 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226#if defined(MBEDTLS_SHA512_C)
10227 case MBEDTLS_SSL_HASH_SHA384:
10228 return( MBEDTLS_MD_SHA384 );
10229 case MBEDTLS_SSL_HASH_SHA512:
10230 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010231#endif
10232 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010234 }
10235}
10236
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010237/*
10238 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10239 */
10240unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10241{
10242 switch( md )
10243 {
10244#if defined(MBEDTLS_MD5_C)
10245 case MBEDTLS_MD_MD5:
10246 return( MBEDTLS_SSL_HASH_MD5 );
10247#endif
10248#if defined(MBEDTLS_SHA1_C)
10249 case MBEDTLS_MD_SHA1:
10250 return( MBEDTLS_SSL_HASH_SHA1 );
10251#endif
10252#if defined(MBEDTLS_SHA256_C)
10253 case MBEDTLS_MD_SHA224:
10254 return( MBEDTLS_SSL_HASH_SHA224 );
10255 case MBEDTLS_MD_SHA256:
10256 return( MBEDTLS_SSL_HASH_SHA256 );
10257#endif
10258#if defined(MBEDTLS_SHA512_C)
10259 case MBEDTLS_MD_SHA384:
10260 return( MBEDTLS_SSL_HASH_SHA384 );
10261 case MBEDTLS_MD_SHA512:
10262 return( MBEDTLS_SSL_HASH_SHA512 );
10263#endif
10264 default:
10265 return( MBEDTLS_SSL_HASH_NONE );
10266 }
10267}
10268
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010269#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010270/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010271 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010272 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010273 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010274int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010277
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010278 if( ssl->conf->curve_list == NULL )
10279 return( -1 );
10280
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010281 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010282 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010283 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010284
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010285 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010286}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010287#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010288
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010289#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010290/*
10291 * Check if a hash proposed by the peer is in our list.
10292 * Return 0 if we're willing to use it, -1 otherwise.
10293 */
10294int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10295 mbedtls_md_type_t md )
10296{
10297 const int *cur;
10298
10299 if( ssl->conf->sig_hashes == NULL )
10300 return( -1 );
10301
10302 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10303 if( *cur == (int) md )
10304 return( 0 );
10305
10306 return( -1 );
10307}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010308#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310#if defined(MBEDTLS_X509_CRT_PARSE_C)
10311int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10312 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010313 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010314 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010315{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010316 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010318 int usage = 0;
10319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010321 const char *ext_oid;
10322 size_t ext_len;
10323#endif
10324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10326 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010327 ((void) cert);
10328 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010329 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010330#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10333 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010334 {
10335 /* Server part of the key exchange */
10336 switch( ciphersuite->key_exchange )
10337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 case MBEDTLS_KEY_EXCHANGE_RSA:
10339 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010340 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010341 break;
10342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10344 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10345 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10346 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010347 break;
10348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10350 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010351 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010352 break;
10353
10354 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 case MBEDTLS_KEY_EXCHANGE_NONE:
10356 case MBEDTLS_KEY_EXCHANGE_PSK:
10357 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10358 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010359 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010360 usage = 0;
10361 }
10362 }
10363 else
10364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10366 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010367 }
10368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010370 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010371 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010372 ret = -1;
10373 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010374#else
10375 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10379 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10382 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010383 }
10384 else
10385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10387 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010388 }
10389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010391 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010392 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010393 ret = -1;
10394 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010396
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010397 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010400
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010401/*
10402 * Convert version numbers to/from wire format
10403 * and, for DTLS, to/from TLS equivalent.
10404 *
10405 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010406 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010407 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10408 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010411 unsigned char ver[2] )
10412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413#if defined(MBEDTLS_SSL_PROTO_DTLS)
10414 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010417 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10418
10419 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10420 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10421 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010422 else
10423#else
10424 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010425#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010426 {
10427 ver[0] = (unsigned char) major;
10428 ver[1] = (unsigned char) minor;
10429 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010430}
10431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010433 const unsigned char ver[2] )
10434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435#if defined(MBEDTLS_SSL_PROTO_DTLS)
10436 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010437 {
10438 *major = 255 - ver[0] + 2;
10439 *minor = 255 - ver[1] + 1;
10440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010442 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10443 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010444 else
10445#else
10446 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010447#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010448 {
10449 *major = ver[0];
10450 *minor = ver[1];
10451 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010452}
10453
Simon Butcher99000142016-10-13 17:21:01 +010010454int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10455{
10456#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10457 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10458 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10459
10460 switch( md )
10461 {
10462#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10463#if defined(MBEDTLS_MD5_C)
10464 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010465 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010466#endif
10467#if defined(MBEDTLS_SHA1_C)
10468 case MBEDTLS_SSL_HASH_SHA1:
10469 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10470 break;
10471#endif
10472#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10473#if defined(MBEDTLS_SHA512_C)
10474 case MBEDTLS_SSL_HASH_SHA384:
10475 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10476 break;
10477#endif
10478#if defined(MBEDTLS_SHA256_C)
10479 case MBEDTLS_SSL_HASH_SHA256:
10480 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10481 break;
10482#endif
10483 default:
10484 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10485 }
10486
10487 return 0;
10488#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10489 (void) ssl;
10490 (void) md;
10491
10492 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10494}
10495
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010496#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10497 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10498int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10499 unsigned char *output,
10500 unsigned char *data, size_t data_len )
10501{
10502 int ret = 0;
10503 mbedtls_md5_context mbedtls_md5;
10504 mbedtls_sha1_context mbedtls_sha1;
10505
10506 mbedtls_md5_init( &mbedtls_md5 );
10507 mbedtls_sha1_init( &mbedtls_sha1 );
10508
10509 /*
10510 * digitally-signed struct {
10511 * opaque md5_hash[16];
10512 * opaque sha_hash[20];
10513 * };
10514 *
10515 * md5_hash
10516 * MD5(ClientHello.random + ServerHello.random
10517 * + ServerParams);
10518 * sha_hash
10519 * SHA(ClientHello.random + ServerHello.random
10520 * + ServerParams);
10521 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010522 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010523 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010525 goto exit;
10526 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010527 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010528 ssl->handshake->randbytes, 64 ) ) != 0 )
10529 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010531 goto exit;
10532 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010533 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010534 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010536 goto exit;
10537 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010538 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010539 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010541 goto exit;
10542 }
10543
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010544 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010545 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010547 goto exit;
10548 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010549 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010550 ssl->handshake->randbytes, 64 ) ) != 0 )
10551 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010553 goto exit;
10554 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010555 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010556 data_len ) ) != 0 )
10557 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010559 goto exit;
10560 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010561 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010562 output + 16 ) ) != 0 )
10563 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010565 goto exit;
10566 }
10567
10568exit:
10569 mbedtls_md5_free( &mbedtls_md5 );
10570 mbedtls_sha1_free( &mbedtls_sha1 );
10571
10572 if( ret != 0 )
10573 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10574 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10575
10576 return( ret );
10577
10578}
10579#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10580 MBEDTLS_SSL_PROTO_TLS1_1 */
10581
10582#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10583 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010584
10585#if defined(MBEDTLS_USE_PSA_CRYPTO)
10586int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10587 unsigned char *hash, size_t *hashlen,
10588 unsigned char *data, size_t data_len,
10589 mbedtls_md_type_t md_alg )
10590{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010591 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010592 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010593 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10594
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010596
10597 if( ( status = psa_hash_setup( &hash_operation,
10598 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010599 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010600 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010601 goto exit;
10602 }
10603
Andrzej Kurek814feff2019-01-14 04:35:19 -050010604 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10605 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010606 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010607 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010608 goto exit;
10609 }
10610
Andrzej Kurek814feff2019-01-14 04:35:19 -050010611 if( ( status = psa_hash_update( &hash_operation,
10612 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010613 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010614 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010615 goto exit;
10616 }
10617
Andrzej Kurek814feff2019-01-14 04:35:19 -050010618 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10619 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010620 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010621 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010622 goto exit;
10623 }
10624
10625exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010626 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010627 {
10628 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10629 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010630 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010631 {
10632 case PSA_ERROR_NOT_SUPPORTED:
10633 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010634 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010635 case PSA_ERROR_BUFFER_TOO_SMALL:
10636 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10637 case PSA_ERROR_INSUFFICIENT_MEMORY:
10638 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10639 default:
10640 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10641 }
10642 }
10643 return( 0 );
10644}
10645
10646#else
10647
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010648int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010649 unsigned char *hash, size_t *hashlen,
10650 unsigned char *data, size_t data_len,
10651 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010652{
10653 int ret = 0;
10654 mbedtls_md_context_t ctx;
10655 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010656 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010657
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010659
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010660 mbedtls_md_init( &ctx );
10661
10662 /*
10663 * digitally-signed struct {
10664 * opaque client_random[32];
10665 * opaque server_random[32];
10666 * ServerDHParams params;
10667 * };
10668 */
10669 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10670 {
10671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10672 goto exit;
10673 }
10674 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10675 {
10676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10677 goto exit;
10678 }
10679 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10680 {
10681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10682 goto exit;
10683 }
10684 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10685 {
10686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10687 goto exit;
10688 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010689 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010690 {
10691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10692 goto exit;
10693 }
10694
10695exit:
10696 mbedtls_md_free( &ctx );
10697
10698 if( ret != 0 )
10699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10700 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10701
10702 return( ret );
10703}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010704#endif /* MBEDTLS_USE_PSA_CRYPTO */
10705
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010706#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10707 MBEDTLS_SSL_PROTO_TLS1_2 */
10708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709#endif /* MBEDTLS_SSL_TLS_C */