blob: d8b5f2bc3fd7efff05e56e338e31b3ba17946aa7 [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;
Ron Eldor3b350852019-05-07 18:31:49 +0300428 unsigned char *tmp;
429 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 const mbedtls_md_info_t *md_info;
432 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100433 int ret;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Ron Eldor3b350852019-05-07 18:31:49 +0300437 tmp_len = 20 + strlen( label ) + rlen;
438 tmp = mbedtls_calloc( 1, tmp_len );
439 if( tmp == NULL )
440 {
441 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
442 goto exit;
443 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 hs = ( slen + 1 ) / 2;
446 S1 = secret;
447 S2 = secret + slen - hs;
448
449 nb = strlen( label );
450 memcpy( tmp + 20, label, nb );
451 memcpy( tmp + 20 + nb, random, rlen );
452 nb += rlen;
453
454 /*
455 * First compute P_md5(secret,label+random)[0..dlen]
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300458 {
459 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
460 goto exit;
461 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300464 {
465 goto exit;
466 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
469 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
470 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 for( i = 0; i < dlen; i += 16 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_hmac_reset ( &md_ctx );
475 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
476 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_hmac_reset ( &md_ctx );
479 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
480 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
483
484 for( j = 0; j < k; j++ )
485 dstbuf[i + j] = h_i[j];
486 }
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 /*
491 * XOR out with P_sha1(secret,label+random)[0..dlen]
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300494 {
495 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
496 goto exit;
497 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300500 {
501 goto exit;
502 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
505 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
506 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 for( i = 0; i < dlen; i += 20 )
509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_hmac_reset ( &md_ctx );
511 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
512 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_md_hmac_reset ( &md_ctx );
515 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
516 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
519
520 for( j = 0; j < k; j++ )
521 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
522 }
523
Ron Eldor3b350852019-05-07 18:31:49 +0300524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526
Ron Eldor3b350852019-05-07 18:31:49 +0300527 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500528 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Ron Eldor3b350852019-05-07 18:31:49 +0300530 mbedtls_free( tmp );
531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500536#if defined(MBEDTLS_USE_PSA_CRYPTO)
537static int tls_prf_generic( mbedtls_md_type_t md_type,
538 const unsigned char *secret, size_t slen,
539 const char *label,
540 const unsigned char *random, size_t rlen,
541 unsigned char *dstbuf, size_t dlen )
542{
543 psa_status_t status;
544 psa_algorithm_t alg;
545 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500546 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500547 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
548
Andrzej Kurek2f760752019-01-28 08:08:15 -0500549 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500550 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500551
Andrzej Kurekc929a822019-01-14 03:51:11 -0500552 if( md_type == MBEDTLS_MD_SHA384 )
553 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
554 else
555 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
556
Andrzej Kurek2f760752019-01-28 08:08:15 -0500557 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500558 psa_key_policy_set_usage( &policy,
559 PSA_KEY_USAGE_DERIVE,
560 alg );
561 status = psa_set_key_policy( master_slot, &policy );
562 if( status != PSA_SUCCESS )
563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
564
565 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
566 if( status != PSA_SUCCESS )
567 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
568
569 status = psa_key_derivation( &generator,
570 master_slot, alg,
571 random, rlen,
572 (unsigned char const *) label,
573 (size_t) strlen( label ),
574 dlen );
575 if( status != PSA_SUCCESS )
576 {
577 psa_generator_abort( &generator );
578 psa_destroy_key( master_slot );
579 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
580 }
581
582 status = psa_generator_read( &generator, dstbuf, dlen );
583 if( status != PSA_SUCCESS )
584 {
585 psa_generator_abort( &generator );
586 psa_destroy_key( master_slot );
587 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
588 }
589
590 status = psa_generator_abort( &generator );
591 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500592 {
593 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500594 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500595 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500596
597 status = psa_destroy_key( master_slot );
598 if( status != PSA_SUCCESS )
599 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
600
Andrzej Kurek33171262019-01-15 03:25:18 -0500601 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500602}
603
604#else /* MBEDTLS_USE_PSA_CRYPTO */
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300614 unsigned char *tmp;
615 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
Ron Eldor3b350852019-05-07 18:31:49 +0300628 tmp_len = md_len + strlen( label ) + rlen;
629 tmp = mbedtls_calloc( 1, tmp_len );
630 if( tmp == NULL )
631 {
632 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
633 goto exit;
634 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
636 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 memcpy( tmp + md_len, label, nb );
638 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639 nb += rlen;
640
641 /*
642 * Compute P_<hash>(secret, label + random)[0..dlen]
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300645 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
648 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
649 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100650
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_md_hmac_reset ( &md_ctx );
654 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
655 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_md_hmac_reset ( &md_ctx );
658 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
659 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000662
663 for( j = 0; j < k; j++ )
664 dstbuf[i + j] = h_i[j];
665 }
666
Ron Eldor3b350852019-05-07 18:31:49 +0300667exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669
Ron Eldor3b350852019-05-07 18:31:49 +0300670 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500671 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000672
Ron Eldor3b350852019-05-07 18:31:49 +0300673 mbedtls_free( tmp );
674
675 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000676}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100679static int tls_prf_sha256( const unsigned char *secret, size_t slen,
680 const char *label,
681 const unsigned char *random, size_t rlen,
682 unsigned char *dstbuf, size_t dlen )
683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100685 label, random, rlen, dstbuf, dlen ) );
686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200690static int tls_prf_sha384( const unsigned char *secret, size_t slen,
691 const char *label,
692 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000693 unsigned char *dstbuf, size_t dlen )
694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100696 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_SHA512_C */
699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
704 defined(MBEDTLS_SSL_PROTO_TLS1_1)
705static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706#endif
Paul Bakker380da532012-04-18 16:10:25 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_SSL_PROTO_SSL3)
709static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
710static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#endif
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
714static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
715static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
719#if defined(MBEDTLS_SHA256_C)
720static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
721static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
722static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200723#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_SHA512_C)
726static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
727static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
728static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731
Hanno Becker7d0a5692018-10-23 15:26:22 +0100732#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
733 defined(MBEDTLS_USE_PSA_CRYPTO)
734static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
735{
736 if( ssl->conf->f_psk != NULL )
737 {
738 /* If we've used a callback to select the PSK,
739 * the static configuration is irrelevant. */
740 if( ssl->handshake->psk_opaque != 0 )
741 return( 1 );
742
743 return( 0 );
744 }
745
746 if( ssl->conf->psk_opaque != 0 )
747 return( 1 );
748
749 return( 0 );
750}
751#endif /* MBEDTLS_USE_PSA_CRYPTO &&
752 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
753
Ron Eldorcf280092019-05-14 20:19:13 +0300754#if defined(MBEDTLS_SSL_EXPORT_KEYS)
755static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
756{
757#if defined(MBEDTLS_SSL_PROTO_SSL3)
758 if( tls_prf == ssl3_prf )
759 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300760 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300761 }
762 else
763#endif
764#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
765 if( tls_prf == tls1_prf )
766 {
767 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
768 }
769 else
770#endif
771#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
772#if defined(MBEDTLS_SHA512_C)
773 if( tls_prf == tls_prf_sha384 )
774 {
775 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
776 }
777 else
778#endif
779#if defined(MBEDTLS_SHA256_C)
780 if( tls_prf == tls_prf_sha256 )
781 {
782 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
783 }
784 else
785#endif
786#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
787 return( MBEDTLS_SSL_TLS_PRF_NONE );
788}
789#endif /* MBEDTLS_SSL_EXPORT_KEYS */
790
Ron Eldor51d3ab52019-05-12 14:54:30 +0300791int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
792 const unsigned char *secret, size_t slen,
793 const char *label,
794 const unsigned char *random, size_t rlen,
795 unsigned char *dstbuf, size_t dlen )
796{
797 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
798
799 switch( prf )
800 {
801#if defined(MBEDTLS_SSL_PROTO_SSL3)
802 case MBEDTLS_SSL_TLS_PRF_SSL3:
803 tls_prf = ssl3_prf;
804 break;
805#endif
806#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
807 case MBEDTLS_SSL_TLS_PRF_TLS1:
808 tls_prf = tls1_prf;
809 break;
810#endif
811#if defined(MBEDTLS_SHA512_C)
812 case MBEDTLS_SSL_TLS_PRF_SHA384:
813 tls_prf = tls_prf_sha384;
814 break;
815#endif
816#if defined(MBEDTLS_SHA256_C)
817 case MBEDTLS_SSL_TLS_PRF_SHA256:
818 tls_prf = tls_prf_sha256;
819 break;
820#endif
821 default:
822 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
823 }
824
825 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
826}
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000829{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200830 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000831#if defined(MBEDTLS_USE_PSA_CRYPTO)
832 int psa_fallthrough;
833#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 unsigned char keyblk[256];
836 unsigned char *key1;
837 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100838 unsigned char *mac_enc;
839 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000840 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200841 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000842 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000843 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 const mbedtls_cipher_info_t *cipher_info;
845 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100846
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000847 /* cf. RFC 5246, Section 8.1:
848 * "The master secret is always exactly 48 bytes in length." */
849 size_t const master_secret_len = 48;
850
Hanno Becker35b23c72018-10-23 12:10:41 +0100851#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
852 unsigned char session_hash[48];
853#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_ssl_session *session = ssl->session_negotiate;
856 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
857 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000861#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
862 transform->encrypt_then_mac = session->encrypt_then_mac;
863#endif
864 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000865
866 ciphersuite_info = handshake->ciphersuite_info;
867 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100868 if( cipher_info == NULL )
869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000871 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100873 }
874
Hanno Beckere694c3e2017-12-27 21:34:08 +0000875 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100876 if( md_info == NULL )
877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000879 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100881 }
882
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000884 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_SSL_PROTO_SSL3)
887 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000888 {
Paul Bakker48916f92012-09-16 19:57:18 +0000889 handshake->tls_prf = ssl3_prf;
890 handshake->calc_verify = ssl_calc_verify_ssl;
891 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000892 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200893 else
894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
896 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000897 {
Paul Bakker48916f92012-09-16 19:57:18 +0000898 handshake->tls_prf = tls1_prf;
899 handshake->calc_verify = ssl_calc_verify_tls;
900 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000901 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200902 else
903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
905#if defined(MBEDTLS_SHA512_C)
906 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000907 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000908 {
Paul Bakker48916f92012-09-16 19:57:18 +0000909 handshake->tls_prf = tls_prf_sha384;
910 handshake->calc_verify = ssl_calc_verify_tls_sha384;
911 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000912 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000913 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_SHA256_C)
916 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000917 {
Paul Bakker48916f92012-09-16 19:57:18 +0000918 handshake->tls_prf = tls_prf_sha256;
919 handshake->calc_verify = ssl_calc_verify_tls_sha256;
920 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000921 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200922 else
923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200928 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000929
930 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 * SSLv3:
932 * master =
933 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
934 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
935 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200936 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200937 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 * master = PRF( premaster, "master secret", randbytes )[0..47]
939 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100940 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
943 }
944 else
945 {
946 /* The label for the KDF used for key expansion.
947 * This is either "master secret" or "extended master secret"
948 * depending on whether the Extended Master Secret extension
949 * is used. */
950 char const *lbl = "master secret";
951
952 /* The salt for the KDF used for key expansion.
953 * - If the Extended Master Secret extension is not used,
954 * this is ClientHello.Random + ServerHello.Random
955 * (see Sect. 8.1 in RFC 5246).
956 * - If the Extended Master Secret extension is used,
957 * this is the transcript of the handshake so far.
958 * (see Sect. 4 in RFC 7627). */
959 unsigned char const *salt = handshake->randbytes;
960 size_t salt_len = 64;
961
962#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200966
Hanno Becker35b23c72018-10-23 12:10:41 +0100967 lbl = "extended master secret";
968 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200969 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
971 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000974 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
975 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100976 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000977 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200978 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100979#endif /* MBEDTLS_SHA512_C */
980 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200981 }
982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100984 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200985
Hanno Becker35b23c72018-10-23 12:10:41 +0100986 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200987 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100988#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
989
Hanno Becker7d0a5692018-10-23 15:26:22 +0100990#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
991 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
992 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
993 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
994 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100995 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100996 /* Perform PSK-to-MS expansion in a single step. */
997 psa_status_t status;
998 psa_algorithm_t alg;
999 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001000 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001001
1002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1003
1004 psk = ssl->conf->psk_opaque;
1005 if( ssl->handshake->psk_opaque != 0 )
1006 psk = ssl->handshake->psk_opaque;
1007
Hanno Becker22bf1452019-04-05 11:21:08 +01001008 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001009 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1010 else
1011 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1012
1013 status = psa_key_derivation( &generator, psk, alg,
1014 salt, salt_len,
1015 (unsigned char const *) lbl,
1016 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001017 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001018 if( status != PSA_SUCCESS )
1019 {
1020 psa_generator_abort( &generator );
1021 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1022 }
1023
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001024 status = psa_generator_read( &generator, session->master,
1025 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001026 if( status != PSA_SUCCESS )
1027 {
1028 psa_generator_abort( &generator );
1029 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1030 }
1031
1032 status = psa_generator_abort( &generator );
1033 if( status != PSA_SUCCESS )
1034 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001035 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001036 else
1037#endif
1038 {
1039 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1040 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001041 session->master,
1042 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001043 if( ret != 0 )
1044 {
1045 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1046 return( ret );
1047 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001048
Hanno Becker7d0a5692018-10-23 15:26:22 +01001049 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1050 handshake->premaster,
1051 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001052
Hanno Becker7d0a5692018-10-23 15:26:22 +01001053 mbedtls_platform_zeroize( handshake->premaster,
1054 sizeof(handshake->premaster) );
1055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 /*
1059 * Swap the client and server random values.
1060 */
Paul Bakker48916f92012-09-16 19:57:18 +00001061 memcpy( tmp, handshake->randbytes, 64 );
1062 memcpy( handshake->randbytes, tmp + 32, 32 );
1063 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001064 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
1066 /*
1067 * SSLv3:
1068 * key block =
1069 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1070 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1071 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1072 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1073 * ...
1074 *
1075 * TLSv1:
1076 * key block = PRF( master, "key expansion", randbytes )
1077 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001078 ret = handshake->tls_prf( session->master, 48, "key expansion",
1079 handshake->randbytes, 64, keyblk, 256 );
1080 if( ret != 0 )
1081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001083 return( ret );
1084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1087 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1088 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1089 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1090 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 /*
1093 * Determine the appropriate key, IV and MAC length.
1094 */
Paul Bakker68884e32013-01-07 18:20:04 +01001095
Hanno Becker88aaf652017-12-27 08:17:40 +00001096 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001097
Hanno Becker8031d062018-01-03 15:32:31 +00001098#if defined(MBEDTLS_GCM_C) || \
1099 defined(MBEDTLS_CCM_C) || \
1100 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102 cipher_info->mode == MBEDTLS_MODE_CCM ||
1103 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001105 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001106
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001107 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001108 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001109 transform->taglen =
1110 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001111
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001112 /* All modes haves 96-bit IVs;
1113 * GCM and CCM has 4 implicit and 8 explicit bytes
1114 * ChachaPoly has all 12 bytes implicit
1115 */
Paul Bakker68884e32013-01-07 18:20:04 +01001116 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001117 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1118 transform->fixed_ivlen = 12;
1119 else
1120 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001121
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001122 /* Minimum length of encrypted record */
1123 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001124 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001125 }
1126 else
Hanno Becker8031d062018-01-03 15:32:31 +00001127#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1128#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1129 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1130 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001131 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001132 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1134 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001137 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001140 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001141 mac_key_len = mbedtls_md_get_size( md_info );
1142 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001145 /*
1146 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1147 * (rfc 6066 page 13 or rfc 2104 section 4),
1148 * so we only need to adjust the length here.
1149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001153
1154#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1155 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001156 * HMAC implementation which also truncates the key
1157 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001158 mac_key_len = transform->maclen;
1159#endif
1160 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001162
1163 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001164 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001166 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001168 transform->minlen = transform->maclen;
1169 else
Paul Bakker68884e32013-01-07 18:20:04 +01001170 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001171 /*
1172 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001173 * 1. if EtM is in use: one block plus MAC
1174 * otherwise: * first multiple of blocklen greater than maclen
1175 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1178 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001179 {
1180 transform->minlen = transform->maclen
1181 + cipher_info->block_size;
1182 }
1183 else
1184#endif
1185 {
1186 transform->minlen = transform->maclen
1187 + cipher_info->block_size
1188 - transform->maclen % cipher_info->block_size;
1189 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1192 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1193 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001195 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1198 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1199 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001200 {
1201 transform->minlen += transform->ivlen;
1202 }
1203 else
1204#endif
1205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001207 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1208 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001209 }
Paul Bakker68884e32013-01-07 18:20:04 +01001210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 }
Hanno Becker8031d062018-01-03 15:32:31 +00001212 else
1213#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1214 {
1215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Hanno Becker88aaf652017-12-27 08:17:40 +00001219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1220 (unsigned) keylen,
1221 (unsigned) transform->minlen,
1222 (unsigned) transform->ivlen,
1223 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224
1225 /*
1226 * Finally setup the cipher contexts, IVs and MAC secrets.
1227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001229 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001232 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Paul Bakker68884e32013-01-07 18:20:04 +01001234 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001235 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 /*
1238 * This is not used in TLS v1.1.
1239 */
Paul Bakker48916f92012-09-16 19:57:18 +00001240 iv_copy_len = ( transform->fixed_ivlen ) ?
1241 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001242 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1243 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001244 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 }
1246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#endif /* MBEDTLS_SSL_CLI_C */
1248#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001249 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001251 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001252 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Hanno Becker81c7b182017-11-09 18:39:33 +00001254 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001255 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 /*
1258 * This is not used in TLS v1.1.
1259 */
Paul Bakker48916f92012-09-16 19:57:18 +00001260 iv_copy_len = ( transform->fixed_ivlen ) ?
1261 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001262 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1263 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001264 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001270 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1271 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Hanno Beckerd56ed242018-01-03 15:32:51 +00001274#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_PROTO_SSL3)
1276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001277 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001278 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001281 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1282 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001283 }
1284
Hanno Becker81c7b182017-11-09 18:39:33 +00001285 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1286 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 }
1288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1290#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1291 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1292 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001293 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001294 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1295 For AEAD-based ciphersuites, there is nothing to do here. */
1296 if( mac_key_len != 0 )
1297 {
1298 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1299 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1300 }
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001302 else
1303#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001306 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1307 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001308 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001309#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1312 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001313 {
1314 int ret = 0;
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001317
Hanno Becker88aaf652017-12-27 08:17:40 +00001318 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001319 transform->iv_enc, transform->iv_dec,
1320 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001321 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001322 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001325 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1326 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001327 }
1328 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001329#else
1330 ((void) mac_dec);
1331 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001333
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001334#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1335 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001336 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001337 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1338 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001339 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001340 iv_copy_len );
1341 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001342
1343 if( ssl->conf->f_export_keys_ext != NULL )
1344 {
1345 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1346 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001347 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001348 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001349 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001350 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001351 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001352 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001353#endif
1354
Hanno Beckerf704bef2018-11-16 15:21:18 +00001355#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001356
1357 /* Only use PSA-based ciphers for TLS-1.2.
1358 * That's relevant at least for TLS-1.0, where
1359 * we assume that mbedtls_cipher_crypt() updates
1360 * the structure field for the IV, which the PSA-based
1361 * implementation currently doesn't. */
1362#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001364 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001365 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001366 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1368 {
1369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001370 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001371 }
1372
1373 if( ret == 0 )
1374 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001376 psa_fallthrough = 0;
1377 }
1378 else
1379 {
1380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1381 psa_fallthrough = 1;
1382 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001383 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001384 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 psa_fallthrough = 1;
1386#else
1387 psa_fallthrough = 1;
1388#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001389
Hanno Beckercb1cc802018-11-17 22:27:38 +00001390 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001391#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001392 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001393 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001396 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001397 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001398
Hanno Beckerf704bef2018-11-16 15:21:18 +00001399#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001400 /* Only use PSA-based ciphers for TLS-1.2.
1401 * That's relevant at least for TLS-1.0, where
1402 * we assume that mbedtls_cipher_crypt() updates
1403 * the structure field for the IV, which the PSA-based
1404 * implementation currently doesn't. */
1405#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1406 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001407 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001408 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001409 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1411 {
1412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001413 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001414 }
1415
1416 if( ret == 0 )
1417 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001419 psa_fallthrough = 0;
1420 }
1421 else
1422 {
1423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1424 psa_fallthrough = 1;
1425 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001426 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001427 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 psa_fallthrough = 1;
1429#else
1430 psa_fallthrough = 1;
1431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001432
Hanno Beckercb1cc802018-11-17 22:27:38 +00001433 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001434#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001435 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 cipher_info ) ) != 0 )
1437 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001439 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001440 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001443 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001447 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001448 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001451 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001455 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001456 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_CIPHER_MODE_CBC)
1459 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1462 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001465 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001466 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1469 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001472 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001479 // Initialize compression
1480 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001482 {
Paul Bakker16770332013-10-11 09:59:44 +02001483 if( ssl->compress_buf == NULL )
1484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001486 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001487 if( ssl->compress_buf == NULL )
1488 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001490 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001491 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1492 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001493 }
1494 }
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001497
Paul Bakker48916f92012-09-16 19:57:18 +00001498 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1499 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001501 if( deflateInit( &transform->ctx_deflate,
1502 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001503 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001506 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1507 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001508 }
1509 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001513end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001514 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1515 mbedtls_platform_zeroize( handshake->randbytes,
1516 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001517 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518}
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_PROTO_SSL3)
1521void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001523 mbedtls_md5_context md5;
1524 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 unsigned char pad_1[48];
1526 unsigned char pad_2[48];
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001530 mbedtls_md5_init( &md5 );
1531 mbedtls_sha1_init( &sha1 );
1532
1533 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1534 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
Paul Bakker380da532012-04-18 16:10:25 +00001536 memset( pad_1, 0x36, 48 );
1537 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001539 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1540 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1541 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001543 mbedtls_md5_starts_ret( &md5 );
1544 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1545 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1546 mbedtls_md5_update_ret( &md5, hash, 16 );
1547 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001549 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1550 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1551 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001552
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001553 mbedtls_sha1_starts_ret( &sha1 );
1554 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1555 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1556 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1557 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001561
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001562 mbedtls_md5_free( &md5 );
1563 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001564
Paul Bakker380da532012-04-18 16:10:25 +00001565 return;
1566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1570void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001571{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001572 mbedtls_md5_context md5;
1573 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001576
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001577 mbedtls_md5_init( &md5 );
1578 mbedtls_sha1_init( &sha1 );
1579
1580 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1581 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001582
Andrzej Kurekeb342242019-01-29 09:14:33 -05001583 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001584 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001588
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001589 mbedtls_md5_free( &md5 );
1590 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001591
Paul Bakker380da532012-04-18 16:10:25 +00001592 return;
1593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1597#if defined(MBEDTLS_SHA256_C)
1598void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001599{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001600#if defined(MBEDTLS_USE_PSA_CRYPTO)
1601 size_t hash_size;
1602 psa_status_t status;
1603 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1604
1605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1606 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1607 if( status != PSA_SUCCESS )
1608 {
1609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1610 return;
1611 }
1612
1613 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1614 if( status != PSA_SUCCESS )
1615 {
1616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1617 return;
1618 }
1619 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1621#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001622 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001623
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001624 mbedtls_sha256_init( &sha256 );
1625
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001627
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001628 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001629 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001633
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001634 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001635#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001636 return;
1637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SHA512_C)
1641void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001642{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001643#if defined(MBEDTLS_USE_PSA_CRYPTO)
1644 size_t hash_size;
1645 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001646 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001647
1648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001649 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001650 if( status != PSA_SUCCESS )
1651 {
1652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1653 return;
1654 }
1655
Andrzej Kurek972fba52019-01-30 03:29:12 -05001656 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001657 if( status != PSA_SUCCESS )
1658 {
1659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1660 return;
1661 }
1662 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1664#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001665 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001667 mbedtls_sha512_init( &sha512 );
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001670
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001671 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001672 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001677 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001678#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 return;
1680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_SHA512_C */
1682#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1685int 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 +02001686{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001687 unsigned char *p = ssl->handshake->premaster;
1688 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001689 const unsigned char *psk = ssl->conf->psk;
1690 size_t psk_len = ssl->conf->psk_len;
1691
1692 /* If the psk callback was called, use its result */
1693 if( ssl->handshake->psk != NULL )
1694 {
1695 psk = ssl->handshake->psk;
1696 psk_len = ssl->handshake->psk_len;
1697 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001698
1699 /*
1700 * PMS = struct {
1701 * opaque other_secret<0..2^16-1>;
1702 * opaque psk<0..2^16-1>;
1703 * };
1704 * with "other_secret" depending on the particular key exchange
1705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1707 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001708 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001709 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001711
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001712 *(p++) = (unsigned char)( psk_len >> 8 );
1713 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001714
1715 if( end < p || (size_t)( end - p ) < psk_len )
1716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1717
1718 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001719 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001720 }
1721 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1723#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1724 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001725 {
1726 /*
1727 * other_secret already set by the ClientKeyExchange message,
1728 * and is 48 bytes long
1729 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001730 if( end - p < 2 )
1731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1732
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001733 *p++ = 0;
1734 *p++ = 48;
1735 p += 48;
1736 }
1737 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1739#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1740 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001741 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001742 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001743 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001744
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001745 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001747 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001748 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001751 return( ret );
1752 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001753 *(p++) = (unsigned char)( len >> 8 );
1754 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001755 p += len;
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001758 }
1759 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1761#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1762 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001763 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001764 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001765 size_t zlen;
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001768 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001769 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001772 return( ret );
1773 }
1774
1775 *(p++) = (unsigned char)( zlen >> 8 );
1776 *(p++) = (unsigned char)( zlen );
1777 p += zlen;
1778
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001779 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1780 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001781 }
1782 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001787 }
1788
1789 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001790 if( end - p < 2 )
1791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001792
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001793 *(p++) = (unsigned char)( psk_len >> 8 );
1794 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001795
1796 if( end < p || (size_t)( end - p ) < psk_len )
1797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1798
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001799 memcpy( p, psk, psk_len );
1800 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001801
1802 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1803
1804 return( 0 );
1805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001809/*
1810 * SSLv3.0 MAC functions
1811 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001812#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001813static void ssl_mac( mbedtls_md_context_t *md_ctx,
1814 const unsigned char *secret,
1815 const unsigned char *buf, size_t len,
1816 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001817 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818{
1819 unsigned char header[11];
1820 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001821 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1823 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001824
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001825 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001827 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001828 else
Paul Bakker68884e32013-01-07 18:20:04 +01001829 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
1831 memcpy( header, ctr, 8 );
1832 header[ 8] = (unsigned char) type;
1833 header[ 9] = (unsigned char)( len >> 8 );
1834 header[10] = (unsigned char)( len );
1835
Paul Bakker68884e32013-01-07 18:20:04 +01001836 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_md_starts( md_ctx );
1838 mbedtls_md_update( md_ctx, secret, md_size );
1839 mbedtls_md_update( md_ctx, padding, padlen );
1840 mbedtls_md_update( md_ctx, header, 11 );
1841 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001842 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Paul Bakker68884e32013-01-07 18:20:04 +01001844 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 mbedtls_md_starts( md_ctx );
1846 mbedtls_md_update( md_ctx, secret, md_size );
1847 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001848 mbedtls_md_update( md_ctx, out, md_size );
1849 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001852
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001853/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001854 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001855#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001856 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1857 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1858 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1859/* This function makes sure every byte in the memory region is accessed
1860 * (in ascending addresses order) */
1861static void ssl_read_memory( unsigned char *p, size_t len )
1862{
1863 unsigned char acc = 0;
1864 volatile unsigned char force;
1865
1866 for( ; len != 0; p++, len-- )
1867 acc ^= *p;
1868
1869 force = acc;
1870 (void) force;
1871}
1872#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1873
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001874/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001876 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001877
1878static void ssl_extract_add_data_from_record( unsigned char* add_data,
1879 mbedtls_record *rec )
1880{
1881 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1882 add_data[8] = rec->type;
1883 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1884 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1885 add_data[12] = rec->data_len & 0xFF;
1886}
1887
Hanno Beckera18d1322018-01-03 14:27:32 +00001888int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1889 mbedtls_ssl_transform *transform,
1890 mbedtls_record *rec,
1891 int (*f_rng)(void *, unsigned char *, size_t),
1892 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001895 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001896 unsigned char * data;
1897 unsigned char add_data[13];
1898 size_t post_avail;
1899
1900 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001901#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001902 ((void) ssl);
1903#endif
1904
1905 /* The PRNG is used for dynamic IV generation that's used
1906 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1907#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1908 ( defined(MBEDTLS_AES_C) || \
1909 defined(MBEDTLS_ARIA_C) || \
1910 defined(MBEDTLS_CAMELLIA_C) ) && \
1911 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1912 ((void) f_rng);
1913 ((void) p_rng);
1914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001918 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001919 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1922 }
1923 if( rec == NULL ||
1924 rec->buf == NULL ||
1925 rec->buf_len < rec->data_offset ||
1926 rec->buf_len - rec->data_offset < rec->data_len )
1927 {
1928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930 }
1931
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001932 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1933 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001935 data, rec->data_len );
1936
1937 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1938
1939 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1940 {
1941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1942 (unsigned) rec->data_len,
1943 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1945 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001946
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001948 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 */
Hanno Becker52344c22018-01-03 15:24:20 +00001950#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 if( mode == MBEDTLS_MODE_STREAM ||
1952 ( mode == MBEDTLS_MODE_CBC
1953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001954 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001955#endif
1956 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001958 if( post_avail < transform->maclen )
1959 {
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1961 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1962 }
1963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001965 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001967 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001968 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1969 data, rec->data_len, rec->ctr, rec->type, mac );
1970 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001971 }
1972 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1975 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001976 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001977 {
Hanno Becker992b6872017-11-09 18:57:39 +00001978 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1979
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001980 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001981
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001982 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1983 sizeof( add_data ) );
1984 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1985 data, rec->data_len );
1986 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1987 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1988
1989 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001990 }
1991 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001992#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996 }
1997
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001998 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1999 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002000
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002001 rec->data_len += transform->maclen;
2002 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002003 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002004 }
Hanno Becker52344c22018-01-03 15:24:20 +00002005#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002007 /*
2008 * Encrypt
2009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2011 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002013 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002014 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002016 "including %d bytes of padding",
2017 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002019 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2020 transform->iv_enc, transform->ivlen,
2021 data, rec->data_len,
2022 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002025 return( ret );
2026 }
2027
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002028 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 }
Paul Bakker68884e32013-01-07 18:20:04 +01002034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002036
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037#if defined(MBEDTLS_GCM_C) || \
2038 defined(MBEDTLS_CCM_C) || \
2039 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041 mode == MBEDTLS_MODE_CCM ||
2042 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002043 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002044 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002045 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002046 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002047
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002048 /* Check that there's space for both the authentication tag
2049 * and the explicit IV before and after the record content. */
2050 if( post_avail < transform->taglen ||
2051 rec->data_offset < explicit_iv_len )
2052 {
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2054 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2055 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002056
Paul Bakker68884e32013-01-07 18:20:04 +01002057 /*
2058 * Generate IV
2059 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002060 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2061 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002062 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002064 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2065 explicit_iv_len );
2066 /* Prefix record content with explicit IV. */
2067 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 }
2069 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2070 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002071 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 unsigned char i;
2073
2074 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2075
2076 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002077 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002078 }
2079 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002080 {
2081 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002084 }
2085
Hanno Becker1f10d762019-04-26 13:34:37 +01002086 ssl_extract_add_data_from_record( add_data, rec );
2087
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002088 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2089 iv, transform->ivlen );
2090 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002091 data - explicit_iv_len, explicit_iv_len );
2092 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2093 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002095 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002096 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002097
Paul Bakker68884e32013-01-07 18:20:04 +01002098 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002099 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002100 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002101
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002102 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002103 iv, transform->ivlen,
2104 add_data, 13, /* add data */
2105 data, rec->data_len, /* source */
2106 data, &rec->data_len, /* destination */
2107 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110 return( ret );
2111 }
2112
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002113 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2114 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002115
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002116 rec->data_len += transform->taglen + explicit_iv_len;
2117 rec->data_offset -= explicit_iv_len;
2118 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002119 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2123#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002124 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002127 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 size_t padlen, i;
2129 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002130
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002131 /* Currently we're always using minimal padding
2132 * (up to 255 bytes would be allowed). */
2133 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2134 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 padlen = 0;
2136
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002137 /* Check there's enough space in the buffer for the padding. */
2138 if( post_avail < padlen + 1 )
2139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2142 }
2143
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002145 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002147 rec->data_len += padlen + 1;
2148 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002152 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2153 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002157 if( f_rng == NULL )
2158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2161 }
2162
2163 if( rec->data_offset < transform->ivlen )
2164 {
2165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2166 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2167 }
2168
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002169 /*
2170 * Generate IV
2171 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002172 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002173 if( ret != 0 )
2174 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 memcpy( data - transform->ivlen, transform->iv_enc,
2177 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002184 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002185 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002187 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2188 transform->iv_enc,
2189 transform->ivlen,
2190 data, rec->data_len,
2191 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002194 return( ret );
2195 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002196
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002197 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002201 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002204 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002205 {
2206 /*
2207 * Save IV in SSL3 and TLS1
2208 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2210 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002213#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214 {
2215 data -= transform->ivlen;
2216 rec->data_offset -= transform->ivlen;
2217 rec->data_len += transform->ivlen;
2218 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002221 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002222 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002223 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2224
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 /*
2226 * MAC(MAC_write_key, seq_num +
2227 * TLSCipherText.type +
2228 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002229 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002230 * IV + // except for TLS 1.0
2231 * ENC(content + padding + padding_length));
2232 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233
2234 if( post_avail < transform->maclen)
2235 {
2236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2237 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2238 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002239
Hanno Becker1f10d762019-04-26 13:34:37 +01002240 ssl_extract_add_data_from_record( add_data, rec );
2241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2244 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002245
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2247 sizeof( add_data ) );
2248 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2249 data, rec->data_len );
2250 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2251 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002252
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002255 rec->data_len += transform->maclen;
2256 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002257 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002263 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 /* Make extra sure authentication was performed, exactly once */
2270 if( auth_done != 1 )
2271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002274 }
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
2278 return( 0 );
2279}
2280
Hanno Beckera18d1322018-01-03 14:27:32 +00002281int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2282 mbedtls_ssl_transform *transform,
2283 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002285 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002287 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002288#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002289 size_t padlen = 0, correct = 1;
2290#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002291 unsigned char* data;
2292 unsigned char add_data[13];
2293
Hanno Beckera18d1322018-01-03 14:27:32 +00002294#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002295 ((void) ssl);
2296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002299 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002300 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2303 }
2304 if( rec == NULL ||
2305 rec->buf == NULL ||
2306 rec->buf_len < rec->data_offset ||
2307 rec->buf_len - rec->data_offset < rec->data_len )
2308 {
2309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 }
2312
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002313 data = rec->buf + rec->data_offset;
2314 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2317 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002318 {
2319 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002320 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2321 transform->iv_dec,
2322 transform->ivlen,
2323 data, rec->data_len,
2324 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002327 return( ret );
2328 }
2329
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002330 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 }
Paul Bakker68884e32013-01-07 18:20:04 +01002336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338#if defined(MBEDTLS_GCM_C) || \
2339 defined(MBEDTLS_CCM_C) || \
2340 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002342 mode == MBEDTLS_MODE_CCM ||
2343 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002344 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002345 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002347
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002348 /*
2349 * Compute and update sizes
2350 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002351 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002354 "+ taglen (%d)", rec->data_len,
2355 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002357 }
Paul Bakker68884e32013-01-07 18:20:04 +01002358
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 /*
2360 * Prepare IV
2361 */
2362 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2363 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002364 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002366 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002367
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002368 }
2369 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2370 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002371 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002372 unsigned char i;
2373
2374 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2375
2376 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002377 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002378 }
2379 else
2380 {
2381 /* Reminder if we ever add an AEAD mode with a different size */
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2383 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2384 }
2385
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002386 data += explicit_iv_len;
2387 rec->data_offset += explicit_iv_len;
2388 rec->data_len -= explicit_iv_len + transform->taglen;
2389
2390 ssl_extract_add_data_from_record( add_data, rec );
2391 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2392 add_data, 13 );
2393
2394 memcpy( transform->iv_dec + transform->fixed_ivlen,
2395 data - explicit_iv_len, explicit_iv_len );
2396
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002397 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002398 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002399 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002400
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002401
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002402 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002403 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002404 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002405 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2406 iv, transform->ivlen,
2407 add_data, 13,
2408 data, rec->data_len,
2409 data, &olen,
2410 data + rec->data_len,
2411 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2416 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002417
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002418 return( ret );
2419 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002420 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002421
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002422 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002426 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2430#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002431 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002434 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002435
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 /*
Paul Bakker45829992013-01-03 14:52:21 +01002437 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002440 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2441 {
2442 /* The ciphertext is prefixed with the CBC IV. */
2443 minlen += transform->ivlen;
2444 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002445#endif
Paul Bakker45829992013-01-03 14:52:21 +01002446
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002447 /* Size considerations:
2448 *
2449 * - The CBC cipher text must not be empty and hence
2450 * at least of size transform->ivlen.
2451 *
2452 * Together with the potential IV-prefix, this explains
2453 * the first of the two checks below.
2454 *
2455 * - The record must contain a MAC, either in plain or
2456 * encrypted, depending on whether Encrypt-then-MAC
2457 * is used or not.
2458 * - If it is, the message contains the IV-prefix,
2459 * the CBC ciphertext, and the MAC.
2460 * - If it is not, the padded plaintext, and hence
2461 * the CBC ciphertext, has at least length maclen + 1
2462 * because there is at least the padding length byte.
2463 *
2464 * As the CBC ciphertext is not empty, both cases give the
2465 * lower bound minlen + maclen + 1 on the record size, which
2466 * we test for in the second check below.
2467 */
2468 if( rec->data_len < minlen + transform->ivlen ||
2469 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002472 "+ 1 ) ( + expl IV )", rec->data_len,
2473 transform->ivlen,
2474 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002476 }
2477
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478 /*
2479 * Authenticate before decrypt if enabled
2480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002482 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483 {
Hanno Becker992b6872017-11-09 18:57:39 +00002484 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002487
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002488 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2489 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002490
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002491 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002492
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002493 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2494 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2495 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2496 data, rec->data_len );
2497 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2498 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002499
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002500 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2501 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002502 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002503 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002505 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2506 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002510 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002511 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
2515 /*
2516 * Check length sanity
2517 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002518 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002521 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 }
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002526 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002527 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002528 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002529 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002531 /* This is safe because data_len >= minlen + maclen + 1 initially,
2532 * and at this point we have at most subtracted maclen (note that
2533 * minlen == transform->ivlen here). */
2534 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002535
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 data += transform->ivlen;
2537 rec->data_offset += transform->ivlen;
2538 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002541
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2543 transform->iv_dec, transform->ivlen,
2544 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002547 return( ret );
2548 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002549
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002554 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002558 {
2559 /*
2560 * Save IV in SSL3 and TLS1
2561 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2563 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002565#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002567 /* Safe since data_len >= minlen + maclen + 1, so after having
2568 * subtracted at most minlen and maclen up to this point,
2569 * data_len > 0. */
2570 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002571
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002572 if( auth_done == 1 )
2573 {
2574 correct *= ( rec->data_len >= padlen + 1 );
2575 padlen *= ( rec->data_len >= padlen + 1 );
2576 }
2577 else
Paul Bakker45829992013-01-03 14:52:21 +01002578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002580 if( rec->data_len < transform->maclen + padlen + 1 )
2581 {
2582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2583 rec->data_len,
2584 transform->maclen,
2585 padlen + 1 ) );
2586 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002587#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002588
2589 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2590 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 padlen++;
2594
2595 /* Regardless of the validity of the padding,
2596 * we have data_len >= padlen here. */
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002599 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002601 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SSL_DEBUG_ALL)
2604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002605 "should be no more than %d",
2606 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002607#endif
Paul Bakker45829992013-01-03 14:52:21 +01002608 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610 }
2611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2613#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2614 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 /* The padding check involves a series of up to 256
2618 * consecutive memory reads at the end of the record
2619 * plaintext buffer. In order to hide the length and
2620 * validity of the padding, always perform exactly
2621 * `min(256,plaintext_len)` reads (but take into account
2622 * only the last `padlen` bytes for the padding check). */
2623 size_t pad_count = 0;
2624 size_t real_count = 0;
2625 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002626
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 /* Index of first padding byte; it has been ensured above
2628 * that the subtraction is safe. */
2629 size_t const padding_idx = rec->data_len - padlen;
2630 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2631 size_t const start_idx = rec->data_len - num_checks;
2632 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002633
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002635 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002636 real_count |= ( idx >= padding_idx );
2637 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002638 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002642 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002644#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002645 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002647 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2649 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002653 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002654
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655 /* If the padding was found to be invalid, padlen == 0
2656 * and the subtraction is safe. If the padding was found valid,
2657 * padlen hasn't been changed and the previous assertion
2658 * data_len >= padlen still holds. */
2659 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002661 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002663 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2666 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002669#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
2674 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002675 * Authenticate if not done yet.
2676 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 */
Hanno Becker52344c22018-01-03 15:24:20 +00002678#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002679 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002680 {
Hanno Becker992b6872017-11-09 18:57:39 +00002681 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 /* If the initial value of padlen was such that
2684 * data_len < maclen + padlen + 1, then padlen
2685 * got reset to 1, and the initial check
2686 * data_len >= minlen + maclen + 1
2687 * guarantees that at this point we still
2688 * have at least data_len >= maclen.
2689 *
2690 * If the initial value of padlen was such that
2691 * data_len >= maclen + padlen + 1, then we have
2692 * subtracted either padlen + 1 (if the padding was correct)
2693 * or 0 (if the padding was incorrect) since then,
2694 * hence data_len >= maclen in any case.
2695 */
2696 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002698 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002702 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 ssl_mac( &transform->md_ctx_dec,
2704 transform->mac_dec,
2705 data, rec->data_len,
2706 rec->ctr, rec->type,
2707 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 }
2709 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2711#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2712 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002714 {
2715 /*
2716 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002717 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 *
2719 * Known timing attacks:
2720 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2721 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002722 * To compensate for different timings for the MAC calculation
2723 * depending on how much padding was removed (which is determined
2724 * by padlen), process extra_run more blocks through the hash
2725 * function.
2726 *
2727 * The formula in the paper is
2728 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2729 * where L1 is the size of the header plus the decrypted message
2730 * plus CBC padding and L2 is the size of the header plus the
2731 * decrypted message. This is for an underlying hash function
2732 * with 64-byte blocks.
2733 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2734 * correctly. We round down instead of up, so -56 is the correct
2735 * value for our calculations instead of -55.
2736 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002737 * Repeat the formula rather than defining a block_size variable.
2738 * This avoids requiring division by a variable at runtime
2739 * (which would be marginally less efficient and would require
2740 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002741 */
2742 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002744
2745 /*
2746 * The next two sizes are the minimum and maximum values of
2747 * in_msglen over all padlen values.
2748 *
2749 * They're independent of padlen, since we previously did
2750 * in_msglen -= padlen.
2751 *
2752 * Note that max_len + maclen is never more than the buffer
2753 * length, as we previously did in_msglen -= maclen too.
2754 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002756 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2757
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002758 memset( tmp, 0, sizeof( tmp ) );
2759
2760 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002762#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2763 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002764 case MBEDTLS_MD_MD5:
2765 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002767 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2769 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 break;
2771#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002772#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002773 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2776 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 break;
2778#endif
2779 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2782 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002783
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002784 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002785
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2787 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2788 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002789 /* Make sure we access everything even when padlen > 0. This
2790 * makes the synchronisation requirements for just-in-time
2791 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002792 ssl_read_memory( data + rec->data_len, padlen );
2793 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002794
2795 /* Call mbedtls_md_process at least once due to cache attacks
2796 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002797 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002798 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002799
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002801
2802 /* Make sure we access all the memory that could contain the MAC,
2803 * before we check it in the next code block. This makes the
2804 * synchronisation requirements for just-in-time Prime+Probe
2805 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 ssl_read_memory( data + min_len,
2807 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002808 }
2809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2811 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002817#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2819 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002822 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2823 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_DEBUG_ALL)
2826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002827#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 correct = 0;
2829 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002830 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002831 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002832
2833 /*
2834 * Finally check the correct flag
2835 */
2836 if( correct == 0 )
2837 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002838#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002839
2840 /* Make extra sure authentication was performed, exactly once */
2841 if( auth_done != 1 )
2842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
2849 return( 0 );
2850}
2851
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002852#undef MAC_NONE
2853#undef MAC_PLAINTEXT
2854#undef MAC_CIPHERTEXT
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002857/*
2858 * Compression/decompression functions
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002861{
2862 int ret;
2863 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002864 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002865 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002866 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002870 if( len_pre == 0 )
2871 return( 0 );
2872
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 memcpy( msg_pre, ssl->out_msg, len_pre );
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876 ssl->out_msglen ) );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002879 ssl->out_msg, ssl->out_msglen );
2880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2882 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2883 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002884 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002885
Paul Bakker48916f92012-09-16 19:57:18 +00002886 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887 if( ret != Z_OK )
2888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2890 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891 }
2892
Angus Grattond8213d02016-05-25 20:56:48 +10002893 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002894 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 ssl->out_msglen ) );
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msg, ssl->out_msglen );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903
2904 return( 0 );
2905}
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908{
2909 int ret;
2910 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002911 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002912 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002913 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002917 if( len_pre == 0 )
2918 return( 0 );
2919
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 memcpy( msg_pre, ssl->in_msg, len_pre );
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 ssl->in_msglen ) );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->in_msg, ssl->in_msglen );
2927
Paul Bakker48916f92012-09-16 19:57:18 +00002928 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2929 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2930 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002931 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002932 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933
Paul Bakker48916f92012-09-16 19:57:18 +00002934 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 if( ret != Z_OK )
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2938 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939 }
2940
Angus Grattond8213d02016-05-25 20:56:48 +10002941 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002942 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945 ssl->in_msglen ) );
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->in_msg, ssl->in_msglen );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
2952 return( 0 );
2953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2957static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_PROTO_DTLS)
2960static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002961{
2962 /* If renegotiation is not enforced, retransmit until we would reach max
2963 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002964 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002965 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002966 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002967 unsigned char doublings = 1;
2968
2969 while( ratio != 0 )
2970 {
2971 ++doublings;
2972 ratio >>= 1;
2973 }
2974
2975 if( ++ssl->renego_records_seen > doublings )
2976 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002978 return( 0 );
2979 }
2980 }
2981
2982 return( ssl_write_hello_request( ssl ) );
2983}
2984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002986
Paul Bakker5121ce52009-01-03 21:22:43 +00002987/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002988 * Fill the input message buffer by appending data to it.
2989 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002990 *
2991 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2992 * available (from this read and/or a previous one). Otherwise, an error code
2993 * is returned (possibly EOF or WANT_READ).
2994 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002995 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2996 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2997 * since we always read a whole datagram at once.
2998 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002999 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003000 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003{
Paul Bakker23986e52011-04-24 08:57:21 +00003004 int ret;
3005 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003009 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003012 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003014 }
3015
Angus Grattond8213d02016-05-25 20:56:48 +10003016 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003020 }
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003025 uint32_t timeout;
3026
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003027 /* Just to be sure */
3028 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3029 {
3030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3031 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3033 }
3034
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003035 /*
3036 * The point is, we need to always read a full datagram at once, so we
3037 * sometimes read more then requested, and handle the additional data.
3038 * It could be the rest of the current record (while fetching the
3039 * header) and/or some other records in the same datagram.
3040 */
3041
3042 /*
3043 * Move to the next record in the already read datagram if applicable
3044 */
3045 if( ssl->next_record_offset != 0 )
3046 {
3047 if( ssl->in_left < ssl->next_record_offset )
3048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003051 }
3052
3053 ssl->in_left -= ssl->next_record_offset;
3054
3055 if( ssl->in_left != 0 )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 ssl->next_record_offset ) );
3059 memmove( ssl->in_hdr,
3060 ssl->in_hdr + ssl->next_record_offset,
3061 ssl->in_left );
3062 }
3063
3064 ssl->next_record_offset = 0;
3065 }
3066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003069
3070 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003071 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003072 */
3073 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003076 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003077 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003078
3079 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003080 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003081 * are not at the beginning of a new record, the caller did something
3082 * wrong.
3083 */
3084 if( ssl->in_left != 0 )
3085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003088 }
3089
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003090 /*
3091 * Don't even try to read if time's out already.
3092 * This avoids by-passing the timer when repeatedly receiving messages
3093 * that will end up being dropped.
3094 */
3095 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003096 {
3097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003098 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003099 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003100 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003101 {
Angus Grattond8213d02016-05-25 20:56:48 +10003102 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003105 timeout = ssl->handshake->retransmit_timeout;
3106 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003107 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003110
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003111 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003112 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3113 timeout );
3114 else
3115 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003118
3119 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003121 }
3122
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003123 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003129 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003130 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003133 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003134 }
3135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003139 return( ret );
3140 }
3141
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003142 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003145 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003147 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003148 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003151 return( ret );
3152 }
3153
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003154 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003155 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003157 }
3158
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 if( ret < 0 )
3160 return( ret );
3161
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003162 ssl->in_left = ret;
3163 }
3164 else
3165#endif
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003168 ssl->in_left, nb_want ) );
3169
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 while( ssl->in_left < nb_want )
3171 {
3172 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003173
3174 if( ssl_check_timer( ssl ) != 0 )
3175 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3176 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003177 {
3178 if( ssl->f_recv_timeout != NULL )
3179 {
3180 ret = ssl->f_recv_timeout( ssl->p_bio,
3181 ssl->in_hdr + ssl->in_left, len,
3182 ssl->conf->read_timeout );
3183 }
3184 else
3185 {
3186 ret = ssl->f_recv( ssl->p_bio,
3187 ssl->in_hdr + ssl->in_left, len );
3188 }
3189 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003192 ssl->in_left, nb_want ) );
3193 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194
3195 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003197
3198 if( ret < 0 )
3199 return( ret );
3200
mohammad160352aecb92018-03-28 23:41:40 -07003201 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003202 {
Darryl Green11999bb2018-03-13 15:22:58 +00003203 MBEDTLS_SSL_DEBUG_MSG( 1,
3204 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003205 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3207 }
3208
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003209 ssl->in_left += ret;
3210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003214
3215 return( 0 );
3216}
3217
3218/*
3219 * Flush any data not yet written
3220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003222{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003223 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003224 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003228 if( ssl->f_send == NULL )
3229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003231 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003233 }
3234
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003235 /* Avoid incrementing counter if data is flushed */
3236 if( ssl->out_left == 0 )
3237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003239 return( 0 );
3240 }
3241
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 while( ssl->out_left > 0 )
3243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3245 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Hanno Becker2b1e3542018-08-06 11:19:13 +01003247 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003248 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
3252 if( ret <= 0 )
3253 return( ret );
3254
mohammad160352aecb92018-03-28 23:41:40 -07003255 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003256 {
Darryl Green11999bb2018-03-13 15:22:58 +00003257 MBEDTLS_SSL_DEBUG_MSG( 1,
3258 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003259 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3261 }
3262
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 ssl->out_left -= ret;
3264 }
3265
Hanno Becker2b1e3542018-08-06 11:19:13 +01003266#if defined(MBEDTLS_SSL_PROTO_DTLS)
3267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003268 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003269 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003270 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003271 else
3272#endif
3273 {
3274 ssl->out_hdr = ssl->out_buf + 8;
3275 }
3276 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
3280 return( 0 );
3281}
3282
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003283/*
3284 * Functions to handle the DTLS retransmission state machine
3285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003287/*
3288 * Append current handshake message to current outgoing flight
3289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3294 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3295 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003296
3297 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003298 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003302 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303 }
3304
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003305 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003306 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003309 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310 }
3311
3312 /* Copy current handshake message with headers */
3313 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3314 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003315 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316 msg->next = NULL;
3317
3318 /* Append to the current flight */
3319 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003320 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 else
3322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324 while( cur->next != NULL )
3325 cur = cur->next;
3326 cur->next = msg;
3327 }
3328
Hanno Becker3b235902018-08-06 09:54:53 +01003329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 return( 0 );
3331}
3332
3333/*
3334 * Free the current flight of handshake messages
3335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 mbedtls_ssl_flight_item *cur = flight;
3339 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340
3341 while( cur != NULL )
3342 {
3343 next = cur->next;
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 mbedtls_free( cur->p );
3346 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347
3348 cur = next;
3349 }
3350}
3351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3353static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003354#endif
3355
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003356/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003357 * Swap transform_out and out_ctr with the alternative ones
3358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003362 unsigned char tmp_out_ctr[8];
3363
3364 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003367 return;
3368 }
3369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003371
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003372 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373 tmp_transform = ssl->transform_out;
3374 ssl->transform_out = ssl->handshake->alt_transform_out;
3375 ssl->handshake->alt_transform_out = tmp_transform;
3376
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003377 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003378 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3379 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003380 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003381
3382 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003383 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3386 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3391 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392 }
3393 }
3394#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395}
3396
3397/*
3398 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003399 */
3400int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3401{
3402 int ret = 0;
3403
3404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3405
3406 ret = mbedtls_ssl_flight_transmit( ssl );
3407
3408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3409
3410 return( ret );
3411}
3412
3413/*
3414 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003415 *
3416 * Need to remember the current message in case flush_output returns
3417 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003418 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003419 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003420int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003421{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003422 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003426 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003428
3429 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003430 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 ssl_swap_epochs( ssl );
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003435
3436 while( ssl->handshake->cur_msg != NULL )
3437 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003438 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003439 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003440
Hanno Beckere1dcb032018-08-17 16:47:58 +01003441 int const is_finished =
3442 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3443 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3444
Hanno Becker04da1892018-08-14 13:22:10 +01003445 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3446 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3447
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003448 /* Swap epochs before sending Finished: we can't do it after
3449 * sending ChangeCipherSpec, in case write returns WANT_READ.
3450 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003451 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003452 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003454 ssl_swap_epochs( ssl );
3455 }
3456
Hanno Becker67bc7c32018-08-06 11:33:50 +01003457 ret = ssl_get_remaining_payload_in_datagram( ssl );
3458 if( ret < 0 )
3459 return( ret );
3460 max_frag_len = (size_t) ret;
3461
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003462 /* CCS is copied as is, while HS messages may need fragmentation */
3463 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 if( max_frag_len == 0 )
3466 {
3467 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3468 return( ret );
3469
3470 continue;
3471 }
3472
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003473 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003475 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003476
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003477 /* Update position inside current message */
3478 ssl->handshake->cur_msg_p += cur->len;
3479 }
3480 else
3481 {
3482 const unsigned char * const p = ssl->handshake->cur_msg_p;
3483 const size_t hs_len = cur->len - 12;
3484 const size_t frag_off = p - ( cur->p + 12 );
3485 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487
Hanno Beckere1dcb032018-08-17 16:47:58 +01003488 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003489 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003490 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003491 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003492
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3494 return( ret );
3495
3496 continue;
3497 }
3498 max_hs_frag_len = max_frag_len - 12;
3499
3500 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3501 max_hs_frag_len : rem_len;
3502
3503 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003504 {
3505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003506 (unsigned) cur_hs_frag_len,
3507 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003508 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003509
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003510 /* Messages are stored with handshake headers as if not fragmented,
3511 * copy beginning of headers then fill fragmentation fields.
3512 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3513 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003515 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3516 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3517 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3518
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3520 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3521 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003522
3523 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3524
Hanno Becker3f7b9732018-08-28 09:53:25 +01003525 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003526 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3527 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003528 ssl->out_msgtype = cur->type;
3529
3530 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003531 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003532 }
3533
3534 /* If done with the current message move to the next one if any */
3535 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3536 {
3537 if( cur->next != NULL )
3538 {
3539 ssl->handshake->cur_msg = cur->next;
3540 ssl->handshake->cur_msg_p = cur->next->p + 12;
3541 }
3542 else
3543 {
3544 ssl->handshake->cur_msg = NULL;
3545 ssl->handshake->cur_msg_p = NULL;
3546 }
3547 }
3548
3549 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003550 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003553 return( ret );
3554 }
3555 }
3556
Hanno Becker67bc7c32018-08-06 11:33:50 +01003557 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3558 return( ret );
3559
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003560 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3562 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003563 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003566 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3567 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003568
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570
3571 return( 0 );
3572}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003573
3574/*
3575 * To be called when the last message of an incoming flight is received.
3576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003578{
3579 /* We won't need to resend that one any more */
3580 ssl_flight_free( ssl->handshake->flight );
3581 ssl->handshake->flight = NULL;
3582 ssl->handshake->cur_msg = NULL;
3583
3584 /* The next incoming flight will start with this msg_seq */
3585 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3586
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003587 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003588 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003589
Hanno Becker0271f962018-08-16 13:23:47 +01003590 /* Clear future message buffering structure. */
3591 ssl_buffering_free( ssl );
3592
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003593 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003594 ssl_set_timer( ssl, 0 );
3595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3597 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600 }
3601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003604
3605/*
3606 * To be called when the last message of an outgoing flight is send.
3607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003609{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003610 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003611 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3614 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003617 }
3618 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003622
Paul Bakker5121ce52009-01-03 21:22:43 +00003623/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003624 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626
3627/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003628 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003629 *
3630 * - fill in handshake headers
3631 * - update handshake checksum
3632 * - DTLS: save message for resending
3633 * - then pass to the record layer
3634 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003635 * DTLS: except for HelloRequest, messages are only queued, and will only be
3636 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003637 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003638 * Inputs:
3639 * - ssl->out_msglen: 4 + actual handshake message len
3640 * (4 is the size of handshake headers for TLS)
3641 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3642 * - ssl->out_msg + 4: the handshake message body
3643 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003644 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003645 * - ssl->out_msglen: the length of the record contents
3646 * (including handshake headers but excluding record headers)
3647 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003648 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003649int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003651 int ret;
3652 const size_t hs_len = ssl->out_msglen - 4;
3653 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3656
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003657 /*
3658 * Sanity checks
3659 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003660 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003661 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3662 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003663 /* In SSLv3, the client might send a NoCertificate alert. */
3664#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3665 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3666 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3667 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3668#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3669 {
3670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3672 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003674
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003675 /* Whenever we send anything different from a
3676 * HelloRequest we should be in a handshake - double check. */
3677 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3678 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003679 ssl->handshake == NULL )
3680 {
3681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003686 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003687 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003689 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003693#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003694
Hanno Beckerb50a2532018-08-06 11:52:54 +01003695 /* Double-check that we did not exceed the bounds
3696 * of the outgoing record buffer.
3697 * This should never fail as the various message
3698 * writing functions must obey the bounds of the
3699 * outgoing record buffer, but better be safe.
3700 *
3701 * Note: We deliberately do not check for the MTU or MFL here.
3702 */
3703 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3704 {
3705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3706 "size %u, maximum %u",
3707 (unsigned) ssl->out_msglen,
3708 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3710 }
3711
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003712 /*
3713 * Fill handshake headers
3714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3718 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3719 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003721 /*
3722 * DTLS has additional fields in the Handshake layer,
3723 * between the length field and the actual payload:
3724 * uint16 message_seq;
3725 * uint24 fragment_offset;
3726 * uint24 fragment_length;
3727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003730 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003731 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003732 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003733 {
3734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3735 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003736 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003737 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3739 }
3740
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003741 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003742 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003743
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003744 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003746 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003747 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3748 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3749 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003750 }
3751 else
3752 {
3753 ssl->out_msg[4] = 0;
3754 ssl->out_msg[5] = 0;
3755 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003756
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003757 /* Handshake hashes are computed without fragmentation,
3758 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759 memset( ssl->out_msg + 6, 0x00, 3 );
3760 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003763
Hanno Becker0207e532018-08-28 10:28:28 +01003764 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003765 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3766 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003769 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003772 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3773 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003774 {
3775 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778 return( ret );
3779 }
3780 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003783 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003784 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003785 {
3786 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3787 return( ret );
3788 }
3789 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003790
3791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3792
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003793 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003794}
3795
3796/*
3797 * Record layer functions
3798 */
3799
3800/*
3801 * Write current record.
3802 *
3803 * Uses:
3804 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3805 * - ssl->out_msglen: length of the record content (excl headers)
3806 * - ssl->out_msg: record content
3807 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003809{
3810 int ret, done = 0;
3811 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003813
3814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003817 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003819 {
3820 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003823 return( ret );
3824 }
3825
3826 len = ssl->out_msglen;
3827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3831 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 ret = mbedtls_ssl_hw_record_write( ssl );
3836 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3839 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003840 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003841
3842 if( ret == 0 )
3843 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003846 if( !done )
3847 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003848 unsigned i;
3849 size_t protected_record_size;
3850
Paul Bakker05ef8352012-05-08 09:17:57 +00003851 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003853 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003854
Hanno Becker19859472018-08-06 09:40:20 +01003855 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003856 ssl->out_len[0] = (unsigned char)( len >> 8 );
3857 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003858
Paul Bakker48916f92012-09-16 19:57:18 +00003859 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003860 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003861 mbedtls_record rec;
3862
3863 rec.buf = ssl->out_iv;
3864 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3865 ( ssl->out_iv - ssl->out_buf );
3866 rec.data_len = ssl->out_msglen;
3867 rec.data_offset = ssl->out_msg - rec.buf;
3868
3869 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3870 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3871 ssl->conf->transport, rec.ver );
3872 rec.type = ssl->out_msgtype;
3873
Hanno Beckera18d1322018-01-03 14:27:32 +00003874 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003875 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003878 return( ret );
3879 }
3880
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003881 if( rec.data_offset != 0 )
3882 {
3883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3885 }
3886
Hanno Becker78f839d2019-03-14 12:56:23 +00003887 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003888 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3889 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 }
3891
Hanno Becker2b1e3542018-08-06 11:19:13 +01003892 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3893
3894#if defined(MBEDTLS_SSL_PROTO_DTLS)
3895 /* In case of DTLS, double-check that we don't exceed
3896 * the remaining space in the datagram. */
3897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3898 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003899 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003900 if( ret < 0 )
3901 return( ret );
3902
3903 if( protected_record_size > (size_t) ret )
3904 {
3905 /* Should never happen */
3906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3907 }
3908 }
3909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003912 "version = [%d:%d], msglen = %d",
3913 ssl->out_hdr[0], ssl->out_hdr[1],
3914 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003917 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003918
3919 ssl->out_left += protected_record_size;
3920 ssl->out_hdr += protected_record_size;
3921 ssl_update_out_pointers( ssl, ssl->transform_out );
3922
Hanno Becker04484622018-08-06 09:49:38 +01003923 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3924 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3925 break;
3926
3927 /* The loop goes to its end iff the counter is wrapping */
3928 if( i == ssl_ep_len( ssl ) )
3929 {
3930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3931 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 }
3934
Hanno Becker67bc7c32018-08-06 11:33:50 +01003935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003936 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3937 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003938 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003939 size_t remaining;
3940 ret = ssl_get_remaining_payload_in_datagram( ssl );
3941 if( ret < 0 )
3942 {
3943 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3944 ret );
3945 return( ret );
3946 }
3947
3948 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003949 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003950 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003951 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003952 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003953 else
3954 {
Hanno Becker513815a2018-08-20 11:56:09 +01003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 }
3957 }
3958#endif /* MBEDTLS_SSL_PROTO_DTLS */
3959
3960 if( ( flush == SSL_FORCE_FLUSH ) &&
3961 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 return( ret );
3965 }
3966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
3969 return( 0 );
3970}
3971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003973
3974static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3975{
3976 if( ssl->in_msglen < ssl->in_hslen ||
3977 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3978 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3979 {
3980 return( 1 );
3981 }
3982 return( 0 );
3983}
Hanno Becker44650b72018-08-16 12:51:11 +01003984
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003985static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003986{
3987 return( ( ssl->in_msg[9] << 16 ) |
3988 ( ssl->in_msg[10] << 8 ) |
3989 ssl->in_msg[11] );
3990}
3991
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003992static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003993{
3994 return( ( ssl->in_msg[6] << 16 ) |
3995 ( ssl->in_msg[7] << 8 ) |
3996 ssl->in_msg[8] );
3997}
3998
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003999static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004000{
4001 uint32_t msg_len, frag_off, frag_len;
4002
4003 msg_len = ssl_get_hs_total_len( ssl );
4004 frag_off = ssl_get_hs_frag_off( ssl );
4005 frag_len = ssl_get_hs_frag_len( ssl );
4006
4007 if( frag_off > msg_len )
4008 return( -1 );
4009
4010 if( frag_len > msg_len - frag_off )
4011 return( -1 );
4012
4013 if( frag_len + 12 > ssl->in_msglen )
4014 return( -1 );
4015
4016 return( 0 );
4017}
4018
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004019/*
4020 * Mark bits in bitmask (used for DTLS HS reassembly)
4021 */
4022static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4023{
4024 unsigned int start_bits, end_bits;
4025
4026 start_bits = 8 - ( offset % 8 );
4027 if( start_bits != 8 )
4028 {
4029 size_t first_byte_idx = offset / 8;
4030
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004031 /* Special case */
4032 if( len <= start_bits )
4033 {
4034 for( ; len != 0; len-- )
4035 mask[first_byte_idx] |= 1 << ( start_bits - len );
4036
4037 /* Avoid potential issues with offset or len becoming invalid */
4038 return;
4039 }
4040
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004041 offset += start_bits; /* Now offset % 8 == 0 */
4042 len -= start_bits;
4043
4044 for( ; start_bits != 0; start_bits-- )
4045 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4046 }
4047
4048 end_bits = len % 8;
4049 if( end_bits != 0 )
4050 {
4051 size_t last_byte_idx = ( offset + len ) / 8;
4052
4053 len -= end_bits; /* Now len % 8 == 0 */
4054
4055 for( ; end_bits != 0; end_bits-- )
4056 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4057 }
4058
4059 memset( mask + offset / 8, 0xFF, len / 8 );
4060}
4061
4062/*
4063 * Check that bitmask is full
4064 */
4065static int ssl_bitmask_check( unsigned char *mask, size_t len )
4066{
4067 size_t i;
4068
4069 for( i = 0; i < len / 8; i++ )
4070 if( mask[i] != 0xFF )
4071 return( -1 );
4072
4073 for( i = 0; i < len % 8; i++ )
4074 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4075 return( -1 );
4076
4077 return( 0 );
4078}
4079
Hanno Becker56e205e2018-08-16 09:06:12 +01004080/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004081static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004082 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004083{
Hanno Becker56e205e2018-08-16 09:06:12 +01004084 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004085
Hanno Becker56e205e2018-08-16 09:06:12 +01004086 alloc_len = 12; /* Handshake header */
4087 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004088
Hanno Beckerd07df862018-08-16 09:14:58 +01004089 if( add_bitmap )
4090 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004091
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004092 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004093}
Hanno Becker56e205e2018-08-16 09:06:12 +01004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004096
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004097static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004098{
4099 return( ( ssl->in_msg[1] << 16 ) |
4100 ( ssl->in_msg[2] << 8 ) |
4101 ssl->in_msg[3] );
4102}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004103
Simon Butcher99000142016-10-13 17:21:01 +01004104int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004109 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004111 }
4112
Hanno Becker12555c62018-08-16 12:47:53 +01004113 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004116 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004117 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004120 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004121 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004122 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004123 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004124
Hanno Becker44650b72018-08-16 12:51:11 +01004125 if( ssl_check_hs_header( ssl ) != 0 )
4126 {
4127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4128 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4129 }
4130
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004131 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004132 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4133 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4134 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4135 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004136 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004137 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4138 {
4139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4140 recv_msg_seq,
4141 ssl->handshake->in_msg_seq ) );
4142 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4143 }
4144
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004145 /* Retransmit only on last message from previous flight, to avoid
4146 * too many retransmissions.
4147 * Besides, No sane server ever retransmits HelloVerifyRequest */
4148 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004152 "message_seq = %d, start_of_flight = %d",
4153 recv_msg_seq,
4154 ssl->handshake->in_flight_start_seq ) );
4155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004159 return( ret );
4160 }
4161 }
4162 else
4163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004165 "message_seq = %d, expected = %d",
4166 recv_msg_seq,
4167 ssl->handshake->in_msg_seq ) );
4168 }
4169
Hanno Becker90333da2017-10-10 11:27:13 +01004170 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004171 }
4172 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173
Hanno Becker6d97ef52018-08-16 13:09:04 +01004174 /* Message reassembly is handled alongside buffering of future
4175 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004176 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004177 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004178 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004181 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004182 }
4183 }
4184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004186 /* With TLS we don't handle fragmentation (for now) */
4187 if( ssl->in_msglen < ssl->in_hslen )
4188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4190 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004191 }
4192
Simon Butcher99000142016-10-13 17:21:01 +01004193 return( 0 );
4194}
4195
4196void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4197{
Hanno Becker0271f962018-08-16 13:23:47 +01004198 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004199
Hanno Becker0271f962018-08-16 13:23:47 +01004200 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004201 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004202 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004203 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004204
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004205 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004208 ssl->handshake != NULL )
4209 {
Hanno Becker0271f962018-08-16 13:23:47 +01004210 unsigned offset;
4211 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004212
Hanno Becker0271f962018-08-16 13:23:47 +01004213 /* Increment handshake sequence number */
4214 hs->in_msg_seq++;
4215
4216 /*
4217 * Clear up handshake buffering and reassembly structure.
4218 */
4219
4220 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004221 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004222
4223 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004224 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4225 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004226 offset++, hs_buf++ )
4227 {
4228 *hs_buf = *(hs_buf + 1);
4229 }
4230
4231 /* Create a fresh last entry */
4232 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004233 }
4234#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004235}
4236
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004237/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004238 * DTLS anti-replay: RFC 6347 4.1.2.6
4239 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004240 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4241 * Bit n is set iff record number in_window_top - n has been seen.
4242 *
4243 * Usually, in_window_top is the last record number seen and the lsb of
4244 * in_window is set. The only exception is the initial state (record number 0
4245 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4248static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004249{
4250 ssl->in_window_top = 0;
4251 ssl->in_window = 0;
4252}
4253
4254static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4255{
4256 return( ( (uint64_t) buf[0] << 40 ) |
4257 ( (uint64_t) buf[1] << 32 ) |
4258 ( (uint64_t) buf[2] << 24 ) |
4259 ( (uint64_t) buf[3] << 16 ) |
4260 ( (uint64_t) buf[4] << 8 ) |
4261 ( (uint64_t) buf[5] ) );
4262}
4263
4264/*
4265 * Return 0 if sequence number is acceptable, -1 otherwise
4266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004268{
4269 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4270 uint64_t bit;
4271
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004272 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004273 return( 0 );
4274
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004275 if( rec_seqnum > ssl->in_window_top )
4276 return( 0 );
4277
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004278 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004279
4280 if( bit >= 64 )
4281 return( -1 );
4282
4283 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4284 return( -1 );
4285
4286 return( 0 );
4287}
4288
4289/*
4290 * Update replay window on new validated record
4291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293{
4294 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4295
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004296 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004297 return;
4298
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299 if( rec_seqnum > ssl->in_window_top )
4300 {
4301 /* Update window_top and the contents of the window */
4302 uint64_t shift = rec_seqnum - ssl->in_window_top;
4303
4304 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004305 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004306 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004307 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004308 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004309 ssl->in_window |= 1;
4310 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004311
4312 ssl->in_window_top = rec_seqnum;
4313 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004314 else
4315 {
4316 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004317 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004318
4319 if( bit < 64 ) /* Always true, but be extra sure */
4320 ssl->in_window |= (uint64_t) 1 << bit;
4321 }
4322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004325#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004326/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004327static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4328
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004329/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004330 * Without any SSL context, check if a datagram looks like a ClientHello with
4331 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004332 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004333 *
4334 * - if cookie is valid, return 0
4335 * - if ClientHello looks superficially valid but cookie is not,
4336 * fill obuf and set olen, then
4337 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4338 * - otherwise return a specific error code
4339 */
4340static int ssl_check_dtls_clihlo_cookie(
4341 mbedtls_ssl_cookie_write_t *f_cookie_write,
4342 mbedtls_ssl_cookie_check_t *f_cookie_check,
4343 void *p_cookie,
4344 const unsigned char *cli_id, size_t cli_id_len,
4345 const unsigned char *in, size_t in_len,
4346 unsigned char *obuf, size_t buf_len, size_t *olen )
4347{
4348 size_t sid_len, cookie_len;
4349 unsigned char *p;
4350
4351 if( f_cookie_write == NULL || f_cookie_check == NULL )
4352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4353
4354 /*
4355 * Structure of ClientHello with record and handshake headers,
4356 * and expected values. We don't need to check a lot, more checks will be
4357 * done when actually parsing the ClientHello - skipping those checks
4358 * avoids code duplication and does not make cookie forging any easier.
4359 *
4360 * 0-0 ContentType type; copied, must be handshake
4361 * 1-2 ProtocolVersion version; copied
4362 * 3-4 uint16 epoch; copied, must be 0
4363 * 5-10 uint48 sequence_number; copied
4364 * 11-12 uint16 length; (ignored)
4365 *
4366 * 13-13 HandshakeType msg_type; (ignored)
4367 * 14-16 uint24 length; (ignored)
4368 * 17-18 uint16 message_seq; copied
4369 * 19-21 uint24 fragment_offset; copied, must be 0
4370 * 22-24 uint24 fragment_length; (ignored)
4371 *
4372 * 25-26 ProtocolVersion client_version; (ignored)
4373 * 27-58 Random random; (ignored)
4374 * 59-xx SessionID session_id; 1 byte len + sid_len content
4375 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4376 * ...
4377 *
4378 * Minimum length is 61 bytes.
4379 */
4380 if( in_len < 61 ||
4381 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4382 in[3] != 0 || in[4] != 0 ||
4383 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4384 {
4385 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4386 }
4387
4388 sid_len = in[59];
4389 if( sid_len > in_len - 61 )
4390 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4391
4392 cookie_len = in[60 + sid_len];
4393 if( cookie_len > in_len - 60 )
4394 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4395
4396 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4397 cli_id, cli_id_len ) == 0 )
4398 {
4399 /* Valid cookie */
4400 return( 0 );
4401 }
4402
4403 /*
4404 * If we get here, we've got an invalid cookie, let's prepare HVR.
4405 *
4406 * 0-0 ContentType type; copied
4407 * 1-2 ProtocolVersion version; copied
4408 * 3-4 uint16 epoch; copied
4409 * 5-10 uint48 sequence_number; copied
4410 * 11-12 uint16 length; olen - 13
4411 *
4412 * 13-13 HandshakeType msg_type; hello_verify_request
4413 * 14-16 uint24 length; olen - 25
4414 * 17-18 uint16 message_seq; copied
4415 * 19-21 uint24 fragment_offset; copied
4416 * 22-24 uint24 fragment_length; olen - 25
4417 *
4418 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4419 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4420 *
4421 * Minimum length is 28.
4422 */
4423 if( buf_len < 28 )
4424 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4425
4426 /* Copy most fields and adapt others */
4427 memcpy( obuf, in, 25 );
4428 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4429 obuf[25] = 0xfe;
4430 obuf[26] = 0xff;
4431
4432 /* Generate and write actual cookie */
4433 p = obuf + 28;
4434 if( f_cookie_write( p_cookie,
4435 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4436 {
4437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4438 }
4439
4440 *olen = p - obuf;
4441
4442 /* Go back and fill length fields */
4443 obuf[27] = (unsigned char)( *olen - 28 );
4444
4445 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4446 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4447 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4448
4449 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4450 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4451
4452 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4453}
4454
4455/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004456 * Handle possible client reconnect with the same UDP quadruplet
4457 * (RFC 6347 Section 4.2.8).
4458 *
4459 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4460 * that looks like a ClientHello.
4461 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004462 * - if the input looks like a ClientHello without cookies,
4463 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004464 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004465 * - if the input looks like a ClientHello with a valid cookie,
4466 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004467 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004468 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004469 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004470 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004471 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4472 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004473 */
4474static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4475{
4476 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004477 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004478
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004479 ret = ssl_check_dtls_clihlo_cookie(
4480 ssl->conf->f_cookie_write,
4481 ssl->conf->f_cookie_check,
4482 ssl->conf->p_cookie,
4483 ssl->cli_id, ssl->cli_id_len,
4484 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004485 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004486
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004487 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4488
4489 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004490 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004491 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004492 * If the error is permanent we'll catch it later,
4493 * if it's not, then hopefully it'll work next time. */
4494 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4495
4496 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004497 }
4498
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004499 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004500 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004501 /* Got a valid cookie, partially reset context */
4502 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4503 {
4504 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4505 return( ret );
4506 }
4507
4508 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 }
4510
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 return( ret );
4512}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004513#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004514
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004515/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004516 * ContentType type;
4517 * ProtocolVersion version;
4518 * uint16 epoch; // DTLS only
4519 * uint48 sequence_number; // DTLS only
4520 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004521 *
4522 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004523 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004524 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4525 *
4526 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004527 * 1. proceed with the record if this function returns 0
4528 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4529 * 3. return CLIENT_RECONNECT if this function return that value
4530 * 4. drop the whole datagram if this function returns anything else.
4531 * Point 2 is needed when the peer is resending, and we have already received
4532 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004535{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004536 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 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 +02004539
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004541 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004542 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004545 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004546 ssl->in_msgtype,
4547 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004549 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4551 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4552 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4553 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004556
4557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004558 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4559 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004560 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4561#endif /* MBEDTLS_SSL_PROTO_DTLS */
4562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4563 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004566 }
4567
4568 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004569 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4572 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 }
4574
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4578 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 }
4580
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004581 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004582 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004583 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4586 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004587 }
4588
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004589 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004590 * DTLS-related tests.
4591 * Check epoch before checking length constraint because
4592 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4593 * message gets duplicated before the corresponding Finished message,
4594 * the second ChangeCipherSpec should be discarded because it belongs
4595 * to an old epoch, but not because its length is shorter than
4596 * the minimum record length for packets using the new record transform.
4597 * Note that these two kinds of failures are handled differently,
4598 * as an unexpected record is silently skipped but an invalid
4599 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004600 */
4601#if defined(MBEDTLS_SSL_PROTO_DTLS)
4602 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4603 {
4604 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4605
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004606 /* Check epoch (and sequence number) with DTLS */
4607 if( rec_epoch != ssl->in_epoch )
4608 {
4609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4610 "expected %d, received %d",
4611 ssl->in_epoch, rec_epoch ) );
4612
4613#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4614 /*
4615 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4616 * access the first byte of record content (handshake type), as we
4617 * have an active transform (possibly iv_len != 0), so use the
4618 * fact that the record header len is 13 instead.
4619 */
4620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4621 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4622 rec_epoch == 0 &&
4623 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4624 ssl->in_left > 13 &&
4625 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4626 {
4627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4628 "from the same port" ) );
4629 return( ssl_handle_possible_reconnect( ssl ) );
4630 }
4631 else
4632#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004633 {
4634 /* Consider buffering the record. */
4635 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4636 {
4637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4638 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4639 }
4640
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004641 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004642 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004643 }
4644
4645#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4646 /* Replay detection only works for the current epoch */
4647 if( rec_epoch == ssl->in_epoch &&
4648 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4649 {
4650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4651 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4652 }
4653#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004654
Hanno Becker52c6dc62017-05-26 16:07:36 +01004655 /* Drop unexpected ApplicationData records,
4656 * except at the beginning of renegotiations */
4657 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4658 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4659#if defined(MBEDTLS_SSL_RENEGOTIATION)
4660 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4661 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4662#endif
4663 )
4664 {
4665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4666 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4667 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004668 }
4669#endif /* MBEDTLS_SSL_PROTO_DTLS */
4670
Hanno Becker52c6dc62017-05-26 16:07:36 +01004671
4672 /* Check length against bounds of the current transform and version */
4673 if( ssl->transform_in == NULL )
4674 {
4675 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004676 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004677 {
4678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4679 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4680 }
4681 }
4682 else
4683 {
4684 if( ssl->in_msglen < ssl->transform_in->minlen )
4685 {
4686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4688 }
4689
4690#if defined(MBEDTLS_SSL_PROTO_SSL3)
4691 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004692 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004693 {
4694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4695 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4696 }
4697#endif
4698#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4699 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4700 /*
4701 * TLS encrypted messages can have up to 256 bytes of padding
4702 */
4703 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4704 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004705 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004706 {
4707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4708 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4709 }
4710#endif
4711 }
4712
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004713 return( 0 );
4714}
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004716/*
4717 * If applicable, decrypt (and decompress) record content
4718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004720{
4721 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4724 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4727 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 ret = mbedtls_ssl_hw_record_read( ssl );
4732 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4735 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004736 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004737
4738 if( ret == 0 )
4739 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004742 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004744 mbedtls_record rec;
4745
4746 rec.buf = ssl->in_iv;
4747 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4748 - ( ssl->in_iv - ssl->in_buf );
4749 rec.data_len = ssl->in_msglen;
4750 rec.data_offset = 0;
4751
4752 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4753 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4754 ssl->conf->transport, rec.ver );
4755 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004756 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4757 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 return( ret );
4761 }
4762
Hanno Becker29800d22018-08-07 14:30:18 +01004763 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4764 {
4765 /* Should never happen */
4766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004768
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004769 ssl->in_msglen = rec.data_len;
4770 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4771 ssl->in_len[1] = (unsigned char)( rec.data_len );
4772
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004773 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4774 ssl->in_msg, ssl->in_msglen );
4775
Angus Grattond8213d02016-05-25 20:56:48 +10004776 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004781 else if( ssl->in_msglen == 0 )
4782 {
4783#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4784 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4785 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4786 {
4787 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4789 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4790 }
4791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4792
4793 ssl->nb_zero++;
4794
4795 /*
4796 * Three or more empty messages may be a DoS attack
4797 * (excessive CPU consumption).
4798 */
4799 if( ssl->nb_zero > 3 )
4800 {
4801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4802 "messages, possible DoS attack" ) );
4803 /* Q: Is that the right error code? */
4804 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4805 }
4806 }
4807 else
4808 ssl->nb_zero = 0;
4809
4810#if defined(MBEDTLS_SSL_PROTO_DTLS)
4811 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4812 {
4813 ; /* in_ctr read from peer, not maintained internally */
4814 }
4815 else
4816#endif
4817 {
4818 unsigned i;
4819 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4820 if( ++ssl->in_ctr[i - 1] != 0 )
4821 break;
4822
4823 /* The loop goes to its end iff the counter is wrapping */
4824 if( i == ssl_ep_len( ssl ) )
4825 {
4826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4827 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4828 }
4829 }
4830
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 }
4832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004834 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004836 {
4837 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004840 return( ret );
4841 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004842 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004846 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004849 }
4850#endif
4851
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004852 return( 0 );
4853}
4854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004856
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004857/*
4858 * Read a record.
4859 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004860 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4861 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4862 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004863 */
Hanno Becker1097b342018-08-15 14:09:41 +01004864
4865/* Helper functions for mbedtls_ssl_read_record(). */
4866static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004867static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4868static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004869
Hanno Becker327c93b2018-08-15 13:56:18 +01004870int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004871 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004872{
4873 int ret;
4874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004877 if( ssl->keep_current_message == 0 )
4878 {
4879 do {
Simon Butcher99000142016-10-13 17:21:01 +01004880
Hanno Becker26994592018-08-15 14:14:59 +01004881 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004882 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004883 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004884
Hanno Beckere74d5562018-08-15 14:26:08 +01004885 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004886 {
Hanno Becker40f50842018-08-15 14:48:01 +01004887#if defined(MBEDTLS_SSL_PROTO_DTLS)
4888 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004889
Hanno Becker40f50842018-08-15 14:48:01 +01004890 /* We only check for buffered messages if the
4891 * current datagram is fully consumed. */
4892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004893 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004894 {
Hanno Becker40f50842018-08-15 14:48:01 +01004895 if( ssl_load_buffered_message( ssl ) == 0 )
4896 have_buffered = 1;
4897 }
4898
4899 if( have_buffered == 0 )
4900#endif /* MBEDTLS_SSL_PROTO_DTLS */
4901 {
4902 ret = ssl_get_next_record( ssl );
4903 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4904 continue;
4905
4906 if( ret != 0 )
4907 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004908 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004909 return( ret );
4910 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004911 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004912 }
4913
4914 ret = mbedtls_ssl_handle_message_type( ssl );
4915
Hanno Becker40f50842018-08-15 14:48:01 +01004916#if defined(MBEDTLS_SSL_PROTO_DTLS)
4917 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4918 {
4919 /* Buffer future message */
4920 ret = ssl_buffer_message( ssl );
4921 if( ret != 0 )
4922 return( ret );
4923
4924 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4925 }
4926#endif /* MBEDTLS_SSL_PROTO_DTLS */
4927
Hanno Becker90333da2017-10-10 11:27:13 +01004928 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4929 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004930
4931 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004932 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004933 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004934 return( ret );
4935 }
4936
Hanno Becker327c93b2018-08-15 13:56:18 +01004937 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004938 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004939 {
4940 mbedtls_ssl_update_handshake_status( ssl );
4941 }
Simon Butcher99000142016-10-13 17:21:01 +01004942 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004943 else
Simon Butcher99000142016-10-13 17:21:01 +01004944 {
Hanno Becker02f59072018-08-15 14:00:24 +01004945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004946 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004947 }
4948
4949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4950
4951 return( 0 );
4952}
4953
Hanno Becker40f50842018-08-15 14:48:01 +01004954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004955static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004956{
Hanno Becker40f50842018-08-15 14:48:01 +01004957 if( ssl->in_left > ssl->next_record_offset )
4958 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004959
Hanno Becker40f50842018-08-15 14:48:01 +01004960 return( 0 );
4961}
4962
4963static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4964{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004965 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004966 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004967 int ret = 0;
4968
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004969 if( hs == NULL )
4970 return( -1 );
4971
Hanno Beckere00ae372018-08-20 09:39:42 +01004972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4973
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004974 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4975 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4976 {
4977 /* Check if we have seen a ChangeCipherSpec before.
4978 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004979 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4982 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004983 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004984 }
4985
Hanno Becker39b8bc92018-08-28 17:17:13 +01004986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004987 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4988 ssl->in_msglen = 1;
4989 ssl->in_msg[0] = 1;
4990
4991 /* As long as they are equal, the exact value doesn't matter. */
4992 ssl->in_left = 0;
4993 ssl->next_record_offset = 0;
4994
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004995 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004996 goto exit;
4997 }
Hanno Becker37f95322018-08-16 13:55:32 +01004998
Hanno Beckerb8f50142018-08-28 10:01:34 +01004999#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005000 /* Debug only */
5001 {
5002 unsigned offset;
5003 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5004 {
5005 hs_buf = &hs->buffering.hs[offset];
5006 if( hs_buf->is_valid == 1 )
5007 {
5008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5009 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005010 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005011 }
5012 }
5013 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005014#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005015
5016 /* Check if we have buffered and/or fully reassembled the
5017 * next handshake message. */
5018 hs_buf = &hs->buffering.hs[0];
5019 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5020 {
5021 /* Synthesize a record containing the buffered HS message. */
5022 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5023 ( hs_buf->data[2] << 8 ) |
5024 hs_buf->data[3];
5025
5026 /* Double-check that we haven't accidentally buffered
5027 * a message that doesn't fit into the input buffer. */
5028 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5029 {
5030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5032 }
5033
5034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5035 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5036 hs_buf->data, msg_len + 12 );
5037
5038 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5039 ssl->in_hslen = msg_len + 12;
5040 ssl->in_msglen = msg_len + 12;
5041 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5042
5043 ret = 0;
5044 goto exit;
5045 }
5046 else
5047 {
5048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5049 hs->in_msg_seq ) );
5050 }
5051
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005052 ret = -1;
5053
5054exit:
5055
5056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5057 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005058}
5059
Hanno Beckera02b0b42018-08-21 17:20:27 +01005060static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5061 size_t desired )
5062{
5063 int offset;
5064 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5066 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005067
Hanno Becker01315ea2018-08-21 17:22:17 +01005068 /* Get rid of future records epoch first, if such exist. */
5069 ssl_free_buffered_record( ssl );
5070
5071 /* Check if we have enough space available now. */
5072 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5073 hs->buffering.total_bytes_buffered ) )
5074 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005076 return( 0 );
5077 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005078
Hanno Becker4f432ad2018-08-28 10:02:32 +01005079 /* We don't have enough space to buffer the next expected handshake
5080 * message. Remove buffers used for future messages to gain space,
5081 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005082 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5083 offset >= 0; offset-- )
5084 {
5085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5086 offset ) );
5087
Hanno Beckerb309b922018-08-23 13:18:05 +01005088 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005089
5090 /* Check if we have enough space available now. */
5091 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5092 hs->buffering.total_bytes_buffered ) )
5093 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005095 return( 0 );
5096 }
5097 }
5098
5099 return( -1 );
5100}
5101
Hanno Becker40f50842018-08-15 14:48:01 +01005102static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5103{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 int ret = 0;
5105 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5106
5107 if( hs == NULL )
5108 return( 0 );
5109
5110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5111
5112 switch( ssl->in_msgtype )
5113 {
5114 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005116
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005117 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005118 break;
5119
5120 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005121 {
5122 unsigned recv_msg_seq_offset;
5123 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5124 mbedtls_ssl_hs_buffer *hs_buf;
5125 size_t msg_len = ssl->in_hslen - 12;
5126
5127 /* We should never receive an old handshake
5128 * message - double-check nonetheless. */
5129 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5130 {
5131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5133 }
5134
5135 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5136 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5137 {
5138 /* Silently ignore -- message too far in the future */
5139 MBEDTLS_SSL_DEBUG_MSG( 2,
5140 ( "Ignore future HS message with sequence number %u, "
5141 "buffering window %u - %u",
5142 recv_msg_seq, ssl->handshake->in_msg_seq,
5143 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5144
5145 goto exit;
5146 }
5147
5148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5149 recv_msg_seq, recv_msg_seq_offset ) );
5150
5151 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5152
5153 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005154 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005155 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005156 size_t reassembly_buf_sz;
5157
Hanno Becker37f95322018-08-16 13:55:32 +01005158 hs_buf->is_fragmented =
5159 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5160
5161 /* We copy the message back into the input buffer
5162 * after reassembly, so check that it's not too large.
5163 * This is an implementation-specific limitation
5164 * and not one from the standard, hence it is not
5165 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005166 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005167 {
5168 /* Ignore message */
5169 goto exit;
5170 }
5171
Hanno Beckere0b150f2018-08-21 15:51:03 +01005172 /* Check if we have enough space to buffer the message. */
5173 if( hs->buffering.total_bytes_buffered >
5174 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5175 {
5176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5178 }
5179
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005180 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5181 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005182
5183 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5184 hs->buffering.total_bytes_buffered ) )
5185 {
5186 if( recv_msg_seq_offset > 0 )
5187 {
5188 /* If we can't buffer a future message because
5189 * of space limitations -- ignore. */
5190 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",
5191 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5192 (unsigned) hs->buffering.total_bytes_buffered ) );
5193 goto exit;
5194 }
Hanno Beckere1801392018-08-21 16:51:05 +01005195 else
5196 {
5197 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",
5198 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5199 (unsigned) hs->buffering.total_bytes_buffered ) );
5200 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005201
Hanno Beckera02b0b42018-08-21 17:20:27 +01005202 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005203 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005204 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",
5205 (unsigned) msg_len,
5206 (unsigned) reassembly_buf_sz,
5207 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005208 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005209 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5210 goto exit;
5211 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005212 }
5213
5214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5215 msg_len ) );
5216
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005217 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5218 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005219 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005220 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005221 goto exit;
5222 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005223 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005224
5225 /* Prepare final header: copy msg_type, length and message_seq,
5226 * then add standardised fragment_offset and fragment_length */
5227 memcpy( hs_buf->data, ssl->in_msg, 6 );
5228 memset( hs_buf->data + 6, 0, 3 );
5229 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5230
5231 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005232
5233 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005234 }
5235 else
5236 {
5237 /* Make sure msg_type and length are consistent */
5238 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5241 /* Ignore */
5242 goto exit;
5243 }
5244 }
5245
Hanno Becker4422bbb2018-08-20 09:40:19 +01005246 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005247 {
5248 size_t frag_len, frag_off;
5249 unsigned char * const msg = hs_buf->data + 12;
5250
5251 /*
5252 * Check and copy current fragment
5253 */
5254
5255 /* Validation of header fields already done in
5256 * mbedtls_ssl_prepare_handshake_record(). */
5257 frag_off = ssl_get_hs_frag_off( ssl );
5258 frag_len = ssl_get_hs_frag_len( ssl );
5259
5260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5261 frag_off, frag_len ) );
5262 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5263
5264 if( hs_buf->is_fragmented )
5265 {
5266 unsigned char * const bitmask = msg + msg_len;
5267 ssl_bitmask_set( bitmask, frag_off, frag_len );
5268 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5269 msg_len ) == 0 );
5270 }
5271 else
5272 {
5273 hs_buf->is_complete = 1;
5274 }
5275
5276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5277 hs_buf->is_complete ? "" : "not yet " ) );
5278 }
5279
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005280 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005281 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005282
5283 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005284 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005285 break;
5286 }
5287
5288exit:
5289
5290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5291 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005292}
5293#endif /* MBEDTLS_SSL_PROTO_DTLS */
5294
Hanno Becker1097b342018-08-15 14:09:41 +01005295static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005296{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005297 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005298 * Consume last content-layer message and potentially
5299 * update in_msglen which keeps track of the contents'
5300 * consumption state.
5301 *
5302 * (1) Handshake messages:
5303 * Remove last handshake message, move content
5304 * and adapt in_msglen.
5305 *
5306 * (2) Alert messages:
5307 * Consume whole record content, in_msglen = 0.
5308 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005309 * (3) Change cipher spec:
5310 * Consume whole record content, in_msglen = 0.
5311 *
5312 * (4) Application data:
5313 * Don't do anything - the record layer provides
5314 * the application data as a stream transport
5315 * and consumes through mbedtls_ssl_read only.
5316 *
5317 */
5318
5319 /* Case (1): Handshake messages */
5320 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005322 /* Hard assertion to be sure that no application data
5323 * is in flight, as corrupting ssl->in_msglen during
5324 * ssl->in_offt != NULL is fatal. */
5325 if( ssl->in_offt != NULL )
5326 {
5327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5329 }
5330
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005331 /*
5332 * Get next Handshake message in the current record
5333 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005334
Hanno Becker4a810fb2017-05-24 16:27:30 +01005335 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005336 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005337 * current handshake content: If DTLS handshake
5338 * fragmentation is used, that's the fragment
5339 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005340 * size here is faulty and should be changed at
5341 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005342 * (2) While it doesn't seem to cause problems, one
5343 * has to be very careful not to assume that in_hslen
5344 * is always <= in_msglen in a sensible communication.
5345 * Again, it's wrong for DTLS handshake fragmentation.
5346 * The following check is therefore mandatory, and
5347 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005348 * Additionally, ssl->in_hslen might be arbitrarily out of
5349 * bounds after handling a DTLS message with an unexpected
5350 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005351 */
5352 if( ssl->in_hslen < ssl->in_msglen )
5353 {
5354 ssl->in_msglen -= ssl->in_hslen;
5355 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5356 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005357
Hanno Becker4a810fb2017-05-24 16:27:30 +01005358 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5359 ssl->in_msg, ssl->in_msglen );
5360 }
5361 else
5362 {
5363 ssl->in_msglen = 0;
5364 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005365
Hanno Becker4a810fb2017-05-24 16:27:30 +01005366 ssl->in_hslen = 0;
5367 }
5368 /* Case (4): Application data */
5369 else if( ssl->in_offt != NULL )
5370 {
5371 return( 0 );
5372 }
5373 /* Everything else (CCS & Alerts) */
5374 else
5375 {
5376 ssl->in_msglen = 0;
5377 }
5378
Hanno Becker1097b342018-08-15 14:09:41 +01005379 return( 0 );
5380}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005381
Hanno Beckere74d5562018-08-15 14:26:08 +01005382static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5383{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005384 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005385 return( 1 );
5386
5387 return( 0 );
5388}
5389
Hanno Becker5f066e72018-08-16 14:56:31 +01005390#if defined(MBEDTLS_SSL_PROTO_DTLS)
5391
5392static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5393{
5394 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5395 if( hs == NULL )
5396 return;
5397
Hanno Becker01315ea2018-08-21 17:22:17 +01005398 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005399 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005400 hs->buffering.total_bytes_buffered -=
5401 hs->buffering.future_record.len;
5402
5403 mbedtls_free( hs->buffering.future_record.data );
5404 hs->buffering.future_record.data = NULL;
5405 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005406}
5407
5408static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5409{
5410 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5411 unsigned char * rec;
5412 size_t rec_len;
5413 unsigned rec_epoch;
5414
5415 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5416 return( 0 );
5417
5418 if( hs == NULL )
5419 return( 0 );
5420
Hanno Becker5f066e72018-08-16 14:56:31 +01005421 rec = hs->buffering.future_record.data;
5422 rec_len = hs->buffering.future_record.len;
5423 rec_epoch = hs->buffering.future_record.epoch;
5424
5425 if( rec == NULL )
5426 return( 0 );
5427
Hanno Becker4cb782d2018-08-20 11:19:05 +01005428 /* Only consider loading future records if the
5429 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005430 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005431 return( 0 );
5432
Hanno Becker5f066e72018-08-16 14:56:31 +01005433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5434
5435 if( rec_epoch != ssl->in_epoch )
5436 {
5437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5438 goto exit;
5439 }
5440
5441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5442
5443 /* Double-check that the record is not too large */
5444 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5445 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5446 {
5447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5449 }
5450
5451 memcpy( ssl->in_hdr, rec, rec_len );
5452 ssl->in_left = rec_len;
5453 ssl->next_record_offset = 0;
5454
5455 ssl_free_buffered_record( ssl );
5456
5457exit:
5458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5459 return( 0 );
5460}
5461
5462static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5463{
5464 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5465 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005466 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005467
5468 /* Don't buffer future records outside handshakes. */
5469 if( hs == NULL )
5470 return( 0 );
5471
5472 /* Only buffer handshake records (we are only interested
5473 * in Finished messages). */
5474 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5475 return( 0 );
5476
5477 /* Don't buffer more than one future epoch record. */
5478 if( hs->buffering.future_record.data != NULL )
5479 return( 0 );
5480
Hanno Becker01315ea2018-08-21 17:22:17 +01005481 /* Don't buffer record if there's not enough buffering space remaining. */
5482 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5483 hs->buffering.total_bytes_buffered ) )
5484 {
5485 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",
5486 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5487 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005488 return( 0 );
5489 }
5490
Hanno Becker5f066e72018-08-16 14:56:31 +01005491 /* Buffer record */
5492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5493 ssl->in_epoch + 1 ) );
5494 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5495 rec_hdr_len + ssl->in_msglen );
5496
5497 /* ssl_parse_record_header() only considers records
5498 * of the next epoch as candidates for buffering. */
5499 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005500 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005501
5502 hs->buffering.future_record.data =
5503 mbedtls_calloc( 1, hs->buffering.future_record.len );
5504 if( hs->buffering.future_record.data == NULL )
5505 {
5506 /* If we run out of RAM trying to buffer a
5507 * record from the next epoch, just ignore. */
5508 return( 0 );
5509 }
5510
Hanno Becker01315ea2018-08-21 17:22:17 +01005511 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005512
Hanno Becker01315ea2018-08-21 17:22:17 +01005513 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005514 return( 0 );
5515}
5516
5517#endif /* MBEDTLS_SSL_PROTO_DTLS */
5518
Hanno Beckere74d5562018-08-15 14:26:08 +01005519static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005520{
5521 int ret;
5522
Hanno Becker5f066e72018-08-16 14:56:31 +01005523#if defined(MBEDTLS_SSL_PROTO_DTLS)
5524 /* We might have buffered a future record; if so,
5525 * and if the epoch matches now, load it.
5526 * On success, this call will set ssl->in_left to
5527 * the length of the buffered record, so that
5528 * the calls to ssl_fetch_input() below will
5529 * essentially be no-ops. */
5530 ret = ssl_load_buffered_record( ssl );
5531 if( ret != 0 )
5532 return( ret );
5533#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005538 return( ret );
5539 }
5540
5541 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5545 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005546 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005547 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5548 {
5549 ret = ssl_buffer_future_record( ssl );
5550 if( ret != 0 )
5551 return( ret );
5552
5553 /* Fall through to handling of unexpected records */
5554 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5555 }
5556
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005557 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5558 {
5559 /* Skip unexpected record (but not whole datagram) */
5560 ssl->next_record_offset = ssl->in_msglen
5561 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005562
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5564 "(header)" ) );
5565 }
5566 else
5567 {
5568 /* Skip invalid record and the rest of the datagram */
5569 ssl->next_record_offset = 0;
5570 ssl->in_left = 0;
5571
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5573 "(header)" ) );
5574 }
5575
5576 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005577 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005578 }
5579#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005580 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005581 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005582
5583 /*
5584 * Read and optionally decrypt the message contents
5585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5587 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005590 return( ret );
5591 }
5592
5593 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005598 if( ssl->next_record_offset < ssl->in_left )
5599 {
5600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5601 }
5602 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005603 else
5604#endif
5605 ssl->in_left = 0;
5606
5607 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005611 {
5612 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5614 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005616 /* Except when waiting for Finished as a bad mac here
5617 * probably means something went wrong in the handshake
5618 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5619 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5620 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5621 {
5622#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5623 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5624 {
5625 mbedtls_ssl_send_alert_message( ssl,
5626 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5627 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5628 }
5629#endif
5630 return( ret );
5631 }
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005634 if( ssl->conf->badmac_limit != 0 &&
5635 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5638 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005639 }
5640#endif
5641
Hanno Becker4a810fb2017-05-24 16:27:30 +01005642 /* As above, invalid records cause
5643 * dismissal of the whole datagram. */
5644
5645 ssl->next_record_offset = 0;
5646 ssl->in_left = 0;
5647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005649 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005650 }
5651
5652 return( ret );
5653 }
5654 else
5655#endif
5656 {
5657 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5659 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 mbedtls_ssl_send_alert_message( ssl,
5662 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5663 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005664 }
5665#endif
5666 return( ret );
5667 }
5668 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669
Simon Butcher99000142016-10-13 17:21:01 +01005670 return( 0 );
5671}
5672
5673int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5674{
5675 int ret;
5676
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005677 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 * Handle particular types of records
5679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 {
Simon Butcher99000142016-10-13 17:21:01 +01005682 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5683 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005684 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 }
5687
Hanno Beckere678eaa2018-08-21 14:57:46 +01005688 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005689 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005690 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005691 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5693 ssl->in_msglen ) );
5694 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005695 }
5696
Hanno Beckere678eaa2018-08-21 14:57:46 +01005697 if( ssl->in_msg[0] != 1 )
5698 {
5699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5700 ssl->in_msg[0] ) );
5701 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5702 }
5703
5704#if defined(MBEDTLS_SSL_PROTO_DTLS)
5705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5706 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5707 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5708 {
5709 if( ssl->handshake == NULL )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5712 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5713 }
5714
5715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5716 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5717 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005718#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005719 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005722 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005723 if( ssl->in_msglen != 2 )
5724 {
5725 /* Note: Standard allows for more than one 2 byte alert
5726 to be packed in a single message, but Mbed TLS doesn't
5727 currently support this. */
5728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5729 ssl->in_msglen ) );
5730 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5731 }
5732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 ssl->in_msg[0], ssl->in_msg[1] ) );
5735
5736 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005737 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005742 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 }
5745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5747 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5750 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005751 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005752
5753#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5754 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5755 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5756 {
Hanno Becker90333da2017-10-10 11:27:13 +01005757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005758 /* Will be handled when trying to parse ServerHello */
5759 return( 0 );
5760 }
5761#endif
5762
5763#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5764 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5765 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5766 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5767 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5768 {
5769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5770 /* Will be handled in mbedtls_ssl_parse_certificate() */
5771 return( 0 );
5772 }
5773#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5774
5775 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005776 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005777 }
5778
Hanno Beckerc76c6192017-06-06 10:03:17 +01005779#if defined(MBEDTLS_SSL_PROTO_DTLS)
5780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5781 ssl->handshake != NULL &&
5782 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5783 {
5784 ssl_handshake_wrapup_free_hs_transform( ssl );
5785 }
5786#endif
5787
Paul Bakker5121ce52009-01-03 21:22:43 +00005788 return( 0 );
5789}
5790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005792{
5793 int ret;
5794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5796 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5797 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005798 {
5799 return( ret );
5800 }
5801
5802 return( 0 );
5803}
5804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005806 unsigned char level,
5807 unsigned char message )
5808{
5809 int ret;
5810
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005811 if( ssl == NULL || ssl->conf == NULL )
5812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005818 ssl->out_msglen = 2;
5819 ssl->out_msg[0] = level;
5820 ssl->out_msg[1] = message;
5821
Hanno Becker67bc7c32018-08-06 11:33:50 +01005822 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005825 return( ret );
5826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005828
5829 return( 0 );
5830}
5831
Hanno Beckerb9d44792019-02-08 07:19:04 +00005832#if defined(MBEDTLS_X509_CRT_PARSE_C)
5833static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5834{
5835#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5836 if( session->peer_cert != NULL )
5837 {
5838 mbedtls_x509_crt_free( session->peer_cert );
5839 mbedtls_free( session->peer_cert );
5840 session->peer_cert = NULL;
5841 }
5842#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5843 if( session->peer_cert_digest != NULL )
5844 {
5845 /* Zeroization is not necessary. */
5846 mbedtls_free( session->peer_cert_digest );
5847 session->peer_cert_digest = NULL;
5848 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5849 session->peer_cert_digest_len = 0;
5850 }
5851#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5852}
5853#endif /* MBEDTLS_X509_CRT_PARSE_C */
5854
Paul Bakker5121ce52009-01-03 21:22:43 +00005855/*
5856 * Handshake functions
5857 */
Hanno Becker21489932019-02-05 13:20:55 +00005858#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005859/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005861{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005862 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5863 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866
Hanno Becker7177a882019-02-05 13:36:46 +00005867 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005870 ssl->state++;
5871 return( 0 );
5872 }
5873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005876}
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005879{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005880 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5881 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005884
Hanno Becker7177a882019-02-05 13:36:46 +00005885 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005888 ssl->state++;
5889 return( 0 );
5890 }
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005894}
Gilles Peskinef9828522017-05-03 12:28:43 +02005895
Hanno Becker21489932019-02-05 13:20:55 +00005896#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005897/* Some certificate support -> implement write and parse */
5898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005902 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005904 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5905 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005908
Hanno Becker7177a882019-02-05 13:36:46 +00005909 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005912 ssl->state++;
5913 return( 0 );
5914 }
5915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005917 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005918 {
5919 if( ssl->client_auth == 0 )
5920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 ssl->state++;
5923 return( 0 );
5924 }
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005927 /*
5928 * If using SSLv3 and got no cert, send an Alert message
5929 * (otherwise an empty Certificate message will be sent).
5930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5932 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 {
5934 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5936 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5937 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 goto write_msg;
5941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944#endif /* MBEDTLS_SSL_CLI_C */
5945#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005946 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5951 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 }
5953 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005957
5958 /*
5959 * 0 . 0 handshake type
5960 * 1 . 3 handshake length
5961 * 4 . 6 length of all certs
5962 * 7 . 9 length of cert. 1
5963 * 10 . n-1 peer certificate
5964 * n . n+2 length of cert. 2
5965 * n+3 . ... upper level cert, etc.
5966 */
5967 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Paul Bakker29087132010-03-21 21:03:34 +00005970 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005971 {
5972 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005973 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005976 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005978 }
5979
5980 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5981 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5982 ssl->out_msg[i + 2] = (unsigned char)( n );
5983
5984 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5985 i += n; crt = crt->next;
5986 }
5987
5988 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5989 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5990 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5991
5992 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5994 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005995
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005996#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005997write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005998#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005999
6000 ssl->state++;
6001
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006002 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 return( ret );
6006 }
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006009
Paul Bakkered27a042013-04-18 22:46:23 +02006010 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006011}
6012
Hanno Becker84879e32019-01-31 07:44:03 +00006013#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006014
6015#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006016static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6017 unsigned char *crt_buf,
6018 size_t crt_buf_len )
6019{
6020 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6021
6022 if( peer_crt == NULL )
6023 return( -1 );
6024
6025 if( peer_crt->raw.len != crt_buf_len )
6026 return( -1 );
6027
Hanno Becker46f34d02019-02-08 14:00:04 +00006028 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006029}
Hanno Becker177475a2019-02-05 17:02:46 +00006030#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6031static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6032 unsigned char *crt_buf,
6033 size_t crt_buf_len )
6034{
6035 int ret;
6036 unsigned char const * const peer_cert_digest =
6037 ssl->session->peer_cert_digest;
6038 mbedtls_md_type_t const peer_cert_digest_type =
6039 ssl->session->peer_cert_digest_type;
6040 mbedtls_md_info_t const * const digest_info =
6041 mbedtls_md_info_from_type( peer_cert_digest_type );
6042 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6043 size_t digest_len;
6044
6045 if( peer_cert_digest == NULL || digest_info == NULL )
6046 return( -1 );
6047
6048 digest_len = mbedtls_md_get_size( digest_info );
6049 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6050 return( -1 );
6051
6052 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6053 if( ret != 0 )
6054 return( -1 );
6055
6056 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6057}
6058#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006059#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006060
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006061/*
6062 * Once the certificate message is read, parse it into a cert chain and
6063 * perform basic checks, but leave actual verification to the caller
6064 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006065static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6066 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006067{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006068 int ret;
6069#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6070 int crt_cnt=0;
6071#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006072 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006073 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006078 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6079 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 }
6082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6084 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6088 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 }
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006093
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006097 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006098
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006099 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006108 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6109 i += 3;
6110
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006111 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 while( i < ssl->in_hslen )
6113 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006114 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006115 if ( i + 3 > ssl->in_hslen ) {
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006117 mbedtls_ssl_send_alert_message( ssl,
6118 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6119 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006120 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6121 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006122 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6123 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 if( ssl->in_msg[i] != 0 )
6125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006127 mbedtls_ssl_send_alert_message( ssl,
6128 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6129 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 }
6132
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006133 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6135 | (unsigned int) ssl->in_msg[i + 2];
6136 i += 3;
6137
6138 if( n < 128 || i + n > ssl->in_hslen )
6139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006141 mbedtls_ssl_send_alert_message( ssl,
6142 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6143 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 }
6146
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006147 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006148#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6149 if( crt_cnt++ == 0 &&
6150 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6151 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006152 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006153 /* During client-side renegotiation, check that the server's
6154 * end-CRTs hasn't changed compared to the initial handshake,
6155 * mitigating the triple handshake attack. On success, reuse
6156 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6158 if( ssl_check_peer_crt_unchanged( ssl,
6159 &ssl->in_msg[i],
6160 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006161 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6163 mbedtls_ssl_send_alert_message( ssl,
6164 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6165 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6166 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006167 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006168
6169 /* Now we can safely free the original chain. */
6170 ssl_clear_peer_cert( ssl->session );
6171 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006172#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6173
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006174 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006175#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006176 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006177#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006178 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006179 * it in-place from the input buffer instead of making a copy. */
6180 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6181#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006182 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006184 case 0: /*ok*/
6185 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6186 /* Ignore certificate with an unknown algorithm: maybe a
6187 prior certificate was already trusted. */
6188 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006189
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006190 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6191 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6192 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006193
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006194 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6195 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6196 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006197
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006198 default:
6199 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6200 crt_parse_der_failed:
6201 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6202 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6203 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 }
6205
6206 i += n;
6207 }
6208
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006209 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006210 return( 0 );
6211}
6212
Hanno Becker4a55f632019-02-05 12:49:06 +00006213#if defined(MBEDTLS_SSL_SRV_C)
6214static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6215{
6216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6217 return( -1 );
6218
6219#if defined(MBEDTLS_SSL_PROTO_SSL3)
6220 /*
6221 * Check if the client sent an empty certificate
6222 */
6223 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6224 {
6225 if( ssl->in_msglen == 2 &&
6226 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6227 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6228 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6229 {
6230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6231 return( 0 );
6232 }
6233
6234 return( -1 );
6235 }
6236#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6237
6238#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6239 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6240 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6241 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6242 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6243 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6244 {
6245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6246 return( 0 );
6247 }
6248
6249 return( -1 );
6250#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6251 MBEDTLS_SSL_PROTO_TLS1_2 */
6252}
6253#endif /* MBEDTLS_SSL_SRV_C */
6254
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006255/* Check if a certificate message is expected.
6256 * Return either
6257 * - SSL_CERTIFICATE_EXPECTED, or
6258 * - SSL_CERTIFICATE_SKIP
6259 * indicating whether a Certificate message is expected or not.
6260 */
6261#define SSL_CERTIFICATE_EXPECTED 0
6262#define SSL_CERTIFICATE_SKIP 1
6263static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6264 int authmode )
6265{
6266 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006267 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006268
6269 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6270 return( SSL_CERTIFICATE_SKIP );
6271
6272#if defined(MBEDTLS_SSL_SRV_C)
6273 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6274 {
6275 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6276 return( SSL_CERTIFICATE_SKIP );
6277
6278 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6279 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006280 ssl->session_negotiate->verify_result =
6281 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6282 return( SSL_CERTIFICATE_SKIP );
6283 }
6284 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006285#else
6286 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006287#endif /* MBEDTLS_SSL_SRV_C */
6288
6289 return( SSL_CERTIFICATE_EXPECTED );
6290}
6291
Hanno Becker68636192019-02-05 14:36:34 +00006292static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6293 int authmode,
6294 mbedtls_x509_crt *chain,
6295 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006296{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006297 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006298 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006299 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006300 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006301
Hanno Becker8927c832019-04-03 12:52:50 +01006302 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6303 void *p_vrfy;
6304
Hanno Becker68636192019-02-05 14:36:34 +00006305 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6306 return( 0 );
6307
Hanno Becker8927c832019-04-03 12:52:50 +01006308 if( ssl->f_vrfy != NULL )
6309 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006311 f_vrfy = ssl->f_vrfy;
6312 p_vrfy = ssl->p_vrfy;
6313 }
6314 else
6315 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006317 f_vrfy = ssl->conf->f_vrfy;
6318 p_vrfy = ssl->conf->p_vrfy;
6319 }
6320
Hanno Becker68636192019-02-05 14:36:34 +00006321 /*
6322 * Main check: verify certificate
6323 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006324#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6325 if( ssl->conf->f_ca_cb != NULL )
6326 {
6327 ((void) rs_ctx);
6328 have_ca_chain = 1;
6329
6330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006331 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006332 chain,
6333 ssl->conf->f_ca_cb,
6334 ssl->conf->p_ca_cb,
6335 ssl->conf->cert_profile,
6336 ssl->hostname,
6337 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006338 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006339 }
6340 else
6341#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6342 {
6343 mbedtls_x509_crt *ca_chain;
6344 mbedtls_x509_crl *ca_crl;
6345
6346#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6347 if( ssl->handshake->sni_ca_chain != NULL )
6348 {
6349 ca_chain = ssl->handshake->sni_ca_chain;
6350 ca_crl = ssl->handshake->sni_ca_crl;
6351 }
6352 else
6353#endif
6354 {
6355 ca_chain = ssl->conf->ca_chain;
6356 ca_crl = ssl->conf->ca_crl;
6357 }
6358
6359 if( ca_chain != NULL )
6360 have_ca_chain = 1;
6361
6362 ret = mbedtls_x509_crt_verify_restartable(
6363 chain,
6364 ca_chain, ca_crl,
6365 ssl->conf->cert_profile,
6366 ssl->hostname,
6367 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006368 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006369 }
Hanno Becker68636192019-02-05 14:36:34 +00006370
6371 if( ret != 0 )
6372 {
6373 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6374 }
6375
6376#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6377 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6378 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6379#endif
6380
6381 /*
6382 * Secondary checks: always done, but change 'ret' only if it was 0
6383 */
6384
6385#if defined(MBEDTLS_ECP_C)
6386 {
6387 const mbedtls_pk_context *pk = &chain->pk;
6388
6389 /* If certificate uses an EC key, make sure the curve is OK */
6390 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6391 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6392 {
6393 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6394
6395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6396 if( ret == 0 )
6397 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6398 }
6399 }
6400#endif /* MBEDTLS_ECP_C */
6401
6402 if( mbedtls_ssl_check_cert_usage( chain,
6403 ciphersuite_info,
6404 ! ssl->conf->endpoint,
6405 &ssl->session_negotiate->verify_result ) != 0 )
6406 {
6407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6408 if( ret == 0 )
6409 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6410 }
6411
6412 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6413 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6414 * with details encoded in the verification flags. All other kinds
6415 * of error codes, including those from the user provided f_vrfy
6416 * functions, are treated as fatal and lead to a failure of
6417 * ssl_parse_certificate even if verification was optional. */
6418 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6419 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6420 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6421 {
6422 ret = 0;
6423 }
6424
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006425 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006426 {
6427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6428 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6429 }
6430
6431 if( ret != 0 )
6432 {
6433 uint8_t alert;
6434
6435 /* The certificate may have been rejected for several reasons.
6436 Pick one and send the corresponding alert. Which alert to send
6437 may be a subject of debate in some cases. */
6438 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6439 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6440 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6441 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6442 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6443 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6444 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6445 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6446 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6447 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6448 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6449 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6450 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6451 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6452 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6453 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6454 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6455 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6456 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6457 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6458 else
6459 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 alert );
6462 }
6463
6464#if defined(MBEDTLS_DEBUG_C)
6465 if( ssl->session_negotiate->verify_result != 0 )
6466 {
6467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6468 ssl->session_negotiate->verify_result ) );
6469 }
6470 else
6471 {
6472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6473 }
6474#endif /* MBEDTLS_DEBUG_C */
6475
6476 return( ret );
6477}
6478
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006479#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6480static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6481 unsigned char *start, size_t len )
6482{
6483 int ret;
6484 /* Remember digest of the peer's end-CRT. */
6485 ssl->session_negotiate->peer_cert_digest =
6486 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6487 if( ssl->session_negotiate->peer_cert_digest == NULL )
6488 {
6489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6490 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6491 mbedtls_ssl_send_alert_message( ssl,
6492 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6493 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6494
6495 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6496 }
6497
6498 ret = mbedtls_md( mbedtls_md_info_from_type(
6499 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6500 start, len,
6501 ssl->session_negotiate->peer_cert_digest );
6502
6503 ssl->session_negotiate->peer_cert_digest_type =
6504 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6505 ssl->session_negotiate->peer_cert_digest_len =
6506 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6507
6508 return( ret );
6509}
6510
6511static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6512 unsigned char *start, size_t len )
6513{
6514 unsigned char *end = start + len;
6515 int ret;
6516
6517 /* Make a copy of the peer's raw public key. */
6518 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6519 ret = mbedtls_pk_parse_subpubkey( &start, end,
6520 &ssl->handshake->peer_pubkey );
6521 if( ret != 0 )
6522 {
6523 /* We should have parsed the public key before. */
6524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6525 }
6526
6527 return( 0 );
6528}
6529#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6530
Hanno Becker68636192019-02-05 14:36:34 +00006531int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6532{
6533 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006534 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006535#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6536 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6537 ? ssl->handshake->sni_authmode
6538 : ssl->conf->authmode;
6539#else
6540 const int authmode = ssl->conf->authmode;
6541#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006542 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006543 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006544
6545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6546
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006547 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6548 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006549 {
6550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006551 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006552 }
6553
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006554#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6555 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006556 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006557 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006558 chain = ssl->handshake->ecrs_peer_cert;
6559 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006560 goto crt_verify;
6561 }
6562#endif
6563
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006564 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006565 {
6566 /* mbedtls_ssl_read_record may have sent an alert already. We
6567 let it decide whether to alert. */
6568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006569 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006570 }
6571
Hanno Becker4a55f632019-02-05 12:49:06 +00006572#if defined(MBEDTLS_SSL_SRV_C)
6573 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6574 {
6575 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006576
6577 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006578 ret = 0;
6579 else
6580 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006581
Hanno Becker6bdfab22019-02-05 13:11:17 +00006582 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006583 }
6584#endif /* MBEDTLS_SSL_SRV_C */
6585
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006586 /* Clear existing peer CRT structure in case we tried to
6587 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006588 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006589
6590 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6591 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006592 {
6593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6594 sizeof( mbedtls_x509_crt ) ) );
6595 mbedtls_ssl_send_alert_message( ssl,
6596 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6597 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006598
Hanno Becker3dad3112019-02-05 17:19:52 +00006599 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6600 goto exit;
6601 }
6602 mbedtls_x509_crt_init( chain );
6603
6604 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006605 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006606 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006607
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006608#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6609 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006610 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006611
6612crt_verify:
6613 if( ssl->handshake->ecrs_enabled)
6614 rs_ctx = &ssl->handshake->ecrs_ctx;
6615#endif
6616
Hanno Becker68636192019-02-05 14:36:34 +00006617 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006618 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006619 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006620 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006621
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006622#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006623 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006624 unsigned char *crt_start, *pk_start;
6625 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006626
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006627 /* We parse the CRT chain without copying, so
6628 * these pointers point into the input buffer,
6629 * and are hence still valid after freeing the
6630 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006631
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006632 crt_start = chain->raw.p;
6633 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006634
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006635 pk_start = chain->pk_raw.p;
6636 pk_len = chain->pk_raw.len;
6637
6638 /* Free the CRT structures before computing
6639 * digest and copying the peer's public key. */
6640 mbedtls_x509_crt_free( chain );
6641 mbedtls_free( chain );
6642 chain = NULL;
6643
6644 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006645 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006646 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006647
6648 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6649 if( ret != 0 )
6650 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006651 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006652#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6653 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006654 ssl->session_negotiate->peer_cert = chain;
6655 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006656#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Hanno Becker6bdfab22019-02-05 13:11:17 +00006660exit:
6661
Hanno Becker3dad3112019-02-05 17:19:52 +00006662 if( ret == 0 )
6663 ssl->state++;
6664
6665#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6666 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6667 {
6668 ssl->handshake->ecrs_peer_cert = chain;
6669 chain = NULL;
6670 }
6671#endif
6672
6673 if( chain != NULL )
6674 {
6675 mbedtls_x509_crt_free( chain );
6676 mbedtls_free( chain );
6677 }
6678
Paul Bakker5121ce52009-01-03 21:22:43 +00006679 return( ret );
6680}
Hanno Becker21489932019-02-05 13:20:55 +00006681#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006684{
6685 int ret;
6686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 ssl->out_msglen = 1;
6691 ssl->out_msg[0] = 1;
6692
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 ssl->state++;
6694
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006695 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006696 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 return( ret );
6699 }
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006702
6703 return( 0 );
6704}
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006707{
6708 int ret;
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006711
Hanno Becker327c93b2018-08-15 13:56:18 +01006712 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 return( ret );
6716 }
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006721 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6722 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006724 }
6725
Hanno Beckere678eaa2018-08-21 14:57:46 +01006726 /* CCS records are only accepted if they have length 1 and content '1',
6727 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006728
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006729 /*
6730 * Switch to our negotiated transform and session parameters for inbound
6731 * data.
6732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006734 ssl->transform_in = ssl->transform_negotiate;
6735 ssl->session_in = ssl->session_negotiate;
6736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006741 ssl_dtls_replay_reset( ssl );
6742#endif
6743
6744 /* Increment epoch */
6745 if( ++ssl->in_epoch == 0 )
6746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006748 /* This is highly unlikely to happen for legitimate reasons, so
6749 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006751 }
6752 }
6753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006755 memset( ssl->in_ctr, 0, 8 );
6756
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006757 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6760 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006765 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6766 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006768 }
6769 }
6770#endif
6771
Paul Bakker5121ce52009-01-03 21:22:43 +00006772 ssl->state++;
6773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775
6776 return( 0 );
6777}
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006781{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006782 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6785 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6786 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006787 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006788 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6791#if defined(MBEDTLS_SHA512_C)
6792 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006793 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6794 else
6795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#if defined(MBEDTLS_SHA256_C)
6797 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006798 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006799 else
6800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006804 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006805 }
Paul Bakker380da532012-04-18 16:10:25 +00006806}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006809{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6811 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006812 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6813 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6816#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006818 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006819 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6820#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006821 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006822#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006823#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006825#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006826 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006827 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006828#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006829 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006830#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006833}
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006836 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6839 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006840 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6841 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6844#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006845#if defined(MBEDTLS_USE_PSA_CRYPTO)
6846 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6847#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006848 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006849#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006852#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006853 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006854#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006855 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006856#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006859}
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6863static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006864 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006865{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006866 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6867 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006868}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006869#endif
Paul Bakker380da532012-04-18 16:10:25 +00006870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6872#if defined(MBEDTLS_SHA256_C)
6873static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006874 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006875{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006876#if defined(MBEDTLS_USE_PSA_CRYPTO)
6877 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6878#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006879 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006880#endif
Paul Bakker380da532012-04-18 16:10:25 +00006881}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006882#endif
Paul Bakker380da532012-04-18 16:10:25 +00006883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#if defined(MBEDTLS_SHA512_C)
6885static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006886 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006887{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006888#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006889 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006890#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006891 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006892#endif
Paul Bakker380da532012-04-18 16:10:25 +00006893}
Paul Bakker769075d2012-11-24 11:26:46 +01006894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006898static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006901 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006902 mbedtls_md5_context md5;
6903 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904
Paul Bakker5121ce52009-01-03 21:22:43 +00006905 unsigned char padbuf[48];
6906 unsigned char md5sum[16];
6907 unsigned char sha1sum[20];
6908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006910 if( !session )
6911 session = ssl->session;
6912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006915 mbedtls_md5_init( &md5 );
6916 mbedtls_sha1_init( &sha1 );
6917
6918 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6919 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006920
6921 /*
6922 * SSLv3:
6923 * hash =
6924 * MD5( master + pad2 +
6925 * MD5( handshake + sender + master + pad1 ) )
6926 * + SHA1( master + pad2 +
6927 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006928 */
6929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006931 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6932 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006933#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006936 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6937 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006941 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006942
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006944
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006945 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6946 mbedtls_md5_update_ret( &md5, session->master, 48 );
6947 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6948 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006950 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6951 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6952 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6953 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954
Paul Bakker1ef83d62012-04-11 12:09:53 +00006955 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006957 mbedtls_md5_starts_ret( &md5 );
6958 mbedtls_md5_update_ret( &md5, session->master, 48 );
6959 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6960 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6961 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006962
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006963 mbedtls_sha1_starts_ret( &sha1 );
6964 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6965 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6966 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6967 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006971 mbedtls_md5_free( &md5 );
6972 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006974 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6975 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6976 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006983static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006987 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006988 mbedtls_md5_context md5;
6989 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006990 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006993 if( !session )
6994 session = ssl->session;
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006998 mbedtls_md5_init( &md5 );
6999 mbedtls_sha1_init( &sha1 );
7000
7001 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7002 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004 /*
7005 * TLSv1:
7006 * hash = PRF( master, finished_label,
7007 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7008 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007011 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7012 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007013#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007016 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7017 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007018#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007021 ? "client finished"
7022 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007023
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007024 mbedtls_md5_finish_ret( &md5, padbuf );
7025 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007026
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007027 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007028 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007031
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007032 mbedtls_md5_free( &md5 );
7033 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007034
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007035 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7042#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007043static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007045{
7046 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007047 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007048 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007049#if defined(MBEDTLS_USE_PSA_CRYPTO)
7050 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007051 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007052 psa_status_t status;
7053#else
7054 mbedtls_sha256_context sha256;
7055#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007058 if( !session )
7059 session = ssl->session;
7060
Andrzej Kurekeb342242019-01-29 09:14:33 -05007061 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7062 ? "client finished"
7063 : "server finished";
7064
7065#if defined(MBEDTLS_USE_PSA_CRYPTO)
7066 sha256_psa = psa_hash_operation_init();
7067
7068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7069
7070 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7071 if( status != PSA_SUCCESS )
7072 {
7073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7074 return;
7075 }
7076
7077 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7078 if( status != PSA_SUCCESS )
7079 {
7080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7081 return;
7082 }
7083 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7084#else
7085
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007086 mbedtls_sha256_init( &sha256 );
7087
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007089
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007090 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007091
7092 /*
7093 * TLSv1.2:
7094 * hash = PRF( master, finished_label,
7095 * Hash( handshake ) )[0.11]
7096 */
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if !defined(MBEDTLS_SHA256_ALT)
7099 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007100 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007101#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007103 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104 mbedtls_sha256_free( &sha256 );
7105#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007107 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007108 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007112 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007119static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007121{
7122 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007123 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007124 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007125#if defined(MBEDTLS_USE_PSA_CRYPTO)
7126 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007127 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007128 psa_status_t status;
7129#else
7130 mbedtls_sha512_context sha512;
7131#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007134 if( !session )
7135 session = ssl->session;
7136
Andrzej Kurekeb342242019-01-29 09:14:33 -05007137 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7138 ? "client finished"
7139 : "server finished";
7140
7141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007142 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007143
7144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7145
Andrzej Kurek972fba52019-01-30 03:29:12 -05007146 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007147 if( status != PSA_SUCCESS )
7148 {
7149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7150 return;
7151 }
7152
Andrzej Kurek972fba52019-01-30 03:29:12 -05007153 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007154 if( status != PSA_SUCCESS )
7155 {
7156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7157 return;
7158 }
7159 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7160#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007161 mbedtls_sha512_init( &sha512 );
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007164
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007165 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007166
7167 /*
7168 * TLSv1.2:
7169 * hash = PRF( master, finished_label,
7170 * Hash( handshake ) )[0.11]
7171 */
7172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007174 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7175 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007176#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007177
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007178 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007179 mbedtls_sha512_free( &sha512 );
7180#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007181
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007182 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007183 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007186
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007187 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#endif /* MBEDTLS_SHA512_C */
7192#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007197
7198 /*
7199 * Free our handshake params
7200 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007201 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007203 ssl->handshake = NULL;
7204
7205 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007206 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007207 */
7208 if( ssl->transform )
7209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 mbedtls_ssl_transform_free( ssl->transform );
7211 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007212 }
7213 ssl->transform = ssl->transform_negotiate;
7214 ssl->transform_negotiate = NULL;
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007217}
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007220{
7221 int resume = ssl->handshake->resume;
7222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225#if defined(MBEDTLS_SSL_RENEGOTIATION)
7226 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007229 ssl->renego_records_seen = 0;
7230 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007231#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007232
7233 /*
7234 * Free the previous session and switch in the current one
7235 */
Paul Bakker0a597072012-09-25 21:55:46 +00007236 if( ssl->session )
7237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007239 /* RFC 7366 3.1: keep the EtM state */
7240 ssl->session_negotiate->encrypt_then_mac =
7241 ssl->session->encrypt_then_mac;
7242#endif
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 mbedtls_ssl_session_free( ssl->session );
7245 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007246 }
7247 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007248 ssl->session_negotiate = NULL;
7249
Paul Bakker0a597072012-09-25 21:55:46 +00007250 /*
7251 * Add cache entry
7252 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007253 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007254 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007255 resume == 0 )
7256 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007257 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007259 }
Paul Bakker0a597072012-09-25 21:55:46 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007263 ssl->handshake->flight != NULL )
7264 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007265 /* Cancel handshake timer */
7266 ssl_set_timer( ssl, 0 );
7267
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007268 /* Keep last flight around in case we need to resend it:
7269 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007271 }
7272 else
7273#endif
7274 ssl_handshake_wrapup_free_hs_transform( ssl );
7275
Paul Bakker48916f92012-09-16 19:57:18 +00007276 ssl->state++;
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007279}
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007282{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007283 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007286
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007287 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007288
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007289 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007290
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007291 /*
7292 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7293 * may define some other value. Currently (early 2016), no defined
7294 * ciphersuite does this (and this is unlikely to change as activity has
7295 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007300 ssl->verify_data_len = hash_len;
7301 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007302#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007303
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7306 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007307
7308 /*
7309 * In case of session resuming, invert the client and server
7310 * ChangeCipherSpec messages order.
7311 */
Paul Bakker0a597072012-09-25 21:55:46 +00007312 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007315 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007319 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007322 }
7323 else
7324 ssl->state++;
7325
Paul Bakker48916f92012-09-16 19:57:18 +00007326 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007327 * Switch to our negotiated transform and session parameters for outbound
7328 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007334 {
7335 unsigned char i;
7336
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007337 /* Remember current epoch settings for resending */
7338 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007339 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007340
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007341 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007342 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007343
7344 /* Increment epoch */
7345 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007346 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007347 break;
7348
7349 /* The loop goes to its end iff the counter is wrapping */
7350 if( i == 0 )
7351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7353 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007354 }
7355 }
7356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007358 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007360 ssl->transform_out = ssl->transform_negotiate;
7361 ssl->session_out = ssl->session_negotiate;
7362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7364 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7369 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007370 }
7371 }
7372#endif
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007377#endif
7378
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007379 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007382 return( ret );
7383 }
7384
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007385#if defined(MBEDTLS_SSL_PROTO_DTLS)
7386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7387 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7388 {
7389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7390 return( ret );
7391 }
7392#endif
7393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
7396 return( 0 );
7397}
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007400#define SSL_MAX_HASH_LEN 36
7401#else
7402#define SSL_MAX_HASH_LEN 12
7403#endif
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007406{
Paul Bakker23986e52011-04-24 08:57:21 +00007407 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007408 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007409 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007412
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007413 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007414
Hanno Becker327c93b2018-08-15 13:56:18 +01007415 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 return( ret );
7419 }
7420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007424 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7425 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007427 }
7428
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007429 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_PROTO_SSL3)
7431 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007432 hash_len = 36;
7433 else
7434#endif
7435 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7438 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007441 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7442 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444 }
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007447 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007450 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7451 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007453 }
7454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007456 ssl->verify_data_len = hash_len;
7457 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007458#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007459
Paul Bakker0a597072012-09-25 21:55:46 +00007460 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007463 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007470 }
7471 else
7472 ssl->state++;
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007475 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007477#endif
7478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480
7481 return( 0 );
7482}
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7489 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7490 mbedtls_md5_init( &handshake->fin_md5 );
7491 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007492 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7493 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7496#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007497#if defined(MBEDTLS_USE_PSA_CRYPTO)
7498 handshake->fin_sha256_psa = psa_hash_operation_init();
7499 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7500#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007502 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007503#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007506#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007507 handshake->fin_sha384_psa = psa_hash_operation_init();
7508 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007509#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007511 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007512#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007515
7516 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007517
7518#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7519 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7520 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7521#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#if defined(MBEDTLS_DHM_C)
7524 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_ECDH_C)
7527 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007528#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007529#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007530 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007531#if defined(MBEDTLS_SSL_CLI_C)
7532 handshake->ecjpake_cache = NULL;
7533 handshake->ecjpake_cache_len = 0;
7534#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007535#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007536
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007537#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007538 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007539#endif
7540
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007541#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7542 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7543#endif
Hanno Becker75173122019-02-06 16:18:31 +00007544
7545#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7546 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7547 mbedtls_pk_init( &handshake->peer_pubkey );
7548#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007549}
7550
Hanno Beckera18d1322018-01-03 14:27:32 +00007551void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7556 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007557
Hanno Beckerd56ed242018-01-03 15:32:51 +00007558#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 mbedtls_md_init( &transform->md_ctx_enc );
7560 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007561#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007562}
7563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007567}
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007570{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007571 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007572 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007574 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007576 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007577 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007578
7579 /*
7580 * Either the pointers are now NULL or cleared properly and can be freed.
7581 * Now allocate missing structures.
7582 */
7583 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007584 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007585 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007586 }
Paul Bakker48916f92012-09-16 19:57:18 +00007587
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007588 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007589 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007590 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007591 }
Paul Bakker48916f92012-09-16 19:57:18 +00007592
Paul Bakker82788fb2014-10-20 13:59:19 +02007593 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007594 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007595 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007596 }
Paul Bakker48916f92012-09-16 19:57:18 +00007597
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007598 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007599 if( ssl->handshake == NULL ||
7600 ssl->transform_negotiate == NULL ||
7601 ssl->session_negotiate == NULL )
7602 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 mbedtls_free( ssl->handshake );
7606 mbedtls_free( ssl->transform_negotiate );
7607 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007608
7609 ssl->handshake = NULL;
7610 ssl->transform_negotiate = NULL;
7611 ssl->session_negotiate = NULL;
7612
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007613 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007614 }
7615
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007616 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007618 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007619 ssl_handshake_params_init( ssl->handshake );
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7623 {
7624 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007625
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007626 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7628 else
7629 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007630
7631 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007632 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007633#endif
7634
Paul Bakker48916f92012-09-16 19:57:18 +00007635 return( 0 );
7636}
7637
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007638#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007639/* Dummy cookie callbacks for defaults */
7640static int ssl_cookie_write_dummy( void *ctx,
7641 unsigned char **p, unsigned char *end,
7642 const unsigned char *cli_id, size_t cli_id_len )
7643{
7644 ((void) ctx);
7645 ((void) p);
7646 ((void) end);
7647 ((void) cli_id);
7648 ((void) cli_id_len);
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007651}
7652
7653static int ssl_cookie_check_dummy( void *ctx,
7654 const unsigned char *cookie, size_t cookie_len,
7655 const unsigned char *cli_id, size_t cli_id_len )
7656{
7657 ((void) ctx);
7658 ((void) cookie);
7659 ((void) cookie_len);
7660 ((void) cli_id);
7661 ((void) cli_id_len);
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007664}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007665#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007666
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007667/* Once ssl->out_hdr as the address of the beginning of the
7668 * next outgoing record is set, deduce the other pointers.
7669 *
7670 * Note: For TLS, we save the implicit record sequence number
7671 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7672 * and the caller has to make sure there's space for this.
7673 */
7674
7675static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7676 mbedtls_ssl_transform *transform )
7677{
7678#if defined(MBEDTLS_SSL_PROTO_DTLS)
7679 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7680 {
7681 ssl->out_ctr = ssl->out_hdr + 3;
7682 ssl->out_len = ssl->out_hdr + 11;
7683 ssl->out_iv = ssl->out_hdr + 13;
7684 }
7685 else
7686#endif
7687 {
7688 ssl->out_ctr = ssl->out_hdr - 8;
7689 ssl->out_len = ssl->out_hdr + 3;
7690 ssl->out_iv = ssl->out_hdr + 5;
7691 }
7692
7693 /* Adjust out_msg to make space for explicit IV, if used. */
7694 if( transform != NULL &&
7695 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7696 {
7697 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7698 }
7699 else
7700 ssl->out_msg = ssl->out_iv;
7701}
7702
7703/* Once ssl->in_hdr as the address of the beginning of the
7704 * next incoming record is set, deduce the other pointers.
7705 *
7706 * Note: For TLS, we save the implicit record sequence number
7707 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7708 * and the caller has to make sure there's space for this.
7709 */
7710
7711static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7712 mbedtls_ssl_transform *transform )
7713{
7714#if defined(MBEDTLS_SSL_PROTO_DTLS)
7715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7716 {
7717 ssl->in_ctr = ssl->in_hdr + 3;
7718 ssl->in_len = ssl->in_hdr + 11;
7719 ssl->in_iv = ssl->in_hdr + 13;
7720 }
7721 else
7722#endif
7723 {
7724 ssl->in_ctr = ssl->in_hdr - 8;
7725 ssl->in_len = ssl->in_hdr + 3;
7726 ssl->in_iv = ssl->in_hdr + 5;
7727 }
7728
7729 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7730 if( transform != NULL &&
7731 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7732 {
7733 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7734 }
7735 else
7736 ssl->in_msg = ssl->in_iv;
7737}
7738
Paul Bakker5121ce52009-01-03 21:22:43 +00007739/*
7740 * Initialize an SSL context
7741 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007742void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7743{
7744 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7745}
7746
7747/*
7748 * Setup an SSL context
7749 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007750
7751static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7752{
7753 /* Set the incoming and outgoing record pointers. */
7754#if defined(MBEDTLS_SSL_PROTO_DTLS)
7755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7756 {
7757 ssl->out_hdr = ssl->out_buf;
7758 ssl->in_hdr = ssl->in_buf;
7759 }
7760 else
7761#endif /* MBEDTLS_SSL_PROTO_DTLS */
7762 {
7763 ssl->out_hdr = ssl->out_buf + 8;
7764 ssl->in_hdr = ssl->in_buf + 8;
7765 }
7766
7767 /* Derive other internal pointers. */
7768 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7769 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7770}
7771
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007772int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007773 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007774{
Paul Bakker48916f92012-09-16 19:57:18 +00007775 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007776
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007777 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007778
7779 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007780 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007781 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007782
7783 /* Set to NULL in case of an error condition */
7784 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007785
Angus Grattond8213d02016-05-25 20:56:48 +10007786 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7787 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 {
Angus Grattond8213d02016-05-25 20:56:48 +10007789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007790 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007791 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007792 }
7793
7794 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7795 if( ssl->out_buf == NULL )
7796 {
7797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007798 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007799 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007800 }
7801
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007802 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007803
Paul Bakker48916f92012-09-16 19:57:18 +00007804 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007805 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
7807 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007808
7809error:
7810 mbedtls_free( ssl->in_buf );
7811 mbedtls_free( ssl->out_buf );
7812
7813 ssl->conf = NULL;
7814
7815 ssl->in_buf = NULL;
7816 ssl->out_buf = NULL;
7817
7818 ssl->in_hdr = NULL;
7819 ssl->in_ctr = NULL;
7820 ssl->in_len = NULL;
7821 ssl->in_iv = NULL;
7822 ssl->in_msg = NULL;
7823
7824 ssl->out_hdr = NULL;
7825 ssl->out_ctr = NULL;
7826 ssl->out_len = NULL;
7827 ssl->out_iv = NULL;
7828 ssl->out_msg = NULL;
7829
k-stachowiak9f7798e2018-07-31 16:52:32 +02007830 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007831}
7832
7833/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007834 * Reset an initialized and used SSL context for re-use while retaining
7835 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007836 *
7837 * If partial is non-zero, keep data in the input buffer and client ID.
7838 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007839 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007840static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007841{
Paul Bakker48916f92012-09-16 19:57:18 +00007842 int ret;
7843
Hanno Becker7e772132018-08-10 12:38:21 +01007844#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7845 !defined(MBEDTLS_SSL_SRV_C)
7846 ((void) partial);
7847#endif
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007850
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007851 /* Cancel any possibly running timer */
7852 ssl_set_timer( ssl, 0 );
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854#if defined(MBEDTLS_SSL_RENEGOTIATION)
7855 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007856 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007857
7858 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7860 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007863
Paul Bakker7eb013f2011-10-06 12:37:39 +00007864 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007865 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007866
7867 ssl->in_msgtype = 0;
7868 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007870 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007871 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007874 ssl_dtls_replay_reset( ssl );
7875#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007876
7877 ssl->in_hslen = 0;
7878 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007879
7880 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007881
7882 ssl->out_msgtype = 0;
7883 ssl->out_msglen = 0;
7884 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7886 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007887 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007888#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007889
Hanno Becker19859472018-08-06 09:40:20 +01007890 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7891
Paul Bakker48916f92012-09-16 19:57:18 +00007892 ssl->transform_in = NULL;
7893 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007894
Hanno Becker78640902018-08-13 16:35:15 +01007895 ssl->session_in = NULL;
7896 ssl->session_out = NULL;
7897
Angus Grattond8213d02016-05-25 20:56:48 +10007898 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007899
7900#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007901 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007902#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7903 {
7904 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007905 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007906 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7909 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7912 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7915 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007916 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007917 }
7918#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007919
Paul Bakker48916f92012-09-16 19:57:18 +00007920 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 mbedtls_ssl_transform_free( ssl->transform );
7923 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007924 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007925 }
Paul Bakker48916f92012-09-16 19:57:18 +00007926
Paul Bakkerc0463502013-02-14 11:19:38 +01007927 if( ssl->session )
7928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 mbedtls_ssl_session_free( ssl->session );
7930 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007931 ssl->session = NULL;
7932 }
7933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007935 ssl->alpn_chosen = NULL;
7936#endif
7937
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007938#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007939#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007940 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007941#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007942 {
7943 mbedtls_free( ssl->cli_id );
7944 ssl->cli_id = NULL;
7945 ssl->cli_id_len = 0;
7946 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007947#endif
7948
Paul Bakker48916f92012-09-16 19:57:18 +00007949 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7950 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007951
7952 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007953}
7954
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007955/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007956 * Reset an initialized and used SSL context for re-use while retaining
7957 * all application-set variables, function pointers and data.
7958 */
7959int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7960{
7961 return( ssl_session_reset_int( ssl, 0 ) );
7962}
7963
7964/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007965 * SSL set accessors
7966 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007967void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007969 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007970}
7971
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007972void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007973{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007974 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007975}
7976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007978void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007980 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007981}
7982#endif
7983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007985void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007986{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007987 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007988}
7989#endif
7990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007992
Hanno Becker1841b0a2018-08-24 11:13:57 +01007993void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7994 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007995{
7996 ssl->disable_datagram_packing = !allow_packing;
7997}
7998
7999void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8000 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008002 conf->hs_timeout_min = min;
8003 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008004}
8005#endif
8006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008007void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008008{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008009 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008010}
8011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008013void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008014 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008015 void *p_vrfy )
8016{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008017 conf->f_vrfy = f_vrfy;
8018 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008021
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008022void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008023 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 void *p_rng )
8025{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008026 conf->f_rng = f_rng;
8027 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008028}
8029
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008030void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008031 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008032 void *p_dbg )
8033{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008034 conf->f_dbg = f_dbg;
8035 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008036}
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008039 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008040 mbedtls_ssl_send_t *f_send,
8041 mbedtls_ssl_recv_t *f_recv,
8042 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008043{
8044 ssl->p_bio = p_bio;
8045 ssl->f_send = f_send;
8046 ssl->f_recv = f_recv;
8047 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008048}
8049
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008050#if defined(MBEDTLS_SSL_PROTO_DTLS)
8051void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8052{
8053 ssl->mtu = mtu;
8054}
8055#endif
8056
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008058{
8059 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008060}
8061
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008062void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8063 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008064 mbedtls_ssl_set_timer_t *f_set_timer,
8065 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008066{
8067 ssl->p_timer = p_timer;
8068 ssl->f_set_timer = f_set_timer;
8069 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008070
8071 /* Make sure we start with no timer running */
8072 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008073}
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008076void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008077 void *p_cache,
8078 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8079 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008081 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008082 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008083 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087#if defined(MBEDTLS_SSL_CLI_C)
8088int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008089{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008090 int ret;
8091
8092 if( ssl == NULL ||
8093 session == NULL ||
8094 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008095 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008098 }
8099
Hanno Becker52055ae2019-02-06 14:30:46 +00008100 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8101 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008102 return( ret );
8103
Paul Bakker0a597072012-09-25 21:55:46 +00008104 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008105
8106 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008111 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008113 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8114 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8115 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8116 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008117}
8118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008119void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008120 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008121 int major, int minor )
8122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008124 return;
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008127 return;
8128
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008129 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008130}
8131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008133void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008134 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008135{
8136 conf->cert_profile = profile;
8137}
8138
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008139/* Append a new keycert entry to a (possibly empty) list */
8140static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8141 mbedtls_x509_crt *cert,
8142 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008143{
niisato8ee24222018-06-25 19:05:48 +09008144 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008145
niisato8ee24222018-06-25 19:05:48 +09008146 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8147 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008148 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008149
niisato8ee24222018-06-25 19:05:48 +09008150 new_cert->cert = cert;
8151 new_cert->key = key;
8152 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008153
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008154 /* Update head is the list was null, else add to the end */
8155 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008156 {
niisato8ee24222018-06-25 19:05:48 +09008157 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008158 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008159 else
8160 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008161 mbedtls_ssl_key_cert *cur = *head;
8162 while( cur->next != NULL )
8163 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008164 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008165 }
8166
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008167 return( 0 );
8168}
8169
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008170int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008171 mbedtls_x509_crt *own_cert,
8172 mbedtls_pk_context *pk_key )
8173{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008174 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008175}
8176
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008177void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008178 mbedtls_x509_crt *ca_chain,
8179 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008180{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008181 conf->ca_chain = ca_chain;
8182 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008183
8184#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8185 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8186 * cannot be used together. */
8187 conf->f_ca_cb = NULL;
8188 conf->p_ca_cb = NULL;
8189#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008190}
Hanno Becker5adaad92019-03-27 16:54:37 +00008191
8192#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8193void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008194 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008195 void *p_ca_cb )
8196{
8197 conf->f_ca_cb = f_ca_cb;
8198 conf->p_ca_cb = p_ca_cb;
8199
8200 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8201 * cannot be used together. */
8202 conf->ca_chain = NULL;
8203 conf->ca_crl = NULL;
8204}
8205#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008207
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008208#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8209int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8210 mbedtls_x509_crt *own_cert,
8211 mbedtls_pk_context *pk_key )
8212{
8213 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8214 own_cert, pk_key ) );
8215}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008216
8217void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8218 mbedtls_x509_crt *ca_chain,
8219 mbedtls_x509_crl *ca_crl )
8220{
8221 ssl->handshake->sni_ca_chain = ca_chain;
8222 ssl->handshake->sni_ca_crl = ca_crl;
8223}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008224
8225void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8226 int authmode )
8227{
8228 ssl->handshake->sni_authmode = authmode;
8229}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008230#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8231
Hanno Becker8927c832019-04-03 12:52:50 +01008232#if defined(MBEDTLS_X509_CRT_PARSE_C)
8233void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8234 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8235 void *p_vrfy )
8236{
8237 ssl->f_vrfy = f_vrfy;
8238 ssl->p_vrfy = p_vrfy;
8239}
8240#endif
8241
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008242#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008243/*
8244 * Set EC J-PAKE password for current handshake
8245 */
8246int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8247 const unsigned char *pw,
8248 size_t pw_len )
8249{
8250 mbedtls_ecjpake_role role;
8251
Janos Follath8eb64132016-06-03 15:40:57 +01008252 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8254
8255 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8256 role = MBEDTLS_ECJPAKE_SERVER;
8257 else
8258 role = MBEDTLS_ECJPAKE_CLIENT;
8259
8260 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8261 role,
8262 MBEDTLS_MD_SHA256,
8263 MBEDTLS_ECP_DP_SECP256R1,
8264 pw, pw_len ) );
8265}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008266#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008269
8270static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8271{
8272 /* Remove reference to existing PSK, if any. */
8273#if defined(MBEDTLS_USE_PSA_CRYPTO)
8274 if( conf->psk_opaque != 0 )
8275 {
8276 /* The maintenance of the PSK key slot is the
8277 * user's responsibility. */
8278 conf->psk_opaque = 0;
8279 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008280 /* This and the following branch should never
8281 * be taken simultaenously as we maintain the
8282 * invariant that raw and opaque PSKs are never
8283 * configured simultaneously. As a safeguard,
8284 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008285#endif /* MBEDTLS_USE_PSA_CRYPTO */
8286 if( conf->psk != NULL )
8287 {
8288 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8289
8290 mbedtls_free( conf->psk );
8291 conf->psk = NULL;
8292 conf->psk_len = 0;
8293 }
8294
8295 /* Remove reference to PSK identity, if any. */
8296 if( conf->psk_identity != NULL )
8297 {
8298 mbedtls_free( conf->psk_identity );
8299 conf->psk_identity = NULL;
8300 conf->psk_identity_len = 0;
8301 }
8302}
8303
Hanno Becker7390c712018-11-15 13:33:04 +00008304/* This function assumes that PSK identity in the SSL config is unset.
8305 * It checks that the provided identity is well-formed and attempts
8306 * to make a copy of it in the SSL config.
8307 * On failure, the PSK identity in the config remains unset. */
8308static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8309 unsigned char const *psk_identity,
8310 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008311{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008312 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008313 if( psk_identity == NULL ||
8314 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008315 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008316 {
8317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8318 }
8319
Hanno Becker7390c712018-11-15 13:33:04 +00008320 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8321 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008322 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008323
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008324 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008325 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008326
8327 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008328}
8329
Hanno Becker7390c712018-11-15 13:33:04 +00008330int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8331 const unsigned char *psk, size_t psk_len,
8332 const unsigned char *psk_identity, size_t psk_identity_len )
8333{
8334 int ret;
8335 /* Remove opaque/raw PSK + PSK Identity */
8336 ssl_conf_remove_psk( conf );
8337
8338 /* Check and set raw PSK */
8339 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8341 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8342 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8343 conf->psk_len = psk_len;
8344 memcpy( conf->psk, psk, conf->psk_len );
8345
8346 /* Check and set PSK Identity */
8347 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8348 if( ret != 0 )
8349 ssl_conf_remove_psk( conf );
8350
8351 return( ret );
8352}
8353
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008354static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8355{
8356#if defined(MBEDTLS_USE_PSA_CRYPTO)
8357 if( ssl->handshake->psk_opaque != 0 )
8358 {
8359 ssl->handshake->psk_opaque = 0;
8360 }
8361 else
8362#endif /* MBEDTLS_USE_PSA_CRYPTO */
8363 if( ssl->handshake->psk != NULL )
8364 {
8365 mbedtls_platform_zeroize( ssl->handshake->psk,
8366 ssl->handshake->psk_len );
8367 mbedtls_free( ssl->handshake->psk );
8368 ssl->handshake->psk_len = 0;
8369 }
8370}
8371
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008372int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8373 const unsigned char *psk, size_t psk_len )
8374{
8375 if( psk == NULL || ssl->handshake == NULL )
8376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8377
8378 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8380
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008381 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008382
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008383 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008385
8386 ssl->handshake->psk_len = psk_len;
8387 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8388
8389 return( 0 );
8390}
8391
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008392#if defined(MBEDTLS_USE_PSA_CRYPTO)
8393int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008394 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008395 const unsigned char *psk_identity,
8396 size_t psk_identity_len )
8397{
Hanno Becker7390c712018-11-15 13:33:04 +00008398 int ret;
8399 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008400 ssl_conf_remove_psk( conf );
8401
Hanno Becker7390c712018-11-15 13:33:04 +00008402 /* Check and set opaque PSK */
8403 if( psk_slot == 0 )
8404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008405 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008406
8407 /* Check and set PSK Identity */
8408 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8409 psk_identity_len );
8410 if( ret != 0 )
8411 ssl_conf_remove_psk( conf );
8412
8413 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008414}
8415
8416int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008417 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008418{
8419 if( psk_slot == 0 || ssl->handshake == NULL )
8420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8421
8422 ssl_remove_psk( ssl );
8423 ssl->handshake->psk_opaque = psk_slot;
8424 return( 0 );
8425}
8426#endif /* MBEDTLS_USE_PSA_CRYPTO */
8427
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008428void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008430 size_t),
8431 void *p_psk )
8432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->f_psk = f_psk;
8434 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008437
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008438#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008439
8440#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008441int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008442{
8443 int ret;
8444
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008445 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8446 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8447 {
8448 mbedtls_mpi_free( &conf->dhm_P );
8449 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008452
8453 return( 0 );
8454}
Hanno Becker470a8c42017-10-04 15:28:46 +01008455#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008456
Hanno Beckera90658f2017-10-04 15:29:08 +01008457int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8458 const unsigned char *dhm_P, size_t P_len,
8459 const unsigned char *dhm_G, size_t G_len )
8460{
8461 int ret;
8462
8463 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8464 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8465 {
8466 mbedtls_mpi_free( &conf->dhm_P );
8467 mbedtls_mpi_free( &conf->dhm_G );
8468 return( ret );
8469 }
8470
8471 return( 0 );
8472}
Paul Bakker5121ce52009-01-03 21:22:43 +00008473
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008474int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008475{
8476 int ret;
8477
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008478 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8479 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8480 {
8481 mbedtls_mpi_free( &conf->dhm_P );
8482 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008483 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008484 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008485
8486 return( 0 );
8487}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008488#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008489
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008490#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8491/*
8492 * Set the minimum length for Diffie-Hellman parameters
8493 */
8494void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8495 unsigned int bitlen )
8496{
8497 conf->dhm_min_bitlen = bitlen;
8498}
8499#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8500
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008501#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008502/*
8503 * Set allowed/preferred hashes for handshake signatures
8504 */
8505void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8506 const int *hashes )
8507{
8508 conf->sig_hashes = hashes;
8509}
Hanno Becker947194e2017-04-07 13:25:49 +01008510#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008511
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008512#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008513/*
8514 * Set the allowed elliptic curves
8515 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008517 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008518{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008519 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008520}
Hanno Becker947194e2017-04-07 13:25:49 +01008521#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008522
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008523#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008525{
Hanno Becker947194e2017-04-07 13:25:49 +01008526 /* Initialize to suppress unnecessary compiler warning */
8527 size_t hostname_len = 0;
8528
8529 /* Check if new hostname is valid before
8530 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008531 if( hostname != NULL )
8532 {
8533 hostname_len = strlen( hostname );
8534
8535 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8537 }
8538
8539 /* Now it's clear that we will overwrite the old hostname,
8540 * so we can free it safely */
8541
8542 if( ssl->hostname != NULL )
8543 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008544 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008545 mbedtls_free( ssl->hostname );
8546 }
8547
8548 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008549
Paul Bakker5121ce52009-01-03 21:22:43 +00008550 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008551 {
8552 ssl->hostname = NULL;
8553 }
8554 else
8555 {
8556 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008557 if( ssl->hostname == NULL )
8558 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008559
Hanno Becker947194e2017-04-07 13:25:49 +01008560 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008561
Hanno Becker947194e2017-04-07 13:25:49 +01008562 ssl->hostname[hostname_len] = '\0';
8563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008564
8565 return( 0 );
8566}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008567#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008568
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008569#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008570void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008572 const unsigned char *, size_t),
8573 void *p_sni )
8574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008575 conf->f_sni = f_sni;
8576 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008581int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008582{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008583 size_t cur_len, tot_len;
8584 const char **p;
8585
8586 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008587 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8588 * MUST NOT be truncated."
8589 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008590 */
8591 tot_len = 0;
8592 for( p = protos; *p != NULL; p++ )
8593 {
8594 cur_len = strlen( *p );
8595 tot_len += cur_len;
8596
8597 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008599 }
8600
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008601 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008602
8603 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008604}
8605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008607{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008608 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008611
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008612void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008614 conf->max_major_ver = major;
8615 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008616}
8617
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008618void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008619{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008620 conf->min_major_ver = major;
8621 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008622}
8623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008625void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008626{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008627 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008628}
8629#endif
8630
Janos Follath088ce432017-04-10 12:42:31 +01008631#if defined(MBEDTLS_SSL_SRV_C)
8632void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8633 char cert_req_ca_list )
8634{
8635 conf->cert_req_ca_list = cert_req_ca_list;
8636}
8637#endif
8638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008640void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008642 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008643}
8644#endif
8645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008647void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008649 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008650}
8651#endif
8652
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008653#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008654void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008655{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008656 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008657}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008658#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008661int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008662{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008664 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008667 }
8668
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008669 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008670
8671 return( 0 );
8672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008676void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008683void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008684{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008685 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008686}
8687#endif
8688
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008689void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008691 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008692}
8693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008695void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008696{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008697 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008698}
8699
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008700void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008701{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008702 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008703}
8704
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008705void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008706 const unsigned char period[8] )
8707{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008708 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008713#if defined(MBEDTLS_SSL_CLI_C)
8714void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008715{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008716 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008717}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008718#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008719
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008720#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008721void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8722 mbedtls_ssl_ticket_write_t *f_ticket_write,
8723 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8724 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008725{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008726 conf->f_ticket_write = f_ticket_write;
8727 conf->f_ticket_parse = f_ticket_parse;
8728 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008729}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008732
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008733#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8734void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8735 mbedtls_ssl_export_keys_t *f_export_keys,
8736 void *p_export_keys )
8737{
8738 conf->f_export_keys = f_export_keys;
8739 conf->p_export_keys = p_export_keys;
8740}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008741
8742void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8743 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8744 void *p_export_keys )
8745{
8746 conf->f_export_keys_ext = f_export_keys_ext;
8747 conf->p_export_keys = p_export_keys;
8748}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008749#endif
8750
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008751#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008752void mbedtls_ssl_conf_async_private_cb(
8753 mbedtls_ssl_config *conf,
8754 mbedtls_ssl_async_sign_t *f_async_sign,
8755 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8756 mbedtls_ssl_async_resume_t *f_async_resume,
8757 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008758 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008759{
8760 conf->f_async_sign_start = f_async_sign;
8761 conf->f_async_decrypt_start = f_async_decrypt;
8762 conf->f_async_resume = f_async_resume;
8763 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008764 conf->p_async_config_data = async_config_data;
8765}
8766
Gilles Peskine8f97af72018-04-26 11:46:10 +02008767void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8768{
8769 return( conf->p_async_config_data );
8770}
8771
Gilles Peskine1febfef2018-04-30 11:54:39 +02008772void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008773{
8774 if( ssl->handshake == NULL )
8775 return( NULL );
8776 else
8777 return( ssl->handshake->user_async_ctx );
8778}
8779
Gilles Peskine1febfef2018-04-30 11:54:39 +02008780void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008781 void *ctx )
8782{
8783 if( ssl->handshake != NULL )
8784 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008785}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008786#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008787
Paul Bakker5121ce52009-01-03 21:22:43 +00008788/*
8789 * SSL get accessors
8790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008792{
8793 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8794}
8795
Hanno Becker8b170a02017-10-10 11:51:19 +01008796int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8797{
8798 /*
8799 * Case A: We're currently holding back
8800 * a message for further processing.
8801 */
8802
8803 if( ssl->keep_current_message == 1 )
8804 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008806 return( 1 );
8807 }
8808
8809 /*
8810 * Case B: Further records are pending in the current datagram.
8811 */
8812
8813#if defined(MBEDTLS_SSL_PROTO_DTLS)
8814 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8815 ssl->in_left > ssl->next_record_offset )
8816 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008818 return( 1 );
8819 }
8820#endif /* MBEDTLS_SSL_PROTO_DTLS */
8821
8822 /*
8823 * Case C: A handshake message is being processed.
8824 */
8825
Hanno Becker8b170a02017-10-10 11:51:19 +01008826 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8827 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008829 return( 1 );
8830 }
8831
8832 /*
8833 * Case D: An application data message is being processed
8834 */
8835 if( ssl->in_offt != NULL )
8836 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008838 return( 1 );
8839 }
8840
8841 /*
8842 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008843 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008844 * we implement support for multiple alerts in single records.
8845 */
8846
8847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8848 return( 0 );
8849}
8850
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008851uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008852{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008853 if( ssl->session != NULL )
8854 return( ssl->session->verify_result );
8855
8856 if( ssl->session_negotiate != NULL )
8857 return( ssl->session_negotiate->verify_result );
8858
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008859 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008860}
8861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008863{
Paul Bakker926c8e42013-03-06 10:23:34 +01008864 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008865 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008868}
8869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008874 {
8875 switch( ssl->minor_ver )
8876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008878 return( "DTLSv1.0" );
8879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008881 return( "DTLSv1.2" );
8882
8883 default:
8884 return( "unknown (DTLS)" );
8885 }
8886 }
8887#endif
8888
Paul Bakker43ca69c2011-01-15 17:35:19 +00008889 switch( ssl->minor_ver )
8890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008892 return( "SSLv3.0" );
8893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008895 return( "TLSv1.0" );
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008898 return( "TLSv1.1" );
8899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008901 return( "TLSv1.2" );
8902
Paul Bakker43ca69c2011-01-15 17:35:19 +00008903 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008904 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008905 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008906}
8907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008909{
Hanno Becker3136ede2018-08-17 15:28:19 +01008910 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008912 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008913
Hanno Becker78640902018-08-13 16:35:15 +01008914 if( transform == NULL )
8915 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917#if defined(MBEDTLS_ZLIB_SUPPORT)
8918 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8919 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008920#endif
8921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924 case MBEDTLS_MODE_GCM:
8925 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008926 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008928 transform_expansion = transform->minlen;
8929 break;
8930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008932
8933 block_size = mbedtls_cipher_get_block_size(
8934 &transform->cipher_ctx_enc );
8935
Hanno Becker3136ede2018-08-17 15:28:19 +01008936 /* Expansion due to the addition of the MAC. */
8937 transform_expansion += transform->maclen;
8938
8939 /* Expansion due to the addition of CBC padding;
8940 * Theoretically up to 256 bytes, but we never use
8941 * more than the block size of the underlying cipher. */
8942 transform_expansion += block_size;
8943
8944 /* For TLS 1.1 or higher, an explicit IV is added
8945 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008946#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8947 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008948 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008949#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008950
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008951 break;
8952
8953 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008956 }
8957
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008958 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008959}
8960
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008961#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8962size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8963{
8964 size_t max_len;
8965
8966 /*
8967 * Assume mfl_code is correct since it was checked when set
8968 */
Angus Grattond8213d02016-05-25 20:56:48 +10008969 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008970
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008971 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008972 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008973 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008974 {
Angus Grattond8213d02016-05-25 20:56:48 +10008975 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008976 }
8977
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008978 /* During a handshake, use the value being negotiated */
8979 if( ssl->session_negotiate != NULL &&
8980 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8981 {
8982 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8983 }
8984
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008985 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008986}
8987#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8988
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008989#if defined(MBEDTLS_SSL_PROTO_DTLS)
8990static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8991{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008992 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8993 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8994 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8995 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8996 return ( 0 );
8997
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008998 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8999 return( ssl->mtu );
9000
9001 if( ssl->mtu == 0 )
9002 return( ssl->handshake->mtu );
9003
9004 return( ssl->mtu < ssl->handshake->mtu ?
9005 ssl->mtu : ssl->handshake->mtu );
9006}
9007#endif /* MBEDTLS_SSL_PROTO_DTLS */
9008
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009009int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9010{
9011 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9012
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009013#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9014 !defined(MBEDTLS_SSL_PROTO_DTLS)
9015 (void) ssl;
9016#endif
9017
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009018#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9019 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9020
9021 if( max_len > mfl )
9022 max_len = mfl;
9023#endif
9024
9025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009026 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009027 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009028 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009029 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9030 const size_t overhead = (size_t) ret;
9031
9032 if( ret < 0 )
9033 return( ret );
9034
9035 if( mtu <= overhead )
9036 {
9037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9038 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9039 }
9040
9041 if( max_len > mtu - overhead )
9042 max_len = mtu - overhead;
9043 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009044#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009045
Hanno Becker0defedb2018-08-10 12:35:02 +01009046#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9047 !defined(MBEDTLS_SSL_PROTO_DTLS)
9048 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009049#endif
9050
9051 return( (int) max_len );
9052}
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_X509_CRT_PARSE_C)
9055const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009056{
9057 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009058 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009059
Hanno Beckere6824572019-02-07 13:18:46 +00009060#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009061 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009062#else
9063 return( NULL );
9064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009069int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9070 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009071{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009072 if( ssl == NULL ||
9073 dst == NULL ||
9074 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009075 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009078 }
9079
Hanno Becker52055ae2019-02-06 14:30:46 +00009080 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009083
Paul Bakker5121ce52009-01-03 21:22:43 +00009084/*
Paul Bakker1961b702013-01-25 14:49:24 +01009085 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009090
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009091 if( ssl == NULL || ssl->conf == NULL )
9092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009095 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009101#endif
9102
Paul Bakker1961b702013-01-25 14:49:24 +01009103 return( ret );
9104}
9105
9106/*
9107 * Perform the SSL handshake
9108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009110{
9111 int ret = 0;
9112
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009113 if( ssl == NULL || ssl->conf == NULL )
9114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009121
9122 if( ret != 0 )
9123 break;
9124 }
9125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009127
9128 return( ret );
9129}
9130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131#if defined(MBEDTLS_SSL_RENEGOTIATION)
9132#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009133/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009134 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009137{
9138 int ret;
9139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009141
9142 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9144 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009145
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009146 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009147 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009149 return( ret );
9150 }
9151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009153
9154 return( 0 );
9155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009157
9158/*
9159 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160 * - any side: calling mbedtls_ssl_renegotiate(),
9161 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9162 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009163 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009164 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009168{
9169 int ret;
9170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009172
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009173 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9174 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009175
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009176 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9177 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009181 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009182 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009183 ssl->handshake->out_msg_seq = 1;
9184 else
9185 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009186 }
9187#endif
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9190 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009195 return( ret );
9196 }
9197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009199
9200 return( 0 );
9201}
9202
9203/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009204 * Renegotiate current connection on client,
9205 * or request renegotiation on server
9206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009210
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009211 if( ssl == NULL || ssl->conf == NULL )
9212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009215 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009222
9223 /* Did we already try/start sending HelloRequest? */
9224 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009226
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009227 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009232 /*
9233 * On client, either start the renegotiation process or,
9234 * if already in progress, continue the handshake
9235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009240
9241 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009244 return( ret );
9245 }
9246 }
9247 else
9248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009252 return( ret );
9253 }
9254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009256
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009257 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009258}
9259
9260/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009261 * Check record counters and renegotiate if they're above the limit.
9262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009264{
Andres AG2196c7f2016-12-15 17:01:16 +00009265 size_t ep_len = ssl_ep_len( ssl );
9266 int in_ctr_cmp;
9267 int out_ctr_cmp;
9268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9270 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009271 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009272 {
9273 return( 0 );
9274 }
9275
Andres AG2196c7f2016-12-15 17:01:16 +00009276 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9277 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009278 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009279 ssl->conf->renego_period + ep_len, 8 - ep_len );
9280
9281 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009282 {
9283 return( 0 );
9284 }
9285
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009290
9291/*
9292 * Receive application data decrypted from the SSL layer
9293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009295{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009296 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009297 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009298
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009299 if( ssl == NULL || ssl->conf == NULL )
9300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009308 return( ret );
9309
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009310 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009312 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009313 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009314 return( ret );
9315 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009316 }
9317#endif
9318
Hanno Becker4a810fb2017-05-24 16:27:30 +01009319 /*
9320 * Check if renegotiation is necessary and/or handshake is
9321 * in process. If yes, perform/continue, and fall through
9322 * if an unexpected packet is received while the client
9323 * is waiting for the ServerHello.
9324 *
9325 * (There is no equivalent to the last condition on
9326 * the server-side as it is not treated as within
9327 * a handshake while waiting for the ClientHello
9328 * after a renegotiation request.)
9329 */
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009332 ret = ssl_check_ctr_renegotiate( ssl );
9333 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9334 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009337 return( ret );
9338 }
9339#endif
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009344 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9345 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009348 return( ret );
9349 }
9350 }
9351
Hanno Beckere41158b2017-10-23 13:30:32 +01009352 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009353 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009354 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009355 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009356 if( ssl->f_get_timer != NULL &&
9357 ssl->f_get_timer( ssl->p_timer ) == -1 )
9358 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009359 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009360 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009361
Hanno Becker327c93b2018-08-15 13:56:18 +01009362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009363 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009364 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9365 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009366
Hanno Becker4a810fb2017-05-24 16:27:30 +01009367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9368 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009369 }
9370
9371 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009373 {
9374 /*
9375 * OpenSSL sends empty messages to randomize the IV
9376 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009380 return( 0 );
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009383 return( ret );
9384 }
9385 }
9386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009390
Hanno Becker4a810fb2017-05-24 16:27:30 +01009391 /*
9392 * - For client-side, expect SERVER_HELLO_REQUEST.
9393 * - For server-side, expect CLIENT_HELLO.
9394 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9395 */
9396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009400 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009403
9404 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009406 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009407 {
9408 continue;
9409 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009412 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009413#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009414
Hanno Becker4a810fb2017-05-24 16:27:30 +01009415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009420
9421 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009424 {
9425 continue;
9426 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009429 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009430#endif /* MBEDTLS_SSL_SRV_C */
9431
Hanno Becker21df7f92017-10-17 11:03:26 +01009432#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009433 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009434 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9435 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9436 ssl->conf->allow_legacy_renegotiation ==
9437 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9438 {
9439 /*
9440 * Accept renegotiation request
9441 */
Paul Bakker48916f92012-09-16 19:57:18 +00009442
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009443 /* DTLS clients need to know renego is server-initiated */
9444#if defined(MBEDTLS_SSL_PROTO_DTLS)
9445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9446 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9447 {
9448 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9449 }
9450#endif
9451 ret = ssl_start_renegotiation( ssl );
9452 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9453 ret != 0 )
9454 {
9455 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9456 return( ret );
9457 }
9458 }
9459 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009460#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009461 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009462 /*
9463 * Refuse renegotiation
9464 */
9465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#if defined(MBEDTLS_SSL_PROTO_SSL3)
9469 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009470 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009471 /* SSLv3 does not have a "no_renegotiation" warning, so
9472 we send a fatal alert and abort the connection. */
9473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9474 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9475 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009476 }
9477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9479#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9480 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9481 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9484 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9485 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009486 {
9487 return( ret );
9488 }
Paul Bakker48916f92012-09-16 19:57:18 +00009489 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009490 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9492 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009496 }
Paul Bakker48916f92012-09-16 19:57:18 +00009497 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009498
Hanno Becker90333da2017-10-10 11:27:13 +01009499 /* At this point, we don't know whether the renegotiation has been
9500 * completed or not. The cases to consider are the following:
9501 * 1) The renegotiation is complete. In this case, no new record
9502 * has been read yet.
9503 * 2) The renegotiation is incomplete because the client received
9504 * an application data record while awaiting the ServerHello.
9505 * 3) The renegotiation is incomplete because the client received
9506 * a non-handshake, non-application data message while awaiting
9507 * the ServerHello.
9508 * In each of these case, looping will be the proper action:
9509 * - For 1), the next iteration will read a new record and check
9510 * if it's application data.
9511 * - For 2), the loop condition isn't satisfied as application data
9512 * is present, hence continue is the same as break
9513 * - For 3), the loop condition is satisfied and read_record
9514 * will re-deliver the message that was held back by the client
9515 * when expecting the ServerHello.
9516 */
9517 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009518 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009519#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009521 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009522 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009523 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009524 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009527 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009529 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009530 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9535 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009538 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009539 }
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009545 }
9546
9547 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009548
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009549 /* We're going to return something now, cancel timer,
9550 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009552 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009553
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009555 /* If we requested renego but received AppData, resend HelloRequest.
9556 * Do it now, after setting in_offt, to avoid taking this branch
9557 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009561 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009562 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009565 return( ret );
9566 }
9567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009569#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009570 }
9571
9572 n = ( len < ssl->in_msglen )
9573 ? len : ssl->in_msglen;
9574
9575 memcpy( buf, ssl->in_offt, n );
9576 ssl->in_msglen -= n;
9577
9578 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009579 {
9580 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009581 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009582 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009584 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009585 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009586 /* more data available */
9587 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009591
Paul Bakker23986e52011-04-24 08:57:21 +00009592 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009593}
9594
9595/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009596 * Send application data to be encrypted by the SSL layer, taking care of max
9597 * fragment length and buffer size.
9598 *
9599 * According to RFC 5246 Section 6.2.1:
9600 *
9601 * Zero-length fragments of Application data MAY be sent as they are
9602 * potentially useful as a traffic analysis countermeasure.
9603 *
9604 * Therefore, it is possible that the input message length is 0 and the
9605 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009606 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009607static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009608 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009609{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009610 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9611 const size_t max_len = (size_t) ret;
9612
9613 if( ret < 0 )
9614 {
9615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9616 return( ret );
9617 }
9618
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009619 if( len > max_len )
9620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009625 "maximum fragment length: %d > %d",
9626 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009628 }
9629 else
9630#endif
9631 len = max_len;
9632 }
Paul Bakker887bd502011-06-08 13:10:54 +00009633
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 if( ssl->out_left != 0 )
9635 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009636 /*
9637 * The user has previously tried to send the data and
9638 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9639 * written. In this case, we expect the high-level write function
9640 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009645 return( ret );
9646 }
9647 }
Paul Bakker887bd502011-06-08 13:10:54 +00009648 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009649 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009650 /*
9651 * The user is trying to send a message the first time, so we need to
9652 * copy the data into the internal buffers and setup the data structure
9653 * to keep track of partial writes
9654 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009655 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009657 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009658
Hanno Becker67bc7c32018-08-06 11:33:50 +01009659 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009662 return( ret );
9663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009664 }
9665
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009666 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009667}
9668
9669/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009670 * Write application data, doing 1/n-1 splitting if necessary.
9671 *
9672 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009673 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009674 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009677static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009678 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009679{
9680 int ret;
9681
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009682 if( ssl->conf->cbc_record_splitting ==
9683 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009684 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9686 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9687 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009688 {
9689 return( ssl_write_real( ssl, buf, len ) );
9690 }
9691
9692 if( ssl->split_done == 0 )
9693 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009694 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009695 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009696 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009697 }
9698
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009699 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9700 return( ret );
9701 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009702
9703 return( ret + 1 );
9704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009706
9707/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009708 * Write application data (public-facing wrapper)
9709 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009710int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009711{
9712 int ret;
9713
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009715
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009716 if( ssl == NULL || ssl->conf == NULL )
9717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9718
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009720 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9721 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009723 return( ret );
9724 }
9725#endif
9726
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009727 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009728 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009729 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009730 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009732 return( ret );
9733 }
9734 }
9735
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009736#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009737 ret = ssl_write_split( ssl, buf, len );
9738#else
9739 ret = ssl_write_real( ssl, buf, len );
9740#endif
9741
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009743
9744 return( ret );
9745}
9746
9747/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 * Notify the peer that the connection is being closed
9749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009751{
9752 int ret;
9753
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009754 if( ssl == NULL || ssl->conf == NULL )
9755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009758
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009759 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9765 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9766 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009769 return( ret );
9770 }
9771 }
9772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009774
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009775 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009776}
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009779{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009780 if( transform == NULL )
9781 return;
9782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009784 deflateEnd( &transform->ctx_deflate );
9785 inflateEnd( &transform->ctx_inflate );
9786#endif
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9789 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009790
Hanno Beckerd56ed242018-01-03 15:32:51 +00009791#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 mbedtls_md_free( &transform->md_ctx_enc );
9793 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009794#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009795
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009796 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009797}
9798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#if defined(MBEDTLS_X509_CRT_PARSE_C)
9800static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009803
9804 while( cur != NULL )
9805 {
9806 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009808 cur = next;
9809 }
9810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009812
Hanno Becker0271f962018-08-16 13:23:47 +01009813#if defined(MBEDTLS_SSL_PROTO_DTLS)
9814
9815static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9816{
9817 unsigned offset;
9818 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9819
9820 if( hs == NULL )
9821 return;
9822
Hanno Becker283f5ef2018-08-24 09:34:47 +01009823 ssl_free_buffered_record( ssl );
9824
Hanno Becker0271f962018-08-16 13:23:47 +01009825 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009826 ssl_buffering_free_slot( ssl, offset );
9827}
9828
9829static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9830 uint8_t slot )
9831{
9832 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9833 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009834
9835 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9836 return;
9837
Hanno Beckere605b192018-08-21 15:59:07 +01009838 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009839 {
Hanno Beckere605b192018-08-21 15:59:07 +01009840 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009841 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009842 mbedtls_free( hs_buf->data );
9843 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009844 }
9845}
9846
9847#endif /* MBEDTLS_SSL_PROTO_DTLS */
9848
Gilles Peskine9b562d52018-04-25 20:32:43 +02009849void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009850{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009851 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9852
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009853 if( handshake == NULL )
9854 return;
9855
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009856#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9857 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9858 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009859 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009860 handshake->async_in_progress = 0;
9861 }
9862#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9863
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009864#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9865 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9866 mbedtls_md5_free( &handshake->fin_md5 );
9867 mbedtls_sha1_free( &handshake->fin_sha1 );
9868#endif
9869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9870#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009871#if defined(MBEDTLS_USE_PSA_CRYPTO)
9872 psa_hash_abort( &handshake->fin_sha256_psa );
9873#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009874 mbedtls_sha256_free( &handshake->fin_sha256 );
9875#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009876#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009877#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009878#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009879 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009880#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009881 mbedtls_sha512_free( &handshake->fin_sha512 );
9882#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009883#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886#if defined(MBEDTLS_DHM_C)
9887 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889#if defined(MBEDTLS_ECDH_C)
9890 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009891#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009892#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009893 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009894#if defined(MBEDTLS_SSL_CLI_C)
9895 mbedtls_free( handshake->ecjpake_cache );
9896 handshake->ecjpake_cache = NULL;
9897 handshake->ecjpake_cache_len = 0;
9898#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009899#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009900
Janos Follath4ae5c292016-02-10 11:27:43 +00009901#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9902 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009903 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009905#endif
9906
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009907#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9908 if( handshake->psk != NULL )
9909 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009910 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009911 mbedtls_free( handshake->psk );
9912 }
9913#endif
9914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9916 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009917 /*
9918 * Free only the linked list wrapper, not the keys themselves
9919 * since the belong to the SNI callback
9920 */
9921 if( handshake->sni_key_cert != NULL )
9922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009924
9925 while( cur != NULL )
9926 {
9927 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009929 cur = next;
9930 }
9931 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009933
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009934#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009935 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009936 if( handshake->ecrs_peer_cert != NULL )
9937 {
9938 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9939 mbedtls_free( handshake->ecrs_peer_cert );
9940 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009941#endif
9942
Hanno Becker75173122019-02-06 16:18:31 +00009943#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9944 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9945 mbedtls_pk_free( &handshake->peer_pubkey );
9946#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948#if defined(MBEDTLS_SSL_PROTO_DTLS)
9949 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009950 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009951 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009952#endif
9953
Hanno Becker4a63ed42019-01-08 11:39:35 +00009954#if defined(MBEDTLS_ECDH_C) && \
9955 defined(MBEDTLS_USE_PSA_CRYPTO)
9956 psa_destroy_key( handshake->ecdh_psa_privkey );
9957#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9958
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009959 mbedtls_platform_zeroize( handshake,
9960 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009961}
9962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009964{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009965 if( session == NULL )
9966 return;
9967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009969 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009970#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009971
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009972#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009974#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009975
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009976 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009977}
9978
Paul Bakker5121ce52009-01-03 21:22:43 +00009979/*
9980 * Free an SSL context
9981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009983{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009984 if( ssl == NULL )
9985 return;
9986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009988
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009989 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009990 {
Angus Grattond8213d02016-05-25 20:56:48 +10009991 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009993 }
9994
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009995 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009996 {
Angus Grattond8213d02016-05-25 20:56:48 +10009997 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009999 }
10000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010002 if( ssl->compress_buf != NULL )
10003 {
Angus Grattond8213d02016-05-25 20:56:48 +100010004 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010006 }
10007#endif
10008
Paul Bakker48916f92012-09-16 19:57:18 +000010009 if( ssl->transform )
10010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011 mbedtls_ssl_transform_free( ssl->transform );
10012 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010013 }
10014
10015 if( ssl->handshake )
10016 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010017 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10019 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021 mbedtls_free( ssl->handshake );
10022 mbedtls_free( ssl->transform_negotiate );
10023 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010024 }
10025
Paul Bakkerc0463502013-02-14 11:19:38 +010010026 if( ssl->session )
10027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 mbedtls_ssl_session_free( ssl->session );
10029 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010030 }
10031
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010032#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010033 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010034 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010035 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010037 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10041 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10044 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010045 }
10046#endif
10047
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010050#endif
10051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010053
Paul Bakker86f04f42013-02-14 11:20:09 +010010054 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010055 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010056}
10057
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010058/*
10059 * Initialze mbedtls_ssl_config
10060 */
10061void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10062{
10063 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10064}
10065
Simon Butcherc97b6972015-12-27 23:48:17 +000010066#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010067static int ssl_preset_default_hashes[] = {
10068#if defined(MBEDTLS_SHA512_C)
10069 MBEDTLS_MD_SHA512,
10070 MBEDTLS_MD_SHA384,
10071#endif
10072#if defined(MBEDTLS_SHA256_C)
10073 MBEDTLS_MD_SHA256,
10074 MBEDTLS_MD_SHA224,
10075#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010076#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010077 MBEDTLS_MD_SHA1,
10078#endif
10079 MBEDTLS_MD_NONE
10080};
Simon Butcherc97b6972015-12-27 23:48:17 +000010081#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010082
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010083static int ssl_preset_suiteb_ciphersuites[] = {
10084 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10085 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10086 0
10087};
10088
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010089#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010090static int ssl_preset_suiteb_hashes[] = {
10091 MBEDTLS_MD_SHA256,
10092 MBEDTLS_MD_SHA384,
10093 MBEDTLS_MD_NONE
10094};
10095#endif
10096
10097#if defined(MBEDTLS_ECP_C)
10098static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10099 MBEDTLS_ECP_DP_SECP256R1,
10100 MBEDTLS_ECP_DP_SECP384R1,
10101 MBEDTLS_ECP_DP_NONE
10102};
10103#endif
10104
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010105/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010106 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010107 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010108int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010109 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010110{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010111#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010112 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010113#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010114
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010115 /* Use the functions here so that they are covered in tests,
10116 * but otherwise access member directly for efficiency */
10117 mbedtls_ssl_conf_endpoint( conf, endpoint );
10118 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010119
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010120 /*
10121 * Things that are common to all presets
10122 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010123#if defined(MBEDTLS_SSL_CLI_C)
10124 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10125 {
10126 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10127#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10128 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10129#endif
10130 }
10131#endif
10132
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010133#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010134 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010135#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010136
10137#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10138 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10139#endif
10140
10141#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10142 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10143#endif
10144
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010145#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10146 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10147#endif
10148
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010149#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010150 conf->f_cookie_write = ssl_cookie_write_dummy;
10151 conf->f_cookie_check = ssl_cookie_check_dummy;
10152#endif
10153
10154#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10155 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10156#endif
10157
Janos Follath088ce432017-04-10 12:42:31 +010010158#if defined(MBEDTLS_SSL_SRV_C)
10159 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10160#endif
10161
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010162#if defined(MBEDTLS_SSL_PROTO_DTLS)
10163 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10164 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10165#endif
10166
10167#if defined(MBEDTLS_SSL_RENEGOTIATION)
10168 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010169 memset( conf->renego_period, 0x00, 2 );
10170 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010171#endif
10172
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010173#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10174 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10175 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010176 const unsigned char dhm_p[] =
10177 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10178 const unsigned char dhm_g[] =
10179 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10180
Hanno Beckera90658f2017-10-04 15:29:08 +010010181 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10182 dhm_p, sizeof( dhm_p ),
10183 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010184 {
10185 return( ret );
10186 }
10187 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010188#endif
10189
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010190 /*
10191 * Preset-specific defaults
10192 */
10193 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010194 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010195 /*
10196 * NSA Suite B
10197 */
10198 case MBEDTLS_SSL_PRESET_SUITEB:
10199 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10200 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10201 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10202 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10203
10204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10207 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10208 ssl_preset_suiteb_ciphersuites;
10209
10210#if defined(MBEDTLS_X509_CRT_PARSE_C)
10211 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010212#endif
10213
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010214#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010215 conf->sig_hashes = ssl_preset_suiteb_hashes;
10216#endif
10217
10218#if defined(MBEDTLS_ECP_C)
10219 conf->curve_list = ssl_preset_suiteb_curves;
10220#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010221 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010222
10223 /*
10224 * Default
10225 */
10226 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010227 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10228 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10229 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10230 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10231 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10232 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10233 MBEDTLS_SSL_MIN_MINOR_VERSION :
10234 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010235 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10236 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10237
10238#if defined(MBEDTLS_SSL_PROTO_DTLS)
10239 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10240 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10241#endif
10242
10243 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10244 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10245 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10246 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10247 mbedtls_ssl_list_ciphersuites();
10248
10249#if defined(MBEDTLS_X509_CRT_PARSE_C)
10250 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10251#endif
10252
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010253#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010254 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010255#endif
10256
10257#if defined(MBEDTLS_ECP_C)
10258 conf->curve_list = mbedtls_ecp_grp_id_list();
10259#endif
10260
10261#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10262 conf->dhm_min_bitlen = 1024;
10263#endif
10264 }
10265
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010266 return( 0 );
10267}
10268
10269/*
10270 * Free mbedtls_ssl_config
10271 */
10272void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10273{
10274#if defined(MBEDTLS_DHM_C)
10275 mbedtls_mpi_free( &conf->dhm_P );
10276 mbedtls_mpi_free( &conf->dhm_G );
10277#endif
10278
10279#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10280 if( conf->psk != NULL )
10281 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010282 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010283 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010284 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010285 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010286 }
10287
10288 if( conf->psk_identity != NULL )
10289 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010290 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010291 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010292 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010293 conf->psk_identity_len = 0;
10294 }
10295#endif
10296
10297#if defined(MBEDTLS_X509_CRT_PARSE_C)
10298 ssl_key_cert_free( conf->key_cert );
10299#endif
10300
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010301 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010302}
10303
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010304#if defined(MBEDTLS_PK_C) && \
10305 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010306/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311#if defined(MBEDTLS_RSA_C)
10312 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10313 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315#if defined(MBEDTLS_ECDSA_C)
10316 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10317 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010320}
10321
Hanno Becker7e5437a2017-04-28 17:15:26 +010010322unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10323{
10324 switch( type ) {
10325 case MBEDTLS_PK_RSA:
10326 return( MBEDTLS_SSL_SIG_RSA );
10327 case MBEDTLS_PK_ECDSA:
10328 case MBEDTLS_PK_ECKEY:
10329 return( MBEDTLS_SSL_SIG_ECDSA );
10330 default:
10331 return( MBEDTLS_SSL_SIG_ANON );
10332 }
10333}
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010336{
10337 switch( sig )
10338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_RSA_C)
10340 case MBEDTLS_SSL_SIG_RSA:
10341 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343#if defined(MBEDTLS_ECDSA_C)
10344 case MBEDTLS_SSL_SIG_ECDSA:
10345 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010346#endif
10347 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010349 }
10350}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010351#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010352
Hanno Becker7e5437a2017-04-28 17:15:26 +010010353#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10354 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10355
10356/* Find an entry in a signature-hash set matching a given hash algorithm. */
10357mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10358 mbedtls_pk_type_t sig_alg )
10359{
10360 switch( sig_alg )
10361 {
10362 case MBEDTLS_PK_RSA:
10363 return( set->rsa );
10364 case MBEDTLS_PK_ECDSA:
10365 return( set->ecdsa );
10366 default:
10367 return( MBEDTLS_MD_NONE );
10368 }
10369}
10370
10371/* Add a signature-hash-pair to a signature-hash set */
10372void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10373 mbedtls_pk_type_t sig_alg,
10374 mbedtls_md_type_t md_alg )
10375{
10376 switch( sig_alg )
10377 {
10378 case MBEDTLS_PK_RSA:
10379 if( set->rsa == MBEDTLS_MD_NONE )
10380 set->rsa = md_alg;
10381 break;
10382
10383 case MBEDTLS_PK_ECDSA:
10384 if( set->ecdsa == MBEDTLS_MD_NONE )
10385 set->ecdsa = md_alg;
10386 break;
10387
10388 default:
10389 break;
10390 }
10391}
10392
10393/* Allow exactly one hash algorithm for each signature. */
10394void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10395 mbedtls_md_type_t md_alg )
10396{
10397 set->rsa = md_alg;
10398 set->ecdsa = md_alg;
10399}
10400
10401#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10402 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10403
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010404/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010405 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010408{
10409 switch( hash )
10410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_MD5_C)
10412 case MBEDTLS_SSL_HASH_MD5:
10413 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415#if defined(MBEDTLS_SHA1_C)
10416 case MBEDTLS_SSL_HASH_SHA1:
10417 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#if defined(MBEDTLS_SHA256_C)
10420 case MBEDTLS_SSL_HASH_SHA224:
10421 return( MBEDTLS_MD_SHA224 );
10422 case MBEDTLS_SSL_HASH_SHA256:
10423 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425#if defined(MBEDTLS_SHA512_C)
10426 case MBEDTLS_SSL_HASH_SHA384:
10427 return( MBEDTLS_MD_SHA384 );
10428 case MBEDTLS_SSL_HASH_SHA512:
10429 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010430#endif
10431 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010433 }
10434}
10435
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010436/*
10437 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10438 */
10439unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10440{
10441 switch( md )
10442 {
10443#if defined(MBEDTLS_MD5_C)
10444 case MBEDTLS_MD_MD5:
10445 return( MBEDTLS_SSL_HASH_MD5 );
10446#endif
10447#if defined(MBEDTLS_SHA1_C)
10448 case MBEDTLS_MD_SHA1:
10449 return( MBEDTLS_SSL_HASH_SHA1 );
10450#endif
10451#if defined(MBEDTLS_SHA256_C)
10452 case MBEDTLS_MD_SHA224:
10453 return( MBEDTLS_SSL_HASH_SHA224 );
10454 case MBEDTLS_MD_SHA256:
10455 return( MBEDTLS_SSL_HASH_SHA256 );
10456#endif
10457#if defined(MBEDTLS_SHA512_C)
10458 case MBEDTLS_MD_SHA384:
10459 return( MBEDTLS_SSL_HASH_SHA384 );
10460 case MBEDTLS_MD_SHA512:
10461 return( MBEDTLS_SSL_HASH_SHA512 );
10462#endif
10463 default:
10464 return( MBEDTLS_SSL_HASH_NONE );
10465 }
10466}
10467
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010468#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010469/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010470 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010471 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010472 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010473int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010476
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010477 if( ssl->conf->curve_list == NULL )
10478 return( -1 );
10479
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010480 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010481 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010482 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010483
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010484 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010485}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010486#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010487
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010488#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010489/*
10490 * Check if a hash proposed by the peer is in our list.
10491 * Return 0 if we're willing to use it, -1 otherwise.
10492 */
10493int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10494 mbedtls_md_type_t md )
10495{
10496 const int *cur;
10497
10498 if( ssl->conf->sig_hashes == NULL )
10499 return( -1 );
10500
10501 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10502 if( *cur == (int) md )
10503 return( 0 );
10504
10505 return( -1 );
10506}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010507#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509#if defined(MBEDTLS_X509_CRT_PARSE_C)
10510int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10511 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010512 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010513 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010514{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010515 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010517 int usage = 0;
10518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010520 const char *ext_oid;
10521 size_t ext_len;
10522#endif
10523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10525 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010526 ((void) cert);
10527 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010528 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010529#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10532 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010533 {
10534 /* Server part of the key exchange */
10535 switch( ciphersuite->key_exchange )
10536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537 case MBEDTLS_KEY_EXCHANGE_RSA:
10538 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010539 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010540 break;
10541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010542 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10543 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10544 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10545 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010546 break;
10547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010548 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10549 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010550 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010551 break;
10552
10553 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554 case MBEDTLS_KEY_EXCHANGE_NONE:
10555 case MBEDTLS_KEY_EXCHANGE_PSK:
10556 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10557 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010558 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010559 usage = 0;
10560 }
10561 }
10562 else
10563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10565 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010566 }
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010569 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010570 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010571 ret = -1;
10572 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010573#else
10574 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10578 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10581 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010582 }
10583 else
10584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10586 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010587 }
10588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010589 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010590 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010591 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010592 ret = -1;
10593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010595
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010596 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010599
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010600/*
10601 * Convert version numbers to/from wire format
10602 * and, for DTLS, to/from TLS equivalent.
10603 *
10604 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010605 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010606 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10607 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010610 unsigned char ver[2] )
10611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if defined(MBEDTLS_SSL_PROTO_DTLS)
10613 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010616 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10617
10618 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10619 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10620 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010621 else
10622#else
10623 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010624#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010625 {
10626 ver[0] = (unsigned char) major;
10627 ver[1] = (unsigned char) minor;
10628 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010629}
10630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010632 const unsigned char ver[2] )
10633{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634#if defined(MBEDTLS_SSL_PROTO_DTLS)
10635 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010636 {
10637 *major = 255 - ver[0] + 2;
10638 *minor = 255 - ver[1] + 1;
10639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010641 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10642 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010643 else
10644#else
10645 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010646#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010647 {
10648 *major = ver[0];
10649 *minor = ver[1];
10650 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010651}
10652
Simon Butcher99000142016-10-13 17:21:01 +010010653int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10654{
10655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10656 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10657 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10658
10659 switch( md )
10660 {
10661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10662#if defined(MBEDTLS_MD5_C)
10663 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010664 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010665#endif
10666#if defined(MBEDTLS_SHA1_C)
10667 case MBEDTLS_SSL_HASH_SHA1:
10668 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10669 break;
10670#endif
10671#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10672#if defined(MBEDTLS_SHA512_C)
10673 case MBEDTLS_SSL_HASH_SHA384:
10674 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10675 break;
10676#endif
10677#if defined(MBEDTLS_SHA256_C)
10678 case MBEDTLS_SSL_HASH_SHA256:
10679 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10680 break;
10681#endif
10682 default:
10683 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10684 }
10685
10686 return 0;
10687#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10688 (void) ssl;
10689 (void) md;
10690
10691 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10692#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10693}
10694
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10697int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10698 unsigned char *output,
10699 unsigned char *data, size_t data_len )
10700{
10701 int ret = 0;
10702 mbedtls_md5_context mbedtls_md5;
10703 mbedtls_sha1_context mbedtls_sha1;
10704
10705 mbedtls_md5_init( &mbedtls_md5 );
10706 mbedtls_sha1_init( &mbedtls_sha1 );
10707
10708 /*
10709 * digitally-signed struct {
10710 * opaque md5_hash[16];
10711 * opaque sha_hash[20];
10712 * };
10713 *
10714 * md5_hash
10715 * MD5(ClientHello.random + ServerHello.random
10716 * + ServerParams);
10717 * sha_hash
10718 * SHA(ClientHello.random + ServerHello.random
10719 * + ServerParams);
10720 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010721 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010722 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010724 goto exit;
10725 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010726 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010727 ssl->handshake->randbytes, 64 ) ) != 0 )
10728 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010730 goto exit;
10731 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010732 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010733 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010735 goto exit;
10736 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010737 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010738 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010740 goto exit;
10741 }
10742
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010743 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010744 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010746 goto exit;
10747 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010748 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010749 ssl->handshake->randbytes, 64 ) ) != 0 )
10750 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010752 goto exit;
10753 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010754 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010755 data_len ) ) != 0 )
10756 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010758 goto exit;
10759 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010760 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010761 output + 16 ) ) != 0 )
10762 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010764 goto exit;
10765 }
10766
10767exit:
10768 mbedtls_md5_free( &mbedtls_md5 );
10769 mbedtls_sha1_free( &mbedtls_sha1 );
10770
10771 if( ret != 0 )
10772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10773 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10774
10775 return( ret );
10776
10777}
10778#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10779 MBEDTLS_SSL_PROTO_TLS1_1 */
10780
10781#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10782 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010783
10784#if defined(MBEDTLS_USE_PSA_CRYPTO)
10785int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10786 unsigned char *hash, size_t *hashlen,
10787 unsigned char *data, size_t data_len,
10788 mbedtls_md_type_t md_alg )
10789{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010790 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010791 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010792 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10793
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010795
10796 if( ( status = psa_hash_setup( &hash_operation,
10797 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010798 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010799 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010800 goto exit;
10801 }
10802
Andrzej Kurek814feff2019-01-14 04:35:19 -050010803 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10804 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010805 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010806 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010807 goto exit;
10808 }
10809
Andrzej Kurek814feff2019-01-14 04:35:19 -050010810 if( ( status = psa_hash_update( &hash_operation,
10811 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010812 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010813 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010814 goto exit;
10815 }
10816
Andrzej Kurek814feff2019-01-14 04:35:19 -050010817 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10818 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010819 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010820 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010821 goto exit;
10822 }
10823
10824exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010825 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010826 {
10827 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10828 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010829 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010830 {
10831 case PSA_ERROR_NOT_SUPPORTED:
10832 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010833 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010834 case PSA_ERROR_BUFFER_TOO_SMALL:
10835 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10836 case PSA_ERROR_INSUFFICIENT_MEMORY:
10837 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10838 default:
10839 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10840 }
10841 }
10842 return( 0 );
10843}
10844
10845#else
10846
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010847int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010848 unsigned char *hash, size_t *hashlen,
10849 unsigned char *data, size_t data_len,
10850 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010851{
10852 int ret = 0;
10853 mbedtls_md_context_t ctx;
10854 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010855 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010856
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010858
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010859 mbedtls_md_init( &ctx );
10860
10861 /*
10862 * digitally-signed struct {
10863 * opaque client_random[32];
10864 * opaque server_random[32];
10865 * ServerDHParams params;
10866 * };
10867 */
10868 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10869 {
10870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10871 goto exit;
10872 }
10873 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10874 {
10875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10876 goto exit;
10877 }
10878 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10879 {
10880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10881 goto exit;
10882 }
10883 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10884 {
10885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10886 goto exit;
10887 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010888 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010889 {
10890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10891 goto exit;
10892 }
10893
10894exit:
10895 mbedtls_md_free( &ctx );
10896
10897 if( ret != 0 )
10898 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10899 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10900
10901 return( ret );
10902}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010903#endif /* MBEDTLS_USE_PSA_CRYPTO */
10904
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010905#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10906 MBEDTLS_SSL_PROTO_TLS1_2 */
10907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908#endif /* MBEDTLS_SSL_TLS_C */