blob: 780a682ccc7d59d448002ba3b110fda83f7caa2b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000289
290#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291 if( src->peer_cert != NULL )
292 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200293 int ret;
294
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200295 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200302 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 dst->peer_cert = NULL;
306 return( ret );
307 }
308 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000310 if( src->peer_cert_digest != NULL )
311 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000312 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000313 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000314 if( dst->peer_cert_digest == NULL )
315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
316
317 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
318 src->peer_cert_digest_len );
319 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000320 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000321 }
322#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200325
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200326#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200327 if( src->ticket != NULL )
328 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200329 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200330 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200332
333 memcpy( dst->ticket, src->ticket, src->ticket_len );
334 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200335#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200336
337 return( 0 );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519static int tls_prf_generic( mbedtls_md_type_t md_type,
520 const unsigned char *secret, size_t slen,
521 const char *label,
522 const unsigned char *random, size_t rlen,
523 unsigned char *dstbuf, size_t dlen )
524{
525 psa_status_t status;
526 psa_algorithm_t alg;
527 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500528 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500529 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
530
Andrzej Kurek2f760752019-01-28 08:08:15 -0500531 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500533
Andrzej Kurekc929a822019-01-14 03:51:11 -0500534 if( md_type == MBEDTLS_MD_SHA384 )
535 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
536 else
537 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
538
Andrzej Kurek2f760752019-01-28 08:08:15 -0500539 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500540 psa_key_policy_set_usage( &policy,
541 PSA_KEY_USAGE_DERIVE,
542 alg );
543 status = psa_set_key_policy( master_slot, &policy );
544 if( status != PSA_SUCCESS )
545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
546
547 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
548 if( status != PSA_SUCCESS )
549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
550
551 status = psa_key_derivation( &generator,
552 master_slot, alg,
553 random, rlen,
554 (unsigned char const *) label,
555 (size_t) strlen( label ),
556 dlen );
557 if( status != PSA_SUCCESS )
558 {
559 psa_generator_abort( &generator );
560 psa_destroy_key( master_slot );
561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
562 }
563
564 status = psa_generator_read( &generator, dstbuf, dlen );
565 if( status != PSA_SUCCESS )
566 {
567 psa_generator_abort( &generator );
568 psa_destroy_key( master_slot );
569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
570 }
571
572 status = psa_generator_abort( &generator );
573 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500574 {
575 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500576 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500577 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500578
579 status = psa_destroy_key( master_slot );
580 if( status != PSA_SUCCESS )
581 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
582
Andrzej Kurek33171262019-01-15 03:25:18 -0500583 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500584}
585
586#else /* MBEDTLS_USE_PSA_CRYPTO */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 const unsigned char *secret, size_t slen,
590 const char *label,
591 const unsigned char *random, size_t rlen,
592 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593{
594 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
598 const mbedtls_md_info_t *md_info;
599 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 int ret;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
609 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611
612 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 memcpy( tmp + md_len, label, nb );
614 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 nb += rlen;
616
617 /*
618 * Compute P_<hash>(secret, label + random)[0..dlen]
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
624 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
625 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100626
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
635 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
646 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647
648 return( 0 );
649}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652static int tls_prf_sha256( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663static int tls_prf_sha384( const unsigned char *secret, size_t slen,
664 const char *label,
665 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 unsigned char *dstbuf, size_t dlen )
667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_SHA512_C */
672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
677 defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker380da532012-04-18 16:10:25 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_SSL3)
682static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
683static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
687static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
688static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200689#endif
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
692#if defined(MBEDTLS_SHA256_C)
693static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
694static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
695static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
699static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
700static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
701static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Hanno Becker7d0a5692018-10-23 15:26:22 +0100705#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
706 defined(MBEDTLS_USE_PSA_CRYPTO)
707static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
708{
709 if( ssl->conf->f_psk != NULL )
710 {
711 /* If we've used a callback to select the PSK,
712 * the static configuration is irrelevant. */
713 if( ssl->handshake->psk_opaque != 0 )
714 return( 1 );
715
716 return( 0 );
717 }
718
719 if( ssl->conf->psk_opaque != 0 )
720 return( 1 );
721
722 return( 0 );
723}
724#endif /* MBEDTLS_USE_PSA_CRYPTO &&
725 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000728{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200729 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000730#if defined(MBEDTLS_USE_PSA_CRYPTO)
731 int psa_fallthrough;
732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 unsigned char keyblk[256];
735 unsigned char *key1;
736 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100737 unsigned char *mac_enc;
738 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000739 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000741 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 const mbedtls_cipher_info_t *cipher_info;
744 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100745
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000746 /* cf. RFC 5246, Section 8.1:
747 * "The master secret is always exactly 48 bytes in length." */
748 size_t const master_secret_len = 48;
749
Hanno Becker35b23c72018-10-23 12:10:41 +0100750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
751 unsigned char session_hash[48];
752#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_ssl_session *session = ssl->session_negotiate;
755 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Hanno Beckere694c3e2017-12-27 21:34:08 +0000760
761 ciphersuite_info = handshake->ciphersuite_info;
762 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100763 if( cipher_info == NULL )
764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000766 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100768 }
769
Hanno Beckere694c3e2017-12-27 21:34:08 +0000770 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100771 if( md_info == NULL )
772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000774 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100776 }
777
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_SSL3)
782 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 {
Paul Bakker48916f92012-09-16 19:57:18 +0000784 handshake->tls_prf = ssl3_prf;
785 handshake->calc_verify = ssl_calc_verify_ssl;
786 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788 else
789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
791 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 {
Paul Bakker48916f92012-09-16 19:57:18 +0000793 handshake->tls_prf = tls1_prf;
794 handshake->calc_verify = ssl_calc_verify_tls;
795 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797 else
798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
800#if defined(MBEDTLS_SHA512_C)
801 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000802 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803 {
Paul Bakker48916f92012-09-16 19:57:18 +0000804 handshake->tls_prf = tls_prf_sha384;
805 handshake->calc_verify = ssl_calc_verify_tls_sha384;
806 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000808 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SHA256_C)
811 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000812 {
Paul Bakker48916f92012-09-16 19:57:18 +0000813 handshake->tls_prf = tls_prf_sha256;
814 handshake->calc_verify = ssl_calc_verify_tls_sha256;
815 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200817 else
818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200823 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000824
825 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 * SSLv3:
827 * master =
828 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
829 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
830 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200831 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200832 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 * master = PRF( premaster, "master secret", randbytes )[0..47]
834 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100835 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
838 }
839 else
840 {
841 /* The label for the KDF used for key expansion.
842 * This is either "master secret" or "extended master secret"
843 * depending on whether the Extended Master Secret extension
844 * is used. */
845 char const *lbl = "master secret";
846
847 /* The salt for the KDF used for key expansion.
848 * - If the Extended Master Secret extension is not used,
849 * this is ClientHello.Random + ServerHello.Random
850 * (see Sect. 8.1 in RFC 5246).
851 * - If the Extended Master Secret extension is used,
852 * this is the transcript of the handshake so far.
853 * (see Sect. 4 in RFC 7627). */
854 unsigned char const *salt = handshake->randbytes;
855 size_t salt_len = 64;
856
857#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861
Hanno Becker35b23c72018-10-23 12:10:41 +0100862 lbl = "extended master secret";
863 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200864 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000869 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
870 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100871 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000872 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200873 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100874#endif /* MBEDTLS_SHA512_C */
875 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100879 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880
Hanno Becker35b23c72018-10-23 12:10:41 +0100881 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200882 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100883#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
884
Hanno Becker7d0a5692018-10-23 15:26:22 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
886 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
887 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
888 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
889 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100890 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 /* Perform PSK-to-MS expansion in a single step. */
892 psa_status_t status;
893 psa_algorithm_t alg;
894 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500895 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100896
897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
898
899 psk = ssl->conf->psk_opaque;
900 if( ssl->handshake->psk_opaque != 0 )
901 psk = ssl->handshake->psk_opaque;
902
903 if( md_type == MBEDTLS_MD_SHA384 )
904 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
905 else
906 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
907
908 status = psa_key_derivation( &generator, psk, alg,
909 salt, salt_len,
910 (unsigned char const *) lbl,
911 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000912 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100913 if( status != PSA_SUCCESS )
914 {
915 psa_generator_abort( &generator );
916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
917 }
918
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000919 status = psa_generator_read( &generator, session->master,
920 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921 if( status != PSA_SUCCESS )
922 {
923 psa_generator_abort( &generator );
924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
925 }
926
927 status = psa_generator_abort( &generator );
928 if( status != PSA_SUCCESS )
929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100930 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100931 else
932#endif
933 {
934 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
935 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000936 session->master,
937 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100938 if( ret != 0 )
939 {
940 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
941 return( ret );
942 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200943
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
945 handshake->premaster,
946 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 mbedtls_platform_zeroize( handshake->premaster,
949 sizeof(handshake->premaster) );
950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 /*
954 * Swap the client and server random values.
955 */
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( tmp, handshake->randbytes, 64 );
957 memcpy( handshake->randbytes, tmp + 32, 32 );
958 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500959 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 /*
962 * SSLv3:
963 * key block =
964 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
965 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
966 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
967 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
968 * ...
969 *
970 * TLSv1:
971 * key block = PRF( master, "key expansion", randbytes )
972 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100973 ret = handshake->tls_prf( session->master, 48, "key expansion",
974 handshake->randbytes, 64, keyblk, 256 );
975 if( ret != 0 )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100978 return( ret );
979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
982 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
984 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
985 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500987 mbedtls_platform_zeroize( handshake->randbytes,
988 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Determine the appropriate key, IV and MAC length.
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993
Hanno Becker88aaf652017-12-27 08:17:40 +0000994 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200997 cipher_info->mode == MBEDTLS_MODE_CCM ||
998 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001000 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001002 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001004 transform->taglen =
1005 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001006
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001007 /* All modes haves 96-bit IVs;
1008 * GCM and CCM has 4 implicit and 8 explicit bytes
1009 * ChachaPoly has all 12 bytes implicit
1010 */
Paul Bakker68884e32013-01-07 18:20:04 +01001011 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001012 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1013 transform->fixed_ivlen = 12;
1014 else
1015 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001017 /* Minimum length of encrypted record */
1018 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001020 }
1021 else
1022 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1025 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001028 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001032 mac_key_len = mbedtls_md_get_size( md_info );
1033 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001036 /*
1037 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1038 * (rfc 6066 page 13 or rfc 2104 section 4),
1039 * so we only need to adjust the length here.
1040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001044
1045#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1046 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001047 * HMAC implementation which also truncates the key
1048 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001049 mac_key_len = transform->maclen;
1050#endif
1051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001053
1054 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001055 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001057 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001059 transform->minlen = transform->maclen;
1060 else
Paul Bakker68884e32013-01-07 18:20:04 +01001061 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001062 /*
1063 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001064 * 1. if EtM is in use: one block plus MAC
1065 * otherwise: * first multiple of blocklen greater than maclen
1066 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1069 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001070 {
1071 transform->minlen = transform->maclen
1072 + cipher_info->block_size;
1073 }
1074 else
1075#endif
1076 {
1077 transform->minlen = transform->maclen
1078 + cipher_info->block_size
1079 - transform->maclen % cipher_info->block_size;
1080 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1083 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1084 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001085 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001086 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1089 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1090 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001091 {
1092 transform->minlen += transform->ivlen;
1093 }
1094 else
1095#endif
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001099 }
Paul Bakker68884e32013-01-07 18:20:04 +01001100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 }
1102
Hanno Becker88aaf652017-12-27 08:17:40 +00001103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1104 (unsigned) keylen,
1105 (unsigned) transform->minlen,
1106 (unsigned) transform->ivlen,
1107 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
1109 /*
1110 * Finally setup the cipher contexts, IVs and MAC secrets.
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001115 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001116 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker68884e32013-01-07 18:20:04 +01001118 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001119 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001121 /*
1122 * This is not used in TLS v1.1.
1123 */
Paul Bakker48916f92012-09-16 19:57:18 +00001124 iv_copy_len = ( transform->fixed_ivlen ) ?
1125 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001126 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1127 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
1130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_SSL_CLI_C */
1132#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001133 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001135 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Hanno Becker81c7b182017-11-09 18:39:33 +00001138 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001139 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001141 /*
1142 * This is not used in TLS v1.1.
1143 */
Paul Bakker48916f92012-09-16 19:57:18 +00001144 iv_copy_len = ( transform->fixed_ivlen ) ?
1145 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001146 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1147 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001148 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001150 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001159 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001160 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001164 }
1165
Hanno Becker81c7b182017-11-09 18:39:33 +00001166 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1167 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001168 }
1169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1172 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1173 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001174 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001175 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1176 For AEAD-based ciphersuites, there is nothing to do here. */
1177 if( mac_key_len != 0 )
1178 {
1179 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1180 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1181 }
Paul Bakker68884e32013-01-07 18:20:04 +01001182 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001183 else
1184#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1191 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001192 {
1193 int ret = 0;
1194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001196
Hanno Becker88aaf652017-12-27 08:17:40 +00001197 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001198 transform->iv_enc, transform->iv_dec,
1199 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001200 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001201 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1204 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001205 }
1206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001208
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001209#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1210 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001211 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001212 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1213 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001214 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001215 iv_copy_len );
1216 }
1217#endif
1218
Hanno Beckerf704bef2018-11-16 15:21:18 +00001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001220
1221 /* Only use PSA-based ciphers for TLS-1.2.
1222 * That's relevant at least for TLS-1.0, where
1223 * we assume that mbedtls_cipher_crypt() updates
1224 * the structure field for the IV, which the PSA-based
1225 * implementation currently doesn't. */
1226#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1227 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001228 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001229 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1230 cipher_info, taglen );
1231 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1232 {
1233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1234 return( ret );
1235 }
1236
1237 if( ret == 0 )
1238 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001240 psa_fallthrough = 0;
1241 }
1242 else
1243 {
1244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1245 psa_fallthrough = 1;
1246 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001247 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001249 psa_fallthrough = 1;
1250#else
1251 psa_fallthrough = 1;
1252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001253
Hanno Beckercb1cc802018-11-17 22:27:38 +00001254 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001256 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001257 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001260 return( ret );
1261 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001262
Hanno Beckerf704bef2018-11-16 15:21:18 +00001263#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001264 /* Only use PSA-based ciphers for TLS-1.2.
1265 * That's relevant at least for TLS-1.0, where
1266 * we assume that mbedtls_cipher_crypt() updates
1267 * the structure field for the IV, which the PSA-based
1268 * implementation currently doesn't. */
1269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1270 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001271 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001272 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1273 cipher_info, taglen );
1274 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1275 {
1276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1277 return( ret );
1278 }
1279
1280 if( ret == 0 )
1281 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001283 psa_fallthrough = 0;
1284 }
1285 else
1286 {
1287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1288 psa_fallthrough = 1;
1289 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001290 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001291 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001292 psa_fallthrough = 1;
1293#else
1294 psa_fallthrough = 1;
1295#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001296
Hanno Beckercb1cc802018-11-17 22:27:38 +00001297 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001299 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001300 cipher_info ) ) != 0 )
1301 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001303 return( ret );
1304 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001307 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311 return( ret );
1312 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001315 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001319 return( ret );
1320 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_CIPHER_MODE_CBC)
1323 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1326 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001329 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001330 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1333 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 return( ret );
1337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001341 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001344 // Initialize compression
1345 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001347 {
Paul Bakker16770332013-10-11 09:59:44 +02001348 if( ssl->compress_buf == NULL )
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001351 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001352 if( ssl->compress_buf == NULL )
1353 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001355 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001356 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001357 }
1358 }
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001361
Paul Bakker48916f92012-09-16 19:57:18 +00001362 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1363 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001364
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001365 if( deflateInit( &transform->ctx_deflate,
1366 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001367 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1370 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001371 }
1372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
1381void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001382{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001383 mbedtls_md5_context md5;
1384 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 unsigned char pad_1[48];
1386 unsigned char pad_2[48];
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001390 mbedtls_md5_init( &md5 );
1391 mbedtls_sha1_init( &sha1 );
1392
1393 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1394 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Paul Bakker380da532012-04-18 16:10:25 +00001396 memset( pad_1, 0x36, 48 );
1397 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001399 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1400 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1401 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001403 mbedtls_md5_starts_ret( &md5 );
1404 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1405 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1406 mbedtls_md5_update_ret( &md5, hash, 16 );
1407 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001409 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1410 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1411 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001412
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001413 mbedtls_sha1_starts_ret( &sha1 );
1414 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1415 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1416 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1417 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001421
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001422 mbedtls_md5_free( &md5 );
1423 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001424
Paul Bakker380da532012-04-18 16:10:25 +00001425 return;
1426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1430void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001431{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001432 mbedtls_md5_context md5;
1433 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001437 mbedtls_md5_init( &md5 );
1438 mbedtls_sha1_init( &sha1 );
1439
1440 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1441 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Andrzej Kurekeb342242019-01-29 09:14:33 -05001443 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001444 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001449 mbedtls_md5_free( &md5 );
1450 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001451
Paul Bakker380da532012-04-18 16:10:25 +00001452 return;
1453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1457#if defined(MBEDTLS_SHA256_C)
1458void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001459{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001460#if defined(MBEDTLS_USE_PSA_CRYPTO)
1461 size_t hash_size;
1462 psa_status_t status;
1463 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1464
1465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1466 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1467 if( status != PSA_SUCCESS )
1468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1470 return;
1471 }
1472
1473 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1474 if( status != PSA_SUCCESS )
1475 {
1476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1477 return;
1478 }
1479 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1481#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001482 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001484 mbedtls_sha256_init( &sha256 );
1485
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001489 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001494 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001495#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001496 return;
1497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SHA512_C)
1501void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001502{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001503#if defined(MBEDTLS_USE_PSA_CRYPTO)
1504 size_t hash_size;
1505 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001506 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001507
1508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001509 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001510 if( status != PSA_SUCCESS )
1511 {
1512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1513 return;
1514 }
1515
Andrzej Kurek972fba52019-01-30 03:29:12 -05001516 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001517 if( status != PSA_SUCCESS )
1518 {
1519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1520 return;
1521 }
1522 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1524#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001526
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001527 mbedtls_sha512_init( &sha512 );
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001530
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001531 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001532 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001537 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001538#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 return;
1540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_SHA512_C */
1542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1545int 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 +02001546{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 unsigned char *p = ssl->handshake->premaster;
1548 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001549 const unsigned char *psk = ssl->conf->psk;
1550 size_t psk_len = ssl->conf->psk_len;
1551
1552 /* If the psk callback was called, use its result */
1553 if( ssl->handshake->psk != NULL )
1554 {
1555 psk = ssl->handshake->psk;
1556 psk_len = ssl->handshake->psk_len;
1557 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
1559 /*
1560 * PMS = struct {
1561 * opaque other_secret<0..2^16-1>;
1562 * opaque psk<0..2^16-1>;
1563 * };
1564 * with "other_secret" depending on the particular key exchange
1565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1567 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001569 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001572 *(p++) = (unsigned char)( psk_len >> 8 );
1573 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001574
1575 if( end < p || (size_t)( end - p ) < psk_len )
1576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1577
1578 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001579 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 }
1581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1583#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1584 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001585 {
1586 /*
1587 * other_secret already set by the ClientKeyExchange message,
1588 * and is 48 bytes long
1589 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001590 if( end - p < 2 )
1591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1592
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001593 *p++ = 0;
1594 *p++ = 48;
1595 p += 48;
1596 }
1597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1599#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1600 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001602 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001603 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001605 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001607 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001608 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 return( ret );
1612 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001613 *(p++) = (unsigned char)( len >> 8 );
1614 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001615 p += len;
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 }
1619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1621#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1622 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001624 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 size_t zlen;
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001628 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001629 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 return( ret );
1633 }
1634
1635 *(p++) = (unsigned char)( zlen >> 8 );
1636 *(p++) = (unsigned char)( zlen );
1637 p += zlen;
1638
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001639 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1640 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 }
1642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001647 }
1648
1649 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001650 if( end - p < 2 )
1651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001652
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001653 *(p++) = (unsigned char)( psk_len >> 8 );
1654 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001655
1656 if( end < p || (size_t)( end - p ) < psk_len )
1657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1658
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001659 memcpy( p, psk, psk_len );
1660 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001661
1662 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1663
1664 return( 0 );
1665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001669/*
1670 * SSLv3.0 MAC functions
1671 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001672#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001673static void ssl_mac( mbedtls_md_context_t *md_ctx,
1674 const unsigned char *secret,
1675 const unsigned char *buf, size_t len,
1676 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001677 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001678{
1679 unsigned char header[11];
1680 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1683 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001684
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001685 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001687 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001688 else
Paul Bakker68884e32013-01-07 18:20:04 +01001689 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 memcpy( header, ctr, 8 );
1692 header[ 8] = (unsigned char) type;
1693 header[ 9] = (unsigned char)( len >> 8 );
1694 header[10] = (unsigned char)( len );
1695
Paul Bakker68884e32013-01-07 18:20:04 +01001696 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 mbedtls_md_starts( md_ctx );
1698 mbedtls_md_update( md_ctx, secret, md_size );
1699 mbedtls_md_update( md_ctx, padding, padlen );
1700 mbedtls_md_update( md_ctx, header, 11 );
1701 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001702 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Paul Bakker68884e32013-01-07 18:20:04 +01001704 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_md_starts( md_ctx );
1706 mbedtls_md_update( md_ctx, secret, md_size );
1707 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001708 mbedtls_md_update( md_ctx, out, md_size );
1709 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001712
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001713/* The function below is only used in the Lucky 13 counter-measure in
1714 * ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001715#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001716 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1717 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1718 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1719/* This function makes sure every byte in the memory region is accessed
1720 * (in ascending addresses order) */
1721static void ssl_read_memory( unsigned char *p, size_t len )
1722{
1723 unsigned char acc = 0;
1724 volatile unsigned char force;
1725
1726 for( ; len != 0; p++, len-- )
1727 acc ^= *p;
1728
1729 force = acc;
1730 (void) force;
1731}
1732#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1733
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001734/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001740 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001744 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001748 }
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001753 ssl->out_msg, ssl->out_msglen );
1754
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001756 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 */
Hanno Becker52344c22018-01-03 15:24:20 +00001758#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 if( mode == MBEDTLS_MODE_STREAM ||
1760 ( mode == MBEDTLS_MODE_CBC
1761#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1762 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001763#endif
1764 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#if defined(MBEDTLS_SSL_PROTO_SSL3)
1767 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001768 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001769 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001770
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001771 ssl_mac( &ssl->transform_out->md_ctx_enc,
1772 ssl->transform_out->mac_enc,
1773 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001774 ssl->out_ctr, ssl->out_msgtype,
1775 mac );
1776
1777 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 }
1779 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1782 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1783 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 {
Hanno Becker992b6872017-11-09 18:57:39 +00001785 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1788 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1789 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1790 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001792 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001794
1795 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 }
1797 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001798#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001802 }
1803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001805 ssl->out_msg + ssl->out_msglen,
1806 ssl->transform_out->maclen );
1807
1808 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001809 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001810 }
Hanno Becker52344c22018-01-03 15:24:20 +00001811#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001813 /*
1814 * Encrypt
1815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1817 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001819 int ret;
1820 size_t olen = 0;
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 "including %d bytes of padding",
1824 ssl->out_msglen, 0 ) );
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001827 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001828 ssl->transform_out->ivlen,
1829 ssl->out_msg, ssl->out_msglen,
1830 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001833 return( ret );
1834 }
1835
1836 if( ssl->out_msglen != olen )
1837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 }
Paul Bakker68884e32013-01-07 18:20:04 +01001842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001844#if defined(MBEDTLS_GCM_C) || \
1845 defined(MBEDTLS_CCM_C) || \
1846 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001848 mode == MBEDTLS_MODE_CCM ||
1849 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001850 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001851 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001852 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001853 unsigned char *enc_msg;
1854 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 unsigned char iv[12];
1856 mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001857 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001858
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001859 /*
1860 * Prepare additional authenticated data
1861 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001862 memcpy( add_data, ssl->out_ctr, 8 );
1863 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001865 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001866 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1867 add_data[12] = ssl->out_msglen & 0xFF;
1868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001869 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001870
Paul Bakker68884e32013-01-07 18:20:04 +01001871 /*
1872 * Generate IV
1873 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001874 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1875 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001876 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001877 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1878 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1879 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1880
1881 }
1882 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1883 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001884 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001885 unsigned char i;
1886
1887 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1888
1889 for( i = 0; i < 8; i++ )
1890 iv[i+4] ^= ssl->out_ctr[i];
1891 }
1892 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001893 {
1894 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001897 }
1898
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001899 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1900 iv, transform->ivlen );
1901 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1902 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001903
Paul Bakker68884e32013-01-07 18:20:04 +01001904 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001906 */
1907 enc_msg = ssl->out_msg;
1908 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001909 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001912 "including 0 bytes of padding",
1913 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001914
Paul Bakker68884e32013-01-07 18:20:04 +01001915 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001916 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001917 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1919 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001920 add_data, 13,
1921 enc_msg, enc_msglen,
1922 enc_msg, &olen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001923 enc_msg + enc_msglen,
1924 ssl->transform_out->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001927 return( ret );
1928 }
1929
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001930 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001934 }
1935
Hanno Beckere694c3e2017-12-27 21:34:08 +00001936 ssl->out_msglen += ssl->transform_out->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001937 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001938
Hanno Beckere694c3e2017-12-27 21:34:08 +00001939 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen,
1940 ssl->transform_out->taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1944#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001945 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001948 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001949 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001950 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001951
Paul Bakker48916f92012-09-16 19:57:18 +00001952 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1953 ssl->transform_out->ivlen;
1954 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 padlen = 0;
1956
1957 for( i = 0; i <= padlen; i++ )
1958 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1959
1960 ssl->out_msglen += padlen + 1;
1961
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001962 enc_msglen = ssl->out_msglen;
1963 enc_msg = ssl->out_msg;
1964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001966 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001967 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1968 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001971 {
1972 /*
1973 * Generate IV
1974 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001975 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001976 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001977 if( ret != 0 )
1978 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001979
Paul Bakker92be97b2013-01-02 17:30:03 +01001980 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001981 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001982
1983 /*
1984 * Fix pointer positions and message length with added IV
1985 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001986 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001987 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001988 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001993 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001994 ssl->out_msglen, ssl->transform_out->ivlen,
1995 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001998 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001999 ssl->transform_out->ivlen,
2000 enc_msg, enc_msglen,
2001 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002004 return( ret );
2005 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002006
Paul Bakkercca5b812013-08-31 17:40:26 +02002007 if( enc_msglen != olen )
2008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002011 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2014 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002015 {
2016 /*
2017 * Save IV in SSL3 and TLS1
2018 */
2019 memcpy( ssl->transform_out->iv_enc,
2020 ssl->transform_out->cipher_ctx_enc.iv,
2021 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002023#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002026 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002028 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2029
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002030 /*
2031 * MAC(MAC_write_key, seq_num +
2032 * TLSCipherText.type +
2033 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002034 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002035 * IV + // except for TLS 1.0
2036 * ENC(content + padding + padding_length));
2037 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002038 unsigned char pseudo_hdr[13];
2039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002041
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002042 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2043 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002044 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2045 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2050 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002051 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002052 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002054
Hanno Becker3d8c9072018-01-05 16:24:22 +00002055 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2056 ssl->transform_out->maclen );
2057
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002058 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002059 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002063 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002065 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002071 /* Make extra sure authentication was performed, exactly once */
2072 if( auth_done != 1 )
2073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002076 }
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
2080 return( 0 );
2081}
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002086 int auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002087#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002088 size_t padlen = 0, correct = 1;
2089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002097 }
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002104 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 }
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2109 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002110 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002111 int ret;
2112 size_t olen = 0;
2113
Paul Bakker68884e32013-01-07 18:20:04 +01002114 padlen = 0;
2115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002117 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002118 ssl->transform_in->ivlen,
2119 ssl->in_msg, ssl->in_msglen,
2120 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002123 return( ret );
2124 }
2125
2126 if( ssl->in_msglen != olen )
2127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
Paul Bakker68884e32013-01-07 18:20:04 +01002132 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002134#if defined(MBEDTLS_GCM_C) || \
2135 defined(MBEDTLS_CCM_C) || \
2136 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002138 mode == MBEDTLS_MODE_CCM ||
2139 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002140 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002141 int ret;
2142 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002143 unsigned char *dec_msg;
2144 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002145 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002146 unsigned char iv[12];
2147 mbedtls_ssl_transform *transform = ssl->transform_in;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002148 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002149
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002150 /*
2151 * Compute and update sizes
2152 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002153 if( ssl->in_msglen < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002156 "+ taglen (%d)", ssl->in_msglen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002157 explicit_iv_len, ssl->transform_in->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002159 }
Hanno Beckere694c3e2017-12-27 21:34:08 +00002160 dec_msglen = ssl->in_msglen - explicit_iv_len - transform->taglen;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002161
Paul Bakker68884e32013-01-07 18:20:04 +01002162 dec_msg = ssl->in_msg;
2163 dec_msg_result = ssl->in_msg;
2164 ssl->in_msglen = dec_msglen;
2165
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002166 /*
2167 * Prepare additional authenticated data
2168 */
Paul Bakker68884e32013-01-07 18:20:04 +01002169 memcpy( add_data, ssl->in_ctr, 8 );
2170 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002172 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002173 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2174 add_data[12] = ssl->in_msglen & 0xFF;
2175
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002176 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002177
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002178 /*
2179 * Prepare IV
2180 */
2181 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2182 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002183 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2185 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002186
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002187 }
2188 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2189 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002190 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002191 unsigned char i;
2192
2193 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2194
2195 for( i = 0; i < 8; i++ )
2196 iv[i+4] ^= ssl->in_ctr[i];
2197 }
2198 else
2199 {
2200 /* Reminder if we ever add an AEAD mode with a different size */
2201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2203 }
2204
2205 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Beckere694c3e2017-12-27 21:34:08 +00002206 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen,
2207 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002208
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002209 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002210 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002213 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002214 add_data, 13,
2215 dec_msg, dec_msglen,
2216 dec_msg_result, &olen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002217 dec_msg + dec_msglen,
2218 ssl->transform_in->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2223 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002224
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002225 return( ret );
2226 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002227 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002228
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002229 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002233 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2237#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002238 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
Paul Bakker45829992013-01-03 14:52:21 +01002241 /*
2242 * Decrypt and check the padding
2243 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002244 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002245 unsigned char *dec_msg;
2246 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002247 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002248 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002249 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002250
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 /*
Paul Bakker45829992013-01-03 14:52:21 +01002252 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2255 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002256 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002257#endif
Paul Bakker45829992013-01-03 14:52:21 +01002258
2259 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2260 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002263 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2264 ssl->transform_in->ivlen,
2265 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002267 }
2268
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002269 dec_msglen = ssl->in_msglen;
2270 dec_msg = ssl->in_msg;
2271 dec_msg_result = ssl->in_msg;
2272
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002273 /*
2274 * Authenticate before decrypt if enabled
2275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2277 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002278 {
Hanno Becker992b6872017-11-09 18:57:39 +00002279 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002280 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002283
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002284 dec_msglen -= ssl->transform_in->maclen;
2285 ssl->in_msglen -= ssl->transform_in->maclen;
2286
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002287 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2288 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2289 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2290 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2295 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002296 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002297 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002301 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002302 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 ssl->transform_in->maclen );
2304
Hanno Becker992b6872017-11-09 18:57:39 +00002305 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2306 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002312 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315
2316 /*
2317 * Check length sanity
2318 */
2319 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002322 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002324 }
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002327 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002328 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002331 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002332 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002333 dec_msglen -= ssl->transform_in->ivlen;
2334 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002335
Paul Bakker48916f92012-09-16 19:57:18 +00002336 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002337 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002342 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002343 ssl->transform_in->ivlen,
2344 dec_msg, dec_msglen,
2345 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002348 return( ret );
2349 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002350
Paul Bakkercca5b812013-08-31 17:40:26 +02002351 if( dec_msglen != olen )
2352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002355 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2358 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002359 {
2360 /*
2361 * Save IV in SSL3 and TLS1
2362 */
2363 memcpy( ssl->transform_in->iv_dec,
2364 ssl->transform_in->cipher_ctx_dec.iv,
2365 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002367#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002370
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002371 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002372 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#if defined(MBEDTLS_SSL_DEBUG_ALL)
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002376 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002377#endif
Paul Bakker45829992013-01-03 14:52:21 +01002378 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002379 correct = 0;
2380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_PROTO_SSL3)
2383 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 {
Paul Bakker48916f92012-09-16 19:57:18 +00002385 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#if defined(MBEDTLS_SSL_DEBUG_ALL)
2388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002390 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002391#endif
Paul Bakker45829992013-01-03 14:52:21 +01002392 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394 }
2395 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2398 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2399 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
2401 /*
Paul Bakker45829992013-01-03 14:52:21 +01002402 * TLSv1+: always check the padding up to the first failure
2403 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002405 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002406 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002407 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002408
Paul Bakker956c9e02013-12-19 14:42:28 +01002409 /*
2410 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002411 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002412 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002413 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002414 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002415 *
2416 * In both cases we reset padding_idx to a safe value (0) to
2417 * prevent out-of-buffer reads.
2418 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002419 correct &= ( padlen <= ssl->in_msglen );
2420 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002421 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002422
2423 padding_idx *= correct;
2424
Angus Grattonb512bc12018-06-19 15:57:50 +10002425 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002426 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002427 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002428 pad_count += real_count *
2429 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2430 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002431
2432 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002435 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002437#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002438 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002440 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2442 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002446 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002447
2448 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002450 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002452 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002458#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002461#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
2463 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002464 * Authenticate if not done yet.
2465 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 */
Hanno Becker52344c22018-01-03 15:24:20 +00002467#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002468 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002469 {
Hanno Becker992b6872017-11-09 18:57:39 +00002470 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002471
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002474 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2475 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_SSL_PROTO_SSL3)
2478 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002479 {
2480 ssl_mac( &ssl->transform_in->md_ctx_dec,
2481 ssl->transform_in->mac_dec,
2482 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002483 ssl->in_ctr, ssl->in_msgtype,
2484 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002485 }
2486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2488#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2489 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2490 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002491 {
2492 /*
2493 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002494 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002495 *
2496 * Known timing attacks:
2497 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2498 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002499 * To compensate for different timings for the MAC calculation
2500 * depending on how much padding was removed (which is determined
2501 * by padlen), process extra_run more blocks through the hash
2502 * function.
2503 *
2504 * The formula in the paper is
2505 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2506 * where L1 is the size of the header plus the decrypted message
2507 * plus CBC padding and L2 is the size of the header plus the
2508 * decrypted message. This is for an underlying hash function
2509 * with 64-byte blocks.
2510 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2511 * correctly. We round down instead of up, so -56 is the correct
2512 * value for our calculations instead of -55.
2513 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002514 * Repeat the formula rather than defining a block_size variable.
2515 * This avoids requiring division by a variable at runtime
2516 * (which would be marginally less efficient and would require
2517 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002518 */
2519 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002520
2521 /*
2522 * The next two sizes are the minimum and maximum values of
2523 * in_msglen over all padlen values.
2524 *
2525 * They're independent of padlen, since we previously did
2526 * in_msglen -= padlen.
2527 *
2528 * Note that max_len + maclen is never more than the buffer
2529 * length, as we previously did in_msglen -= maclen too.
2530 */
2531 const size_t max_len = ssl->in_msglen + padlen;
2532 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2533
Hanno Beckere694c3e2017-12-27 21:34:08 +00002534 switch( ssl->handshake->ciphersuite_info->mac )
Gilles Peskine20b44082018-05-29 14:06:49 +02002535 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002536#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2537 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002538 case MBEDTLS_MD_MD5:
2539 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002540 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002541 /* 8 bytes of message size, 64-byte compression blocks */
2542 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2543 ( 13 + ssl->in_msglen + 8 ) / 64;
2544 break;
2545#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002546#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002548 /* 16 bytes of message size, 128-byte compression blocks */
2549 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2550 ( 13 + ssl->in_msglen + 16 ) / 128;
2551 break;
2552#endif
2553 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2556 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002557
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002558 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2561 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2562 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2563 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002564 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002565 /* Make sure we access everything even when padlen > 0. This
2566 * makes the synchronisation requirements for just-in-time
2567 * Prime+Probe attacks much tighter and hopefully impractical. */
2568 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002569 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002570
2571 /* Call mbedtls_md_process at least once due to cache attacks
2572 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002573 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002577
2578 /* Make sure we access all the memory that could contain the MAC,
2579 * before we check it in the next code block. This makes the
2580 * synchronisation requirements for just-in-time Prime+Probe
2581 * attacks much tighter and hopefully impractical. */
2582 ssl_read_memory( ssl->in_msg + min_len,
2583 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002584 }
2585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2587 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002593#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002594 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2595 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2596 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Hanno Becker992b6872017-11-09 18:57:39 +00002599 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2600 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SSL_DEBUG_ALL)
2603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002604#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002605 correct = 0;
2606 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002607 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002609
2610 /*
2611 * Finally check the correct flag
2612 */
2613 if( correct == 0 )
2614 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002615#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002616
2617 /* Make extra sure authentication was performed, exactly once */
2618 if( auth_done != 1 )
2619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 if( ssl->in_msglen == 0 )
2625 {
Angus Gratton34817922018-06-19 15:58:22 +10002626#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2627 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2628 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2629 {
2630 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2633 }
2634#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2635
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 ssl->nb_zero++;
2637
2638 /*
2639 * Three or more empty messages may be a DoS attack
2640 * (excessive CPU consumption).
2641 */
2642 if( ssl->nb_zero > 3 )
2643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 }
2648 }
2649 else
2650 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002653 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002654 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002655 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002656 }
2657 else
2658#endif
2659 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002660 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002661 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2662 if( ++ssl->in_ctr[i - 1] != 0 )
2663 break;
2664
2665 /* The loop goes to its end iff the counter is wrapping */
2666 if( i == ssl_ep_len( ssl ) )
2667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2669 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002670 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002671 }
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 return( 0 );
2676}
2677
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002678#undef MAC_NONE
2679#undef MAC_PLAINTEXT
2680#undef MAC_CIPHERTEXT
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002683/*
2684 * Compression/decompression functions
2685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002687{
2688 int ret;
2689 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002690 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002692 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002695
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002696 if( len_pre == 0 )
2697 return( 0 );
2698
Paul Bakker2770fbd2012-07-03 13:30:23 +00002699 memcpy( msg_pre, ssl->out_msg, len_pre );
2700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702 ssl->out_msglen ) );
2703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705 ssl->out_msg, ssl->out_msglen );
2706
Paul Bakker48916f92012-09-16 19:57:18 +00002707 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2708 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2709 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002710 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002711
Paul Bakker48916f92012-09-16 19:57:18 +00002712 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713 if( ret != Z_OK )
2714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2716 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002717 }
2718
Angus Grattond8213d02016-05-25 20:56:48 +10002719 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002720 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002723 ssl->out_msglen ) );
2724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002726 ssl->out_msg, ssl->out_msglen );
2727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002729
2730 return( 0 );
2731}
2732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734{
2735 int ret;
2736 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002737 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002738 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002739 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002742
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002743 if( len_pre == 0 )
2744 return( 0 );
2745
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746 memcpy( msg_pre, ssl->in_msg, len_pre );
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749 ssl->in_msglen ) );
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752 ssl->in_msg, ssl->in_msglen );
2753
Paul Bakker48916f92012-09-16 19:57:18 +00002754 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2755 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2756 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002757 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002758 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759
Paul Bakker48916f92012-09-16 19:57:18 +00002760 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761 if( ret != Z_OK )
2762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2764 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002765 }
2766
Angus Grattond8213d02016-05-25 20:56:48 +10002767 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002768 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771 ssl->in_msglen ) );
2772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002774 ssl->in_msg, ssl->in_msglen );
2775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777
2778 return( 0 );
2779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2783static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785#if defined(MBEDTLS_SSL_PROTO_DTLS)
2786static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002787{
2788 /* If renegotiation is not enforced, retransmit until we would reach max
2789 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002790 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002791 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002792 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002793 unsigned char doublings = 1;
2794
2795 while( ratio != 0 )
2796 {
2797 ++doublings;
2798 ratio >>= 1;
2799 }
2800
2801 if( ++ssl->renego_records_seen > doublings )
2802 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002804 return( 0 );
2805 }
2806 }
2807
2808 return( ssl_write_hello_request( ssl ) );
2809}
2810#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002812
Paul Bakker5121ce52009-01-03 21:22:43 +00002813/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002814 * Fill the input message buffer by appending data to it.
2815 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002816 *
2817 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2818 * available (from this read and/or a previous one). Otherwise, an error code
2819 * is returned (possibly EOF or WANT_READ).
2820 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002821 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2822 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2823 * since we always read a whole datagram at once.
2824 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002825 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002826 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002829{
Paul Bakker23986e52011-04-24 08:57:21 +00002830 int ret;
2831 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002835 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002838 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002840 }
2841
Angus Grattond8213d02016-05-25 20:56:48 +10002842 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002846 }
2847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002851 uint32_t timeout;
2852
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002853 /* Just to be sure */
2854 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2855 {
2856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2857 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2859 }
2860
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002861 /*
2862 * The point is, we need to always read a full datagram at once, so we
2863 * sometimes read more then requested, and handle the additional data.
2864 * It could be the rest of the current record (while fetching the
2865 * header) and/or some other records in the same datagram.
2866 */
2867
2868 /*
2869 * Move to the next record in the already read datagram if applicable
2870 */
2871 if( ssl->next_record_offset != 0 )
2872 {
2873 if( ssl->in_left < ssl->next_record_offset )
2874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002877 }
2878
2879 ssl->in_left -= ssl->next_record_offset;
2880
2881 if( ssl->in_left != 0 )
2882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002884 ssl->next_record_offset ) );
2885 memmove( ssl->in_hdr,
2886 ssl->in_hdr + ssl->next_record_offset,
2887 ssl->in_left );
2888 }
2889
2890 ssl->next_record_offset = 0;
2891 }
2892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002895
2896 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002897 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002898 */
2899 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002902 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002903 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002904
2905 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002906 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002907 * are not at the beginning of a new record, the caller did something
2908 * wrong.
2909 */
2910 if( ssl->in_left != 0 )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002914 }
2915
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002916 /*
2917 * Don't even try to read if time's out already.
2918 * This avoids by-passing the timer when repeatedly receiving messages
2919 * that will end up being dropped.
2920 */
2921 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002922 {
2923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002924 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002925 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002926 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002927 {
Angus Grattond8213d02016-05-25 20:56:48 +10002928 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002931 timeout = ssl->handshake->retransmit_timeout;
2932 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002933 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002936
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002937 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002938 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2939 timeout );
2940 else
2941 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002944
2945 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002947 }
2948
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002949 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002952 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002955 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002956 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002959 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002960 }
2961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002965 return( ret );
2966 }
2967
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002968 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002971 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002973 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002974 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002977 return( ret );
2978 }
2979
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002980 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002983 }
2984
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 if( ret < 0 )
2986 return( ret );
2987
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002988 ssl->in_left = ret;
2989 }
2990 else
2991#endif
2992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002994 ssl->in_left, nb_want ) );
2995
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002996 while( ssl->in_left < nb_want )
2997 {
2998 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002999
3000 if( ssl_check_timer( ssl ) != 0 )
3001 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3002 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003003 {
3004 if( ssl->f_recv_timeout != NULL )
3005 {
3006 ret = ssl->f_recv_timeout( ssl->p_bio,
3007 ssl->in_hdr + ssl->in_left, len,
3008 ssl->conf->read_timeout );
3009 }
3010 else
3011 {
3012 ret = ssl->f_recv( ssl->p_bio,
3013 ssl->in_hdr + ssl->in_left, len );
3014 }
3015 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003018 ssl->in_left, nb_want ) );
3019 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003020
3021 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003023
3024 if( ret < 0 )
3025 return( ret );
3026
mohammad160352aecb92018-03-28 23:41:40 -07003027 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003028 {
Darryl Green11999bb2018-03-13 15:22:58 +00003029 MBEDTLS_SSL_DEBUG_MSG( 1,
3030 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003031 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3033 }
3034
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003035 ssl->in_left += ret;
3036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 }
3038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
3041 return( 0 );
3042}
3043
3044/*
3045 * Flush any data not yet written
3046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003049 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003050 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003054 if( ssl->f_send == NULL )
3055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003057 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003059 }
3060
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003061 /* Avoid incrementing counter if data is flushed */
3062 if( ssl->out_left == 0 )
3063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003065 return( 0 );
3066 }
3067
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 while( ssl->out_left > 0 )
3069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3071 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Hanno Becker2b1e3542018-08-06 11:19:13 +01003073 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003074 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
3078 if( ret <= 0 )
3079 return( ret );
3080
mohammad160352aecb92018-03-28 23:41:40 -07003081 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003082 {
Darryl Green11999bb2018-03-13 15:22:58 +00003083 MBEDTLS_SSL_DEBUG_MSG( 1,
3084 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003085 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3087 }
3088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 ssl->out_left -= ret;
3090 }
3091
Hanno Becker2b1e3542018-08-06 11:19:13 +01003092#if defined(MBEDTLS_SSL_PROTO_DTLS)
3093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003094 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003095 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003096 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003097 else
3098#endif
3099 {
3100 ssl->out_hdr = ssl->out_buf + 8;
3101 }
3102 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
3106 return( 0 );
3107}
3108
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003109/*
3110 * Functions to handle the DTLS retransmission state machine
3111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113/*
3114 * Append current handshake message to current outgoing flight
3115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3120 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3121 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003122
3123 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003124 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129 }
3130
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003131 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003135 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 }
3137
3138 /* Copy current handshake message with headers */
3139 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3140 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003141 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003142 msg->next = NULL;
3143
3144 /* Append to the current flight */
3145 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003146 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147 else
3148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150 while( cur->next != NULL )
3151 cur = cur->next;
3152 cur->next = msg;
3153 }
3154
Hanno Becker3b235902018-08-06 09:54:53 +01003155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003156 return( 0 );
3157}
3158
3159/*
3160 * Free the current flight of handshake messages
3161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 mbedtls_ssl_flight_item *cur = flight;
3165 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166
3167 while( cur != NULL )
3168 {
3169 next = cur->next;
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_free( cur->p );
3172 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173
3174 cur = next;
3175 }
3176}
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3179static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003180#endif
3181
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003182/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003183 * Swap transform_out and out_ctr with the alternative ones
3184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003188 unsigned char tmp_out_ctr[8];
3189
3190 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193 return;
3194 }
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003197
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003198 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003199 tmp_transform = ssl->transform_out;
3200 ssl->transform_out = ssl->handshake->alt_transform_out;
3201 ssl->handshake->alt_transform_out = tmp_transform;
3202
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003203 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003204 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3205 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003206 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003207
3208 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003209 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3212 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3217 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003218 }
3219 }
3220#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003221}
3222
3223/*
3224 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003225 */
3226int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3227{
3228 int ret = 0;
3229
3230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3231
3232 ret = mbedtls_ssl_flight_transmit( ssl );
3233
3234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3235
3236 return( ret );
3237}
3238
3239/*
3240 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003241 *
3242 * Need to remember the current message in case flush_output returns
3243 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003244 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003245 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003246int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003247{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003248 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003254
3255 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003256 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003257 ssl_swap_epochs( ssl );
3258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003260 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261
3262 while( ssl->handshake->cur_msg != NULL )
3263 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003264 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003265 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003266
Hanno Beckere1dcb032018-08-17 16:47:58 +01003267 int const is_finished =
3268 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3269 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3270
Hanno Becker04da1892018-08-14 13:22:10 +01003271 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3272 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3273
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003274 /* Swap epochs before sending Finished: we can't do it after
3275 * sending ChangeCipherSpec, in case write returns WANT_READ.
3276 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003277 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003278 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003280 ssl_swap_epochs( ssl );
3281 }
3282
Hanno Becker67bc7c32018-08-06 11:33:50 +01003283 ret = ssl_get_remaining_payload_in_datagram( ssl );
3284 if( ret < 0 )
3285 return( ret );
3286 max_frag_len = (size_t) ret;
3287
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003288 /* CCS is copied as is, while HS messages may need fragmentation */
3289 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3290 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003291 if( max_frag_len == 0 )
3292 {
3293 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3294 return( ret );
3295
3296 continue;
3297 }
3298
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003299 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003300 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003301 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003303 /* Update position inside current message */
3304 ssl->handshake->cur_msg_p += cur->len;
3305 }
3306 else
3307 {
3308 const unsigned char * const p = ssl->handshake->cur_msg_p;
3309 const size_t hs_len = cur->len - 12;
3310 const size_t frag_off = p - ( cur->p + 12 );
3311 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003312 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003313
Hanno Beckere1dcb032018-08-17 16:47:58 +01003314 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003315 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003316 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003317 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3320 return( ret );
3321
3322 continue;
3323 }
3324 max_hs_frag_len = max_frag_len - 12;
3325
3326 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3327 max_hs_frag_len : rem_len;
3328
3329 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003330 {
3331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003332 (unsigned) cur_hs_frag_len,
3333 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003334 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003335
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003336 /* Messages are stored with handshake headers as if not fragmented,
3337 * copy beginning of headers then fill fragmentation fields.
3338 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3339 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003341 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3342 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3343 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3344
Hanno Becker67bc7c32018-08-06 11:33:50 +01003345 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3346 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3347 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003348
3349 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3350
Hanno Becker3f7b9732018-08-28 09:53:25 +01003351 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3353 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003354 ssl->out_msgtype = cur->type;
3355
3356 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003357 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003358 }
3359
3360 /* If done with the current message move to the next one if any */
3361 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3362 {
3363 if( cur->next != NULL )
3364 {
3365 ssl->handshake->cur_msg = cur->next;
3366 ssl->handshake->cur_msg_p = cur->next->p + 12;
3367 }
3368 else
3369 {
3370 ssl->handshake->cur_msg = NULL;
3371 ssl->handshake->cur_msg_p = NULL;
3372 }
3373 }
3374
3375 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003376 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003379 return( ret );
3380 }
3381 }
3382
Hanno Becker67bc7c32018-08-06 11:33:50 +01003383 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3384 return( ret );
3385
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003386 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3388 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003389 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3393 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003394
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003396
3397 return( 0 );
3398}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003399
3400/*
3401 * To be called when the last message of an incoming flight is received.
3402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003404{
3405 /* We won't need to resend that one any more */
3406 ssl_flight_free( ssl->handshake->flight );
3407 ssl->handshake->flight = NULL;
3408 ssl->handshake->cur_msg = NULL;
3409
3410 /* The next incoming flight will start with this msg_seq */
3411 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3412
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003413 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003414 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003415
Hanno Becker0271f962018-08-16 13:23:47 +01003416 /* Clear future message buffering structure. */
3417 ssl_buffering_free( ssl );
3418
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003419 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003420 ssl_set_timer( ssl, 0 );
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3423 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003426 }
3427 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003430
3431/*
3432 * To be called when the last message of an outgoing flight is send.
3433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003435{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003436 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003437 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3440 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003443 }
3444 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448
Paul Bakker5121ce52009-01-03 21:22:43 +00003449/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003450 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452
3453/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003454 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003455 *
3456 * - fill in handshake headers
3457 * - update handshake checksum
3458 * - DTLS: save message for resending
3459 * - then pass to the record layer
3460 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003461 * DTLS: except for HelloRequest, messages are only queued, and will only be
3462 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003463 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003464 * Inputs:
3465 * - ssl->out_msglen: 4 + actual handshake message len
3466 * (4 is the size of handshake headers for TLS)
3467 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3468 * - ssl->out_msg + 4: the handshake message body
3469 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003470 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003471 * - ssl->out_msglen: the length of the record contents
3472 * (including handshake headers but excluding record headers)
3473 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003474 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003475int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003476{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003477 int ret;
3478 const size_t hs_len = ssl->out_msglen - 4;
3479 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3482
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003483 /*
3484 * Sanity checks
3485 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003486 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003487 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3488 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003489 /* In SSLv3, the client might send a NoCertificate alert. */
3490#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3491 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3492 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3493 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3494#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3495 {
3496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3498 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003500
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003501 /* Whenever we send anything different from a
3502 * HelloRequest we should be in a handshake - double check. */
3503 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3504 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003505 ssl->handshake == NULL )
3506 {
3507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003513 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003515 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003518 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003520
Hanno Beckerb50a2532018-08-06 11:52:54 +01003521 /* Double-check that we did not exceed the bounds
3522 * of the outgoing record buffer.
3523 * This should never fail as the various message
3524 * writing functions must obey the bounds of the
3525 * outgoing record buffer, but better be safe.
3526 *
3527 * Note: We deliberately do not check for the MTU or MFL here.
3528 */
3529 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3530 {
3531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3532 "size %u, maximum %u",
3533 (unsigned) ssl->out_msglen,
3534 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3536 }
3537
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003538 /*
3539 * Fill handshake headers
3540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003543 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3544 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3545 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003547 /*
3548 * DTLS has additional fields in the Handshake layer,
3549 * between the length field and the actual payload:
3550 * uint16 message_seq;
3551 * uint24 fragment_offset;
3552 * uint24 fragment_length;
3553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003556 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003557 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003558 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003559 {
3560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3561 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003562 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003563 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3565 }
3566
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003567 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003568 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003569
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003570 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003571 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003572 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003573 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3574 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3575 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003576 }
3577 else
3578 {
3579 ssl->out_msg[4] = 0;
3580 ssl->out_msg[5] = 0;
3581 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003582
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003583 /* Handshake hashes are computed without fragmentation,
3584 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003585 memset( ssl->out_msg + 6, 0x00, 3 );
3586 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003589
Hanno Becker0207e532018-08-28 10:28:28 +01003590 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003591 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3592 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 }
3594
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003595 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003598 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3599 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600 {
3601 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 return( ret );
3605 }
3606 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003607 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003609 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003610 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003611 {
3612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3613 return( ret );
3614 }
3615 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003616
3617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3618
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003619 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003620}
3621
3622/*
3623 * Record layer functions
3624 */
3625
3626/*
3627 * Write current record.
3628 *
3629 * Uses:
3630 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3631 * - ssl->out_msglen: length of the record content (excl headers)
3632 * - ssl->out_msg: record content
3633 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003634int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003635{
3636 int ret, done = 0;
3637 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003638 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003639
3640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003643 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003645 {
3646 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649 return( ret );
3650 }
3651
3652 len = ssl->out_msglen;
3653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3657 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 ret = mbedtls_ssl_hw_record_write( ssl );
3662 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3665 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003666 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003667
3668 if( ret == 0 )
3669 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003670 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003672 if( !done )
3673 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003674 unsigned i;
3675 size_t protected_record_size;
3676
Paul Bakker05ef8352012-05-08 09:17:57 +00003677 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003679 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003680
Hanno Becker19859472018-08-06 09:40:20 +01003681 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003682 ssl->out_len[0] = (unsigned char)( len >> 8 );
3683 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003684
Paul Bakker48916f92012-09-16 19:57:18 +00003685 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003686 {
3687 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003690 return( ret );
3691 }
3692
3693 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003694 ssl->out_len[0] = (unsigned char)( len >> 8 );
3695 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003696 }
3697
Hanno Becker2b1e3542018-08-06 11:19:13 +01003698 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3699
3700#if defined(MBEDTLS_SSL_PROTO_DTLS)
3701 /* In case of DTLS, double-check that we don't exceed
3702 * the remaining space in the datagram. */
3703 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3704 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003705 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003706 if( ret < 0 )
3707 return( ret );
3708
3709 if( protected_record_size > (size_t) ret )
3710 {
3711 /* Should never happen */
3712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3713 }
3714 }
3715#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003718 "version = [%d:%d], msglen = %d",
3719 ssl->out_hdr[0], ssl->out_hdr[1],
3720 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003723 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003724
3725 ssl->out_left += protected_record_size;
3726 ssl->out_hdr += protected_record_size;
3727 ssl_update_out_pointers( ssl, ssl->transform_out );
3728
Hanno Becker04484622018-08-06 09:49:38 +01003729 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3730 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3731 break;
3732
3733 /* The loop goes to its end iff the counter is wrapping */
3734 if( i == ssl_ep_len( ssl ) )
3735 {
3736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3737 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 }
3740
Hanno Becker67bc7c32018-08-06 11:33:50 +01003741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3743 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003744 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003745 size_t remaining;
3746 ret = ssl_get_remaining_payload_in_datagram( ssl );
3747 if( ret < 0 )
3748 {
3749 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3750 ret );
3751 return( ret );
3752 }
3753
3754 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003755 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003756 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003757 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003758 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003759 else
3760 {
Hanno Becker513815a2018-08-20 11:56:09 +01003761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 }
3763 }
3764#endif /* MBEDTLS_SSL_PROTO_DTLS */
3765
3766 if( ( flush == SSL_FORCE_FLUSH ) &&
3767 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 return( ret );
3771 }
3772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
3775 return( 0 );
3776}
3777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003779
3780static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3781{
3782 if( ssl->in_msglen < ssl->in_hslen ||
3783 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3784 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3785 {
3786 return( 1 );
3787 }
3788 return( 0 );
3789}
Hanno Becker44650b72018-08-16 12:51:11 +01003790
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003791static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003792{
3793 return( ( ssl->in_msg[9] << 16 ) |
3794 ( ssl->in_msg[10] << 8 ) |
3795 ssl->in_msg[11] );
3796}
3797
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003798static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003799{
3800 return( ( ssl->in_msg[6] << 16 ) |
3801 ( ssl->in_msg[7] << 8 ) |
3802 ssl->in_msg[8] );
3803}
3804
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003805static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003806{
3807 uint32_t msg_len, frag_off, frag_len;
3808
3809 msg_len = ssl_get_hs_total_len( ssl );
3810 frag_off = ssl_get_hs_frag_off( ssl );
3811 frag_len = ssl_get_hs_frag_len( ssl );
3812
3813 if( frag_off > msg_len )
3814 return( -1 );
3815
3816 if( frag_len > msg_len - frag_off )
3817 return( -1 );
3818
3819 if( frag_len + 12 > ssl->in_msglen )
3820 return( -1 );
3821
3822 return( 0 );
3823}
3824
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003825/*
3826 * Mark bits in bitmask (used for DTLS HS reassembly)
3827 */
3828static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3829{
3830 unsigned int start_bits, end_bits;
3831
3832 start_bits = 8 - ( offset % 8 );
3833 if( start_bits != 8 )
3834 {
3835 size_t first_byte_idx = offset / 8;
3836
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003837 /* Special case */
3838 if( len <= start_bits )
3839 {
3840 for( ; len != 0; len-- )
3841 mask[first_byte_idx] |= 1 << ( start_bits - len );
3842
3843 /* Avoid potential issues with offset or len becoming invalid */
3844 return;
3845 }
3846
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003847 offset += start_bits; /* Now offset % 8 == 0 */
3848 len -= start_bits;
3849
3850 for( ; start_bits != 0; start_bits-- )
3851 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3852 }
3853
3854 end_bits = len % 8;
3855 if( end_bits != 0 )
3856 {
3857 size_t last_byte_idx = ( offset + len ) / 8;
3858
3859 len -= end_bits; /* Now len % 8 == 0 */
3860
3861 for( ; end_bits != 0; end_bits-- )
3862 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3863 }
3864
3865 memset( mask + offset / 8, 0xFF, len / 8 );
3866}
3867
3868/*
3869 * Check that bitmask is full
3870 */
3871static int ssl_bitmask_check( unsigned char *mask, size_t len )
3872{
3873 size_t i;
3874
3875 for( i = 0; i < len / 8; i++ )
3876 if( mask[i] != 0xFF )
3877 return( -1 );
3878
3879 for( i = 0; i < len % 8; i++ )
3880 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3881 return( -1 );
3882
3883 return( 0 );
3884}
3885
Hanno Becker56e205e2018-08-16 09:06:12 +01003886/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003887static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003888 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003889{
Hanno Becker56e205e2018-08-16 09:06:12 +01003890 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003891
Hanno Becker56e205e2018-08-16 09:06:12 +01003892 alloc_len = 12; /* Handshake header */
3893 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003894
Hanno Beckerd07df862018-08-16 09:14:58 +01003895 if( add_bitmap )
3896 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003897
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003898 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003899}
Hanno Becker56e205e2018-08-16 09:06:12 +01003900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003902
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003903static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003904{
3905 return( ( ssl->in_msg[1] << 16 ) |
3906 ( ssl->in_msg[2] << 8 ) |
3907 ssl->in_msg[3] );
3908}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003909
Simon Butcher99000142016-10-13 17:21:01 +01003910int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003915 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003917 }
3918
Hanno Becker12555c62018-08-16 12:47:53 +01003919 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003922 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003923 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003926 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003927 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003928 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003929 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003930
Hanno Becker44650b72018-08-16 12:51:11 +01003931 if( ssl_check_hs_header( ssl ) != 0 )
3932 {
3933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3935 }
3936
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003937 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003938 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3939 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3940 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3941 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003942 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003943 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3944 {
3945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3946 recv_msg_seq,
3947 ssl->handshake->in_msg_seq ) );
3948 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3949 }
3950
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003951 /* Retransmit only on last message from previous flight, to avoid
3952 * too many retransmissions.
3953 * Besides, No sane server ever retransmits HelloVerifyRequest */
3954 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003958 "message_seq = %d, start_of_flight = %d",
3959 recv_msg_seq,
3960 ssl->handshake->in_flight_start_seq ) );
3961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003965 return( ret );
3966 }
3967 }
3968 else
3969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003971 "message_seq = %d, expected = %d",
3972 recv_msg_seq,
3973 ssl->handshake->in_msg_seq ) );
3974 }
3975
Hanno Becker90333da2017-10-10 11:27:13 +01003976 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003977 }
3978 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003979
Hanno Becker6d97ef52018-08-16 13:09:04 +01003980 /* Message reassembly is handled alongside buffering of future
3981 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003982 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003983 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003984 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003987 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003988 }
3989 }
3990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003992 /* With TLS we don't handle fragmentation (for now) */
3993 if( ssl->in_msglen < ssl->in_hslen )
3994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3996 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003997 }
3998
Simon Butcher99000142016-10-13 17:21:01 +01003999 return( 0 );
4000}
4001
4002void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4003{
Hanno Becker0271f962018-08-16 13:23:47 +01004004 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004005
Hanno Becker0271f962018-08-16 13:23:47 +01004006 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004007 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004008 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004009 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004010
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004011 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004013 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004014 ssl->handshake != NULL )
4015 {
Hanno Becker0271f962018-08-16 13:23:47 +01004016 unsigned offset;
4017 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004018
Hanno Becker0271f962018-08-16 13:23:47 +01004019 /* Increment handshake sequence number */
4020 hs->in_msg_seq++;
4021
4022 /*
4023 * Clear up handshake buffering and reassembly structure.
4024 */
4025
4026 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004027 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004028
4029 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004030 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4031 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004032 offset++, hs_buf++ )
4033 {
4034 *hs_buf = *(hs_buf + 1);
4035 }
4036
4037 /* Create a fresh last entry */
4038 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004039 }
4040#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004041}
4042
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004043/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004044 * DTLS anti-replay: RFC 6347 4.1.2.6
4045 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004046 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4047 * Bit n is set iff record number in_window_top - n has been seen.
4048 *
4049 * Usually, in_window_top is the last record number seen and the lsb of
4050 * in_window is set. The only exception is the initial state (record number 0
4051 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4054static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004055{
4056 ssl->in_window_top = 0;
4057 ssl->in_window = 0;
4058}
4059
4060static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4061{
4062 return( ( (uint64_t) buf[0] << 40 ) |
4063 ( (uint64_t) buf[1] << 32 ) |
4064 ( (uint64_t) buf[2] << 24 ) |
4065 ( (uint64_t) buf[3] << 16 ) |
4066 ( (uint64_t) buf[4] << 8 ) |
4067 ( (uint64_t) buf[5] ) );
4068}
4069
4070/*
4071 * Return 0 if sequence number is acceptable, -1 otherwise
4072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004074{
4075 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4076 uint64_t bit;
4077
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004078 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004079 return( 0 );
4080
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004081 if( rec_seqnum > ssl->in_window_top )
4082 return( 0 );
4083
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004084 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004085
4086 if( bit >= 64 )
4087 return( -1 );
4088
4089 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4090 return( -1 );
4091
4092 return( 0 );
4093}
4094
4095/*
4096 * Update replay window on new validated record
4097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004099{
4100 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4101
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004102 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004103 return;
4104
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004105 if( rec_seqnum > ssl->in_window_top )
4106 {
4107 /* Update window_top and the contents of the window */
4108 uint64_t shift = rec_seqnum - ssl->in_window_top;
4109
4110 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004111 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004113 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004114 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004115 ssl->in_window |= 1;
4116 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004117
4118 ssl->in_window_top = rec_seqnum;
4119 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004120 else
4121 {
4122 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004123 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004124
4125 if( bit < 64 ) /* Always true, but be extra sure */
4126 ssl->in_window |= (uint64_t) 1 << bit;
4127 }
4128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004130
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004131#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004132/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004133static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4134
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004135/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004136 * Without any SSL context, check if a datagram looks like a ClientHello with
4137 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004138 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004139 *
4140 * - if cookie is valid, return 0
4141 * - if ClientHello looks superficially valid but cookie is not,
4142 * fill obuf and set olen, then
4143 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4144 * - otherwise return a specific error code
4145 */
4146static int ssl_check_dtls_clihlo_cookie(
4147 mbedtls_ssl_cookie_write_t *f_cookie_write,
4148 mbedtls_ssl_cookie_check_t *f_cookie_check,
4149 void *p_cookie,
4150 const unsigned char *cli_id, size_t cli_id_len,
4151 const unsigned char *in, size_t in_len,
4152 unsigned char *obuf, size_t buf_len, size_t *olen )
4153{
4154 size_t sid_len, cookie_len;
4155 unsigned char *p;
4156
4157 if( f_cookie_write == NULL || f_cookie_check == NULL )
4158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4159
4160 /*
4161 * Structure of ClientHello with record and handshake headers,
4162 * and expected values. We don't need to check a lot, more checks will be
4163 * done when actually parsing the ClientHello - skipping those checks
4164 * avoids code duplication and does not make cookie forging any easier.
4165 *
4166 * 0-0 ContentType type; copied, must be handshake
4167 * 1-2 ProtocolVersion version; copied
4168 * 3-4 uint16 epoch; copied, must be 0
4169 * 5-10 uint48 sequence_number; copied
4170 * 11-12 uint16 length; (ignored)
4171 *
4172 * 13-13 HandshakeType msg_type; (ignored)
4173 * 14-16 uint24 length; (ignored)
4174 * 17-18 uint16 message_seq; copied
4175 * 19-21 uint24 fragment_offset; copied, must be 0
4176 * 22-24 uint24 fragment_length; (ignored)
4177 *
4178 * 25-26 ProtocolVersion client_version; (ignored)
4179 * 27-58 Random random; (ignored)
4180 * 59-xx SessionID session_id; 1 byte len + sid_len content
4181 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4182 * ...
4183 *
4184 * Minimum length is 61 bytes.
4185 */
4186 if( in_len < 61 ||
4187 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4188 in[3] != 0 || in[4] != 0 ||
4189 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4190 {
4191 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4192 }
4193
4194 sid_len = in[59];
4195 if( sid_len > in_len - 61 )
4196 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4197
4198 cookie_len = in[60 + sid_len];
4199 if( cookie_len > in_len - 60 )
4200 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4201
4202 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4203 cli_id, cli_id_len ) == 0 )
4204 {
4205 /* Valid cookie */
4206 return( 0 );
4207 }
4208
4209 /*
4210 * If we get here, we've got an invalid cookie, let's prepare HVR.
4211 *
4212 * 0-0 ContentType type; copied
4213 * 1-2 ProtocolVersion version; copied
4214 * 3-4 uint16 epoch; copied
4215 * 5-10 uint48 sequence_number; copied
4216 * 11-12 uint16 length; olen - 13
4217 *
4218 * 13-13 HandshakeType msg_type; hello_verify_request
4219 * 14-16 uint24 length; olen - 25
4220 * 17-18 uint16 message_seq; copied
4221 * 19-21 uint24 fragment_offset; copied
4222 * 22-24 uint24 fragment_length; olen - 25
4223 *
4224 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4225 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4226 *
4227 * Minimum length is 28.
4228 */
4229 if( buf_len < 28 )
4230 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4231
4232 /* Copy most fields and adapt others */
4233 memcpy( obuf, in, 25 );
4234 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4235 obuf[25] = 0xfe;
4236 obuf[26] = 0xff;
4237
4238 /* Generate and write actual cookie */
4239 p = obuf + 28;
4240 if( f_cookie_write( p_cookie,
4241 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4242 {
4243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4244 }
4245
4246 *olen = p - obuf;
4247
4248 /* Go back and fill length fields */
4249 obuf[27] = (unsigned char)( *olen - 28 );
4250
4251 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4252 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4253 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4254
4255 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4256 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4257
4258 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4259}
4260
4261/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004262 * Handle possible client reconnect with the same UDP quadruplet
4263 * (RFC 6347 Section 4.2.8).
4264 *
4265 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4266 * that looks like a ClientHello.
4267 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004268 * - if the input looks like a ClientHello without cookies,
4269 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004270 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004271 * - if the input looks like a ClientHello with a valid cookie,
4272 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004273 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004274 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004275 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004276 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004277 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4278 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004279 */
4280static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4281{
4282 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004283 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004284
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004285 ret = ssl_check_dtls_clihlo_cookie(
4286 ssl->conf->f_cookie_write,
4287 ssl->conf->f_cookie_check,
4288 ssl->conf->p_cookie,
4289 ssl->cli_id, ssl->cli_id_len,
4290 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004291 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004292
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004293 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4294
4295 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004296 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004297 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004298 * If the error is permanent we'll catch it later,
4299 * if it's not, then hopefully it'll work next time. */
4300 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4301
4302 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004303 }
4304
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004305 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004306 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004307 /* Got a valid cookie, partially reset context */
4308 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4309 {
4310 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4311 return( ret );
4312 }
4313
4314 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004315 }
4316
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004317 return( ret );
4318}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004319#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004322 * ContentType type;
4323 * ProtocolVersion version;
4324 * uint16 epoch; // DTLS only
4325 * uint48 sequence_number; // DTLS only
4326 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004327 *
4328 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004329 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004330 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4331 *
4332 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004333 * 1. proceed with the record if this function returns 0
4334 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4335 * 3. return CLIENT_RECONNECT if this function return that value
4336 * 4. drop the whole datagram if this function returns anything else.
4337 * Point 2 is needed when the peer is resending, and we have already received
4338 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004342 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344 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 +02004345
Paul Bakker5121ce52009-01-03 21:22:43 +00004346 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004347 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004348 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004352 ssl->in_msgtype,
4353 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004354
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004355 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4357 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4358 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4359 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004362
4363#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004364 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4365 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004366 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4367#endif /* MBEDTLS_SSL_PROTO_DTLS */
4368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4369 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004372 }
4373
4374 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004375 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 }
4380
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004381 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4384 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 }
4386
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004387 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004388 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004389 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004393 }
4394
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004395 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004396 * DTLS-related tests.
4397 * Check epoch before checking length constraint because
4398 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4399 * message gets duplicated before the corresponding Finished message,
4400 * the second ChangeCipherSpec should be discarded because it belongs
4401 * to an old epoch, but not because its length is shorter than
4402 * the minimum record length for packets using the new record transform.
4403 * Note that these two kinds of failures are handled differently,
4404 * as an unexpected record is silently skipped but an invalid
4405 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004406 */
4407#if defined(MBEDTLS_SSL_PROTO_DTLS)
4408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4409 {
4410 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4411
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004412 /* Check epoch (and sequence number) with DTLS */
4413 if( rec_epoch != ssl->in_epoch )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4416 "expected %d, received %d",
4417 ssl->in_epoch, rec_epoch ) );
4418
4419#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4420 /*
4421 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4422 * access the first byte of record content (handshake type), as we
4423 * have an active transform (possibly iv_len != 0), so use the
4424 * fact that the record header len is 13 instead.
4425 */
4426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4427 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4428 rec_epoch == 0 &&
4429 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4430 ssl->in_left > 13 &&
4431 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4432 {
4433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4434 "from the same port" ) );
4435 return( ssl_handle_possible_reconnect( ssl ) );
4436 }
4437 else
4438#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004439 {
4440 /* Consider buffering the record. */
4441 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4442 {
4443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4444 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4445 }
4446
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004447 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004448 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004449 }
4450
4451#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4452 /* Replay detection only works for the current epoch */
4453 if( rec_epoch == ssl->in_epoch &&
4454 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4457 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4458 }
4459#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004460
Hanno Becker52c6dc62017-05-26 16:07:36 +01004461 /* Drop unexpected ApplicationData records,
4462 * except at the beginning of renegotiations */
4463 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4464 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4465#if defined(MBEDTLS_SSL_RENEGOTIATION)
4466 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4467 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4468#endif
4469 )
4470 {
4471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4472 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4473 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004474 }
4475#endif /* MBEDTLS_SSL_PROTO_DTLS */
4476
Hanno Becker52c6dc62017-05-26 16:07:36 +01004477
4478 /* Check length against bounds of the current transform and version */
4479 if( ssl->transform_in == NULL )
4480 {
4481 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004482 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004483 {
4484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4485 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4486 }
4487 }
4488 else
4489 {
4490 if( ssl->in_msglen < ssl->transform_in->minlen )
4491 {
4492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4494 }
4495
4496#if defined(MBEDTLS_SSL_PROTO_SSL3)
4497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004498 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004499 {
4500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4502 }
4503#endif
4504#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4505 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4506 /*
4507 * TLS encrypted messages can have up to 256 bytes of padding
4508 */
4509 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4510 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004511 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004512 {
4513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4515 }
4516#endif
4517 }
4518
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004519 return( 0 );
4520}
Paul Bakker5121ce52009-01-03 21:22:43 +00004521
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004522/*
4523 * If applicable, decrypt (and decompress) record content
4524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004526{
4527 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4530 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4533 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 ret = mbedtls_ssl_hw_record_read( ssl );
4538 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4541 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004542 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004543
4544 if( ret == 0 )
4545 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004548 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 {
4550 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004553 return( ret );
4554 }
4555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004557 ssl->in_msg, ssl->in_msglen );
4558
Angus Grattond8213d02016-05-25 20:56:48 +10004559 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4562 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 }
4564 }
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004567 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004569 {
4570 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004573 return( ret );
4574 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004582 }
4583#endif
4584
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004585 return( 0 );
4586}
4587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004589
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004590/*
4591 * Read a record.
4592 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004593 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4594 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4595 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004596 */
Hanno Becker1097b342018-08-15 14:09:41 +01004597
4598/* Helper functions for mbedtls_ssl_read_record(). */
4599static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004600static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4601static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004602
Hanno Becker327c93b2018-08-15 13:56:18 +01004603int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004604 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004605{
4606 int ret;
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004609
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004610 if( ssl->keep_current_message == 0 )
4611 {
4612 do {
Simon Butcher99000142016-10-13 17:21:01 +01004613
Hanno Becker26994592018-08-15 14:14:59 +01004614 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004615 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004616 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004617
Hanno Beckere74d5562018-08-15 14:26:08 +01004618 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004619 {
Hanno Becker40f50842018-08-15 14:48:01 +01004620#if defined(MBEDTLS_SSL_PROTO_DTLS)
4621 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004622
Hanno Becker40f50842018-08-15 14:48:01 +01004623 /* We only check for buffered messages if the
4624 * current datagram is fully consumed. */
4625 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004626 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004627 {
Hanno Becker40f50842018-08-15 14:48:01 +01004628 if( ssl_load_buffered_message( ssl ) == 0 )
4629 have_buffered = 1;
4630 }
4631
4632 if( have_buffered == 0 )
4633#endif /* MBEDTLS_SSL_PROTO_DTLS */
4634 {
4635 ret = ssl_get_next_record( ssl );
4636 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4637 continue;
4638
4639 if( ret != 0 )
4640 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004641 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004642 return( ret );
4643 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004644 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004645 }
4646
4647 ret = mbedtls_ssl_handle_message_type( ssl );
4648
Hanno Becker40f50842018-08-15 14:48:01 +01004649#if defined(MBEDTLS_SSL_PROTO_DTLS)
4650 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4651 {
4652 /* Buffer future message */
4653 ret = ssl_buffer_message( ssl );
4654 if( ret != 0 )
4655 return( ret );
4656
4657 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4658 }
4659#endif /* MBEDTLS_SSL_PROTO_DTLS */
4660
Hanno Becker90333da2017-10-10 11:27:13 +01004661 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4662 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004663
4664 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004665 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004666 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004667 return( ret );
4668 }
4669
Hanno Becker327c93b2018-08-15 13:56:18 +01004670 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004671 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004672 {
4673 mbedtls_ssl_update_handshake_status( ssl );
4674 }
Simon Butcher99000142016-10-13 17:21:01 +01004675 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004676 else
Simon Butcher99000142016-10-13 17:21:01 +01004677 {
Hanno Becker02f59072018-08-15 14:00:24 +01004678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004679 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004680 }
4681
4682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4683
4684 return( 0 );
4685}
4686
Hanno Becker40f50842018-08-15 14:48:01 +01004687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004688static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004689{
Hanno Becker40f50842018-08-15 14:48:01 +01004690 if( ssl->in_left > ssl->next_record_offset )
4691 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004692
Hanno Becker40f50842018-08-15 14:48:01 +01004693 return( 0 );
4694}
4695
4696static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4697{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004698 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004699 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004700 int ret = 0;
4701
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004702 if( hs == NULL )
4703 return( -1 );
4704
Hanno Beckere00ae372018-08-20 09:39:42 +01004705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4706
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004707 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4708 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4709 {
4710 /* Check if we have seen a ChangeCipherSpec before.
4711 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004712 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004713 {
4714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4715 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004716 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004717 }
4718
Hanno Becker39b8bc92018-08-28 17:17:13 +01004719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004720 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4721 ssl->in_msglen = 1;
4722 ssl->in_msg[0] = 1;
4723
4724 /* As long as they are equal, the exact value doesn't matter. */
4725 ssl->in_left = 0;
4726 ssl->next_record_offset = 0;
4727
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004728 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004729 goto exit;
4730 }
Hanno Becker37f95322018-08-16 13:55:32 +01004731
Hanno Beckerb8f50142018-08-28 10:01:34 +01004732#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004733 /* Debug only */
4734 {
4735 unsigned offset;
4736 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4737 {
4738 hs_buf = &hs->buffering.hs[offset];
4739 if( hs_buf->is_valid == 1 )
4740 {
4741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4742 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004743 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004744 }
4745 }
4746 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004747#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004748
4749 /* Check if we have buffered and/or fully reassembled the
4750 * next handshake message. */
4751 hs_buf = &hs->buffering.hs[0];
4752 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4753 {
4754 /* Synthesize a record containing the buffered HS message. */
4755 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4756 ( hs_buf->data[2] << 8 ) |
4757 hs_buf->data[3];
4758
4759 /* Double-check that we haven't accidentally buffered
4760 * a message that doesn't fit into the input buffer. */
4761 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4765 }
4766
4767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4768 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4769 hs_buf->data, msg_len + 12 );
4770
4771 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4772 ssl->in_hslen = msg_len + 12;
4773 ssl->in_msglen = msg_len + 12;
4774 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4775
4776 ret = 0;
4777 goto exit;
4778 }
4779 else
4780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4782 hs->in_msg_seq ) );
4783 }
4784
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004785 ret = -1;
4786
4787exit:
4788
4789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4790 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004791}
4792
Hanno Beckera02b0b42018-08-21 17:20:27 +01004793static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4794 size_t desired )
4795{
4796 int offset;
4797 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4799 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004800
Hanno Becker01315ea2018-08-21 17:22:17 +01004801 /* Get rid of future records epoch first, if such exist. */
4802 ssl_free_buffered_record( ssl );
4803
4804 /* Check if we have enough space available now. */
4805 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4806 hs->buffering.total_bytes_buffered ) )
4807 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004809 return( 0 );
4810 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004811
Hanno Becker4f432ad2018-08-28 10:02:32 +01004812 /* We don't have enough space to buffer the next expected handshake
4813 * message. Remove buffers used for future messages to gain space,
4814 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004815 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4816 offset >= 0; offset-- )
4817 {
4818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4819 offset ) );
4820
Hanno Beckerb309b922018-08-23 13:18:05 +01004821 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004822
4823 /* Check if we have enough space available now. */
4824 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4825 hs->buffering.total_bytes_buffered ) )
4826 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004828 return( 0 );
4829 }
4830 }
4831
4832 return( -1 );
4833}
4834
Hanno Becker40f50842018-08-15 14:48:01 +01004835static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4836{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004837 int ret = 0;
4838 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4839
4840 if( hs == NULL )
4841 return( 0 );
4842
4843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4844
4845 switch( ssl->in_msgtype )
4846 {
4847 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004849
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004850 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004851 break;
4852
4853 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004854 {
4855 unsigned recv_msg_seq_offset;
4856 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4857 mbedtls_ssl_hs_buffer *hs_buf;
4858 size_t msg_len = ssl->in_hslen - 12;
4859
4860 /* We should never receive an old handshake
4861 * message - double-check nonetheless. */
4862 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4863 {
4864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4866 }
4867
4868 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4869 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4870 {
4871 /* Silently ignore -- message too far in the future */
4872 MBEDTLS_SSL_DEBUG_MSG( 2,
4873 ( "Ignore future HS message with sequence number %u, "
4874 "buffering window %u - %u",
4875 recv_msg_seq, ssl->handshake->in_msg_seq,
4876 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4877
4878 goto exit;
4879 }
4880
4881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4882 recv_msg_seq, recv_msg_seq_offset ) );
4883
4884 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4885
4886 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004887 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004888 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004889 size_t reassembly_buf_sz;
4890
Hanno Becker37f95322018-08-16 13:55:32 +01004891 hs_buf->is_fragmented =
4892 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4893
4894 /* We copy the message back into the input buffer
4895 * after reassembly, so check that it's not too large.
4896 * This is an implementation-specific limitation
4897 * and not one from the standard, hence it is not
4898 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004899 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004900 {
4901 /* Ignore message */
4902 goto exit;
4903 }
4904
Hanno Beckere0b150f2018-08-21 15:51:03 +01004905 /* Check if we have enough space to buffer the message. */
4906 if( hs->buffering.total_bytes_buffered >
4907 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4908 {
4909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4911 }
4912
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004913 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4914 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004915
4916 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4917 hs->buffering.total_bytes_buffered ) )
4918 {
4919 if( recv_msg_seq_offset > 0 )
4920 {
4921 /* If we can't buffer a future message because
4922 * of space limitations -- ignore. */
4923 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",
4924 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4925 (unsigned) hs->buffering.total_bytes_buffered ) );
4926 goto exit;
4927 }
Hanno Beckere1801392018-08-21 16:51:05 +01004928 else
4929 {
4930 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",
4931 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4932 (unsigned) hs->buffering.total_bytes_buffered ) );
4933 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004934
Hanno Beckera02b0b42018-08-21 17:20:27 +01004935 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004936 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004937 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",
4938 (unsigned) msg_len,
4939 (unsigned) reassembly_buf_sz,
4940 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004941 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004942 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4943 goto exit;
4944 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004945 }
4946
4947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4948 msg_len ) );
4949
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004950 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4951 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004952 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004953 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004954 goto exit;
4955 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004956 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004957
4958 /* Prepare final header: copy msg_type, length and message_seq,
4959 * then add standardised fragment_offset and fragment_length */
4960 memcpy( hs_buf->data, ssl->in_msg, 6 );
4961 memset( hs_buf->data + 6, 0, 3 );
4962 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4963
4964 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004965
4966 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004967 }
4968 else
4969 {
4970 /* Make sure msg_type and length are consistent */
4971 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4972 {
4973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4974 /* Ignore */
4975 goto exit;
4976 }
4977 }
4978
Hanno Becker4422bbb2018-08-20 09:40:19 +01004979 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004980 {
4981 size_t frag_len, frag_off;
4982 unsigned char * const msg = hs_buf->data + 12;
4983
4984 /*
4985 * Check and copy current fragment
4986 */
4987
4988 /* Validation of header fields already done in
4989 * mbedtls_ssl_prepare_handshake_record(). */
4990 frag_off = ssl_get_hs_frag_off( ssl );
4991 frag_len = ssl_get_hs_frag_len( ssl );
4992
4993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4994 frag_off, frag_len ) );
4995 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4996
4997 if( hs_buf->is_fragmented )
4998 {
4999 unsigned char * const bitmask = msg + msg_len;
5000 ssl_bitmask_set( bitmask, frag_off, frag_len );
5001 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5002 msg_len ) == 0 );
5003 }
5004 else
5005 {
5006 hs_buf->is_complete = 1;
5007 }
5008
5009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5010 hs_buf->is_complete ? "" : "not yet " ) );
5011 }
5012
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005013 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005014 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005015
5016 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005017 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005018 break;
5019 }
5020
5021exit:
5022
5023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5024 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005025}
5026#endif /* MBEDTLS_SSL_PROTO_DTLS */
5027
Hanno Becker1097b342018-08-15 14:09:41 +01005028static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005029{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005030 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005031 * Consume last content-layer message and potentially
5032 * update in_msglen which keeps track of the contents'
5033 * consumption state.
5034 *
5035 * (1) Handshake messages:
5036 * Remove last handshake message, move content
5037 * and adapt in_msglen.
5038 *
5039 * (2) Alert messages:
5040 * Consume whole record content, in_msglen = 0.
5041 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005042 * (3) Change cipher spec:
5043 * Consume whole record content, in_msglen = 0.
5044 *
5045 * (4) Application data:
5046 * Don't do anything - the record layer provides
5047 * the application data as a stream transport
5048 * and consumes through mbedtls_ssl_read only.
5049 *
5050 */
5051
5052 /* Case (1): Handshake messages */
5053 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005054 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005055 /* Hard assertion to be sure that no application data
5056 * is in flight, as corrupting ssl->in_msglen during
5057 * ssl->in_offt != NULL is fatal. */
5058 if( ssl->in_offt != NULL )
5059 {
5060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5062 }
5063
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005064 /*
5065 * Get next Handshake message in the current record
5066 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005067
Hanno Becker4a810fb2017-05-24 16:27:30 +01005068 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005069 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005070 * current handshake content: If DTLS handshake
5071 * fragmentation is used, that's the fragment
5072 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005073 * size here is faulty and should be changed at
5074 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 * (2) While it doesn't seem to cause problems, one
5076 * has to be very careful not to assume that in_hslen
5077 * is always <= in_msglen in a sensible communication.
5078 * Again, it's wrong for DTLS handshake fragmentation.
5079 * The following check is therefore mandatory, and
5080 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005081 * Additionally, ssl->in_hslen might be arbitrarily out of
5082 * bounds after handling a DTLS message with an unexpected
5083 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005084 */
5085 if( ssl->in_hslen < ssl->in_msglen )
5086 {
5087 ssl->in_msglen -= ssl->in_hslen;
5088 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5089 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005090
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5092 ssl->in_msg, ssl->in_msglen );
5093 }
5094 else
5095 {
5096 ssl->in_msglen = 0;
5097 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005098
Hanno Becker4a810fb2017-05-24 16:27:30 +01005099 ssl->in_hslen = 0;
5100 }
5101 /* Case (4): Application data */
5102 else if( ssl->in_offt != NULL )
5103 {
5104 return( 0 );
5105 }
5106 /* Everything else (CCS & Alerts) */
5107 else
5108 {
5109 ssl->in_msglen = 0;
5110 }
5111
Hanno Becker1097b342018-08-15 14:09:41 +01005112 return( 0 );
5113}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005114
Hanno Beckere74d5562018-08-15 14:26:08 +01005115static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5116{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005117 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005118 return( 1 );
5119
5120 return( 0 );
5121}
5122
Hanno Becker5f066e72018-08-16 14:56:31 +01005123#if defined(MBEDTLS_SSL_PROTO_DTLS)
5124
5125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5126{
5127 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5128 if( hs == NULL )
5129 return;
5130
Hanno Becker01315ea2018-08-21 17:22:17 +01005131 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005132 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005133 hs->buffering.total_bytes_buffered -=
5134 hs->buffering.future_record.len;
5135
5136 mbedtls_free( hs->buffering.future_record.data );
5137 hs->buffering.future_record.data = NULL;
5138 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005139}
5140
5141static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5142{
5143 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5144 unsigned char * rec;
5145 size_t rec_len;
5146 unsigned rec_epoch;
5147
5148 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5149 return( 0 );
5150
5151 if( hs == NULL )
5152 return( 0 );
5153
Hanno Becker5f066e72018-08-16 14:56:31 +01005154 rec = hs->buffering.future_record.data;
5155 rec_len = hs->buffering.future_record.len;
5156 rec_epoch = hs->buffering.future_record.epoch;
5157
5158 if( rec == NULL )
5159 return( 0 );
5160
Hanno Becker4cb782d2018-08-20 11:19:05 +01005161 /* Only consider loading future records if the
5162 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005163 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005164 return( 0 );
5165
Hanno Becker5f066e72018-08-16 14:56:31 +01005166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5167
5168 if( rec_epoch != ssl->in_epoch )
5169 {
5170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5171 goto exit;
5172 }
5173
5174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5175
5176 /* Double-check that the record is not too large */
5177 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5178 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5179 {
5180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5182 }
5183
5184 memcpy( ssl->in_hdr, rec, rec_len );
5185 ssl->in_left = rec_len;
5186 ssl->next_record_offset = 0;
5187
5188 ssl_free_buffered_record( ssl );
5189
5190exit:
5191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5192 return( 0 );
5193}
5194
5195static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5196{
5197 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5198 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005199 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005200
5201 /* Don't buffer future records outside handshakes. */
5202 if( hs == NULL )
5203 return( 0 );
5204
5205 /* Only buffer handshake records (we are only interested
5206 * in Finished messages). */
5207 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5208 return( 0 );
5209
5210 /* Don't buffer more than one future epoch record. */
5211 if( hs->buffering.future_record.data != NULL )
5212 return( 0 );
5213
Hanno Becker01315ea2018-08-21 17:22:17 +01005214 /* Don't buffer record if there's not enough buffering space remaining. */
5215 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5216 hs->buffering.total_bytes_buffered ) )
5217 {
5218 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",
5219 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5220 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005221 return( 0 );
5222 }
5223
Hanno Becker5f066e72018-08-16 14:56:31 +01005224 /* Buffer record */
5225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5226 ssl->in_epoch + 1 ) );
5227 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5228 rec_hdr_len + ssl->in_msglen );
5229
5230 /* ssl_parse_record_header() only considers records
5231 * of the next epoch as candidates for buffering. */
5232 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005233 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005234
5235 hs->buffering.future_record.data =
5236 mbedtls_calloc( 1, hs->buffering.future_record.len );
5237 if( hs->buffering.future_record.data == NULL )
5238 {
5239 /* If we run out of RAM trying to buffer a
5240 * record from the next epoch, just ignore. */
5241 return( 0 );
5242 }
5243
Hanno Becker01315ea2018-08-21 17:22:17 +01005244 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005245
Hanno Becker01315ea2018-08-21 17:22:17 +01005246 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005247 return( 0 );
5248}
5249
5250#endif /* MBEDTLS_SSL_PROTO_DTLS */
5251
Hanno Beckere74d5562018-08-15 14:26:08 +01005252static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005253{
5254 int ret;
5255
Hanno Becker5f066e72018-08-16 14:56:31 +01005256#if defined(MBEDTLS_SSL_PROTO_DTLS)
5257 /* We might have buffered a future record; if so,
5258 * and if the epoch matches now, load it.
5259 * On success, this call will set ssl->in_left to
5260 * the length of the buffered record, so that
5261 * the calls to ssl_fetch_input() below will
5262 * essentially be no-ops. */
5263 ret = ssl_load_buffered_record( ssl );
5264 if( ret != 0 )
5265 return( ret );
5266#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005271 return( ret );
5272 }
5273
5274 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5278 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005279 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005280 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5281 {
5282 ret = ssl_buffer_future_record( ssl );
5283 if( ret != 0 )
5284 return( ret );
5285
5286 /* Fall through to handling of unexpected records */
5287 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5288 }
5289
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005290 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5291 {
5292 /* Skip unexpected record (but not whole datagram) */
5293 ssl->next_record_offset = ssl->in_msglen
5294 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005295
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5297 "(header)" ) );
5298 }
5299 else
5300 {
5301 /* Skip invalid record and the rest of the datagram */
5302 ssl->next_record_offset = 0;
5303 ssl->in_left = 0;
5304
5305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5306 "(header)" ) );
5307 }
5308
5309 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005310 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005311 }
5312#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005313 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005314 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005315
5316 /*
5317 * Read and optionally decrypt the message contents
5318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5320 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005323 return( ret );
5324 }
5325
5326 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005331 if( ssl->next_record_offset < ssl->in_left )
5332 {
5333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5334 }
5335 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005336 else
5337#endif
5338 ssl->in_left = 0;
5339
5340 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005344 {
5345 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5347 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005348 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005349 /* Except when waiting for Finished as a bad mac here
5350 * probably means something went wrong in the handshake
5351 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5352 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5353 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5354 {
5355#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5356 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5357 {
5358 mbedtls_ssl_send_alert_message( ssl,
5359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5360 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5361 }
5362#endif
5363 return( ret );
5364 }
5365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005367 if( ssl->conf->badmac_limit != 0 &&
5368 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5371 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005372 }
5373#endif
5374
Hanno Becker4a810fb2017-05-24 16:27:30 +01005375 /* As above, invalid records cause
5376 * dismissal of the whole datagram. */
5377
5378 ssl->next_record_offset = 0;
5379 ssl->in_left = 0;
5380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005382 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005383 }
5384
5385 return( ret );
5386 }
5387 else
5388#endif
5389 {
5390 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5392 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 mbedtls_ssl_send_alert_message( ssl,
5395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5396 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005397 }
5398#endif
5399 return( ret );
5400 }
5401 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005402
Simon Butcher99000142016-10-13 17:21:01 +01005403 return( 0 );
5404}
5405
5406int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5407{
5408 int ret;
5409
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005410 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005411 * Handle particular types of records
5412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 {
Simon Butcher99000142016-10-13 17:21:01 +01005415 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5416 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005417 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 }
5420
Hanno Beckere678eaa2018-08-21 14:57:46 +01005421 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005422 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005423 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005424 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5426 ssl->in_msglen ) );
5427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005428 }
5429
Hanno Beckere678eaa2018-08-21 14:57:46 +01005430 if( ssl->in_msg[0] != 1 )
5431 {
5432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5433 ssl->in_msg[0] ) );
5434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5435 }
5436
5437#if defined(MBEDTLS_SSL_PROTO_DTLS)
5438 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5439 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5440 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5441 {
5442 if( ssl->handshake == NULL )
5443 {
5444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5445 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5446 }
5447
5448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5449 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5450 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005451#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005452 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005455 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005456 if( ssl->in_msglen != 2 )
5457 {
5458 /* Note: Standard allows for more than one 2 byte alert
5459 to be packed in a single message, but Mbed TLS doesn't
5460 currently support this. */
5461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5462 ssl->in_msglen ) );
5463 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5464 }
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 ssl->in_msg[0], ssl->in_msg[1] ) );
5468
5469 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005470 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005475 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 }
5478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5480 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5483 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005485
5486#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5487 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5488 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5489 {
Hanno Becker90333da2017-10-10 11:27:13 +01005490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005491 /* Will be handled when trying to parse ServerHello */
5492 return( 0 );
5493 }
5494#endif
5495
5496#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5498 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5499 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5500 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5501 {
5502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5503 /* Will be handled in mbedtls_ssl_parse_certificate() */
5504 return( 0 );
5505 }
5506#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5507
5508 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005509 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 }
5511
Hanno Beckerc76c6192017-06-06 10:03:17 +01005512#if defined(MBEDTLS_SSL_PROTO_DTLS)
5513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5514 ssl->handshake != NULL &&
5515 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5516 {
5517 ssl_handshake_wrapup_free_hs_transform( ssl );
5518 }
5519#endif
5520
Paul Bakker5121ce52009-01-03 21:22:43 +00005521 return( 0 );
5522}
5523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005525{
5526 int ret;
5527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5529 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5530 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005531 {
5532 return( ret );
5533 }
5534
5535 return( 0 );
5536}
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005539 unsigned char level,
5540 unsigned char message )
5541{
5542 int ret;
5543
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005544 if( ssl == NULL || ssl->conf == NULL )
5545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005551 ssl->out_msglen = 2;
5552 ssl->out_msg[0] = level;
5553 ssl->out_msg[1] = message;
5554
Hanno Becker67bc7c32018-08-06 11:33:50 +01005555 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005558 return( ret );
5559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005561
5562 return( 0 );
5563}
5564
Hanno Beckerb9d44792019-02-08 07:19:04 +00005565#if defined(MBEDTLS_X509_CRT_PARSE_C)
5566static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5567{
5568#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5569 if( session->peer_cert != NULL )
5570 {
5571 mbedtls_x509_crt_free( session->peer_cert );
5572 mbedtls_free( session->peer_cert );
5573 session->peer_cert = NULL;
5574 }
5575#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5576 if( session->peer_cert_digest != NULL )
5577 {
5578 /* Zeroization is not necessary. */
5579 mbedtls_free( session->peer_cert_digest );
5580 session->peer_cert_digest = NULL;
5581 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5582 session->peer_cert_digest_len = 0;
5583 }
5584#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5585}
5586#endif /* MBEDTLS_X509_CRT_PARSE_C */
5587
Paul Bakker5121ce52009-01-03 21:22:43 +00005588/*
5589 * Handshake functions
5590 */
Hanno Becker21489932019-02-05 13:20:55 +00005591#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005592/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005594{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005595 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5596 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599
Hanno Becker7177a882019-02-05 13:36:46 +00005600 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005603 ssl->state++;
5604 return( 0 );
5605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005609}
5610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005612{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005613 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5614 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617
Hanno Becker7177a882019-02-05 13:36:46 +00005618 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005621 ssl->state++;
5622 return( 0 );
5623 }
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005627}
Gilles Peskinef9828522017-05-03 12:28:43 +02005628
Hanno Becker21489932019-02-05 13:20:55 +00005629#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005630/* Some certificate support -> implement write and parse */
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005633{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005635 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005637 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5638 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005641
Hanno Becker7177a882019-02-05 13:36:46 +00005642 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005645 ssl->state++;
5646 return( 0 );
5647 }
5648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005650 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 {
5652 if( ssl->client_auth == 0 )
5653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 ssl->state++;
5656 return( 0 );
5657 }
5658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 /*
5661 * If using SSLv3 and got no cert, send an Alert message
5662 * (otherwise an empty Certificate message will be sent).
5663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5665 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 {
5667 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5669 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5670 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005673 goto write_msg;
5674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#endif /* MBEDTLS_SSL_CLI_C */
5678#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5684 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 }
5686 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005687#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690
5691 /*
5692 * 0 . 0 handshake type
5693 * 1 . 3 handshake length
5694 * 4 . 6 length of all certs
5695 * 7 . 9 length of cert. 1
5696 * 10 . n-1 peer certificate
5697 * n . n+2 length of cert. 2
5698 * n+3 . ... upper level cert, etc.
5699 */
5700 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005702
Paul Bakker29087132010-03-21 21:03:34 +00005703 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 {
5705 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005706 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005709 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
5713 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5714 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5715 ssl->out_msg[i + 2] = (unsigned char)( n );
5716
5717 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5718 i += n; crt = crt->next;
5719 }
5720
5721 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5722 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5723 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5724
5725 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5727 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005728
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005729#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005730write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005731#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
5733 ssl->state++;
5734
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005735 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005736 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 return( ret );
5739 }
5740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005742
Paul Bakkered27a042013-04-18 22:46:23 +02005743 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744}
5745
Hanno Becker84879e32019-01-31 07:44:03 +00005746#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005747
5748#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005749static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5750 unsigned char *crt_buf,
5751 size_t crt_buf_len )
5752{
5753 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5754
5755 if( peer_crt == NULL )
5756 return( -1 );
5757
5758 if( peer_crt->raw.len != crt_buf_len )
5759 return( -1 );
5760
Hanno Becker46f34d02019-02-08 14:00:04 +00005761 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005762}
Hanno Becker177475a2019-02-05 17:02:46 +00005763#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5764static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5765 unsigned char *crt_buf,
5766 size_t crt_buf_len )
5767{
5768 int ret;
5769 unsigned char const * const peer_cert_digest =
5770 ssl->session->peer_cert_digest;
5771 mbedtls_md_type_t const peer_cert_digest_type =
5772 ssl->session->peer_cert_digest_type;
5773 mbedtls_md_info_t const * const digest_info =
5774 mbedtls_md_info_from_type( peer_cert_digest_type );
5775 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5776 size_t digest_len;
5777
5778 if( peer_cert_digest == NULL || digest_info == NULL )
5779 return( -1 );
5780
5781 digest_len = mbedtls_md_get_size( digest_info );
5782 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5783 return( -1 );
5784
5785 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5786 if( ret != 0 )
5787 return( -1 );
5788
5789 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5790}
5791#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005792#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005793
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005794/*
5795 * Once the certificate message is read, parse it into a cert chain and
5796 * perform basic checks, but leave actual verification to the caller
5797 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005798static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5799 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005800{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005801 int ret;
5802#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5803 int crt_cnt=0;
5804#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005805 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005806 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5812 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 }
5815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5817 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005820 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5821 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 }
5824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005826
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005830 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005831
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005832 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5837 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 }
5840
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005841 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5842 i += 3;
5843
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005844 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 while( i < ssl->in_hslen )
5846 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005847 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005848 if ( i + 3 > ssl->in_hslen ) {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005850 mbedtls_ssl_send_alert_message( ssl,
5851 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5852 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005853 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5854 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005855 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5856 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 if( ssl->in_msg[i] != 0 )
5858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005860 mbedtls_ssl_send_alert_message( ssl,
5861 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5862 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005864 }
5865
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005866 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5868 | (unsigned int) ssl->in_msg[i + 2];
5869 i += 3;
5870
5871 if( n < 128 || i + n > ssl->in_hslen )
5872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005874 mbedtls_ssl_send_alert_message( ssl,
5875 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5876 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 }
5879
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005880 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005881#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5882 if( crt_cnt++ == 0 &&
5883 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5884 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005885 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005886 /* During client-side renegotiation, check that the server's
5887 * end-CRTs hasn't changed compared to the initial handshake,
5888 * mitigating the triple handshake attack. On success, reuse
5889 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5891 if( ssl_check_peer_crt_unchanged( ssl,
5892 &ssl->in_msg[i],
5893 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005894 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5896 mbedtls_ssl_send_alert_message( ssl,
5897 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5898 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5899 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005900 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005901
5902 /* Now we can safely free the original chain. */
5903 ssl_clear_peer_cert( ssl->session );
5904 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005905#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5906
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005907 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00005908#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005909 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00005910#else
Hanno Becker353a6f02019-02-26 11:51:34 +00005911 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00005912 * it in-place from the input buffer instead of making a copy. */
5913 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5914#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005915 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005917 case 0: /*ok*/
5918 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5919 /* Ignore certificate with an unknown algorithm: maybe a
5920 prior certificate was already trusted. */
5921 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005922
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005923 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5924 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5925 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005926
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005927 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5928 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5929 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005930
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005931 default:
5932 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5933 crt_parse_der_failed:
5934 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5935 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5936 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 }
5938
5939 i += n;
5940 }
5941
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005942 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005943 return( 0 );
5944}
5945
Hanno Becker4a55f632019-02-05 12:49:06 +00005946#if defined(MBEDTLS_SSL_SRV_C)
5947static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5948{
5949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5950 return( -1 );
5951
5952#if defined(MBEDTLS_SSL_PROTO_SSL3)
5953 /*
5954 * Check if the client sent an empty certificate
5955 */
5956 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5957 {
5958 if( ssl->in_msglen == 2 &&
5959 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5960 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5961 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5964 return( 0 );
5965 }
5966
5967 return( -1 );
5968 }
5969#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5970
5971#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5972 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5973 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5974 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5975 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5976 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5977 {
5978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5979 return( 0 );
5980 }
5981
5982 return( -1 );
5983#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5984 MBEDTLS_SSL_PROTO_TLS1_2 */
5985}
5986#endif /* MBEDTLS_SSL_SRV_C */
5987
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005988/* Check if a certificate message is expected.
5989 * Return either
5990 * - SSL_CERTIFICATE_EXPECTED, or
5991 * - SSL_CERTIFICATE_SKIP
5992 * indicating whether a Certificate message is expected or not.
5993 */
5994#define SSL_CERTIFICATE_EXPECTED 0
5995#define SSL_CERTIFICATE_SKIP 1
5996static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5997 int authmode )
5998{
5999 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006000 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006001
6002 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6003 return( SSL_CERTIFICATE_SKIP );
6004
6005#if defined(MBEDTLS_SSL_SRV_C)
6006 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6007 {
6008 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6009 return( SSL_CERTIFICATE_SKIP );
6010
6011 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6012 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006013 ssl->session_negotiate->verify_result =
6014 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6015 return( SSL_CERTIFICATE_SKIP );
6016 }
6017 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006018#else
6019 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006020#endif /* MBEDTLS_SSL_SRV_C */
6021
6022 return( SSL_CERTIFICATE_EXPECTED );
6023}
6024
Hanno Becker68636192019-02-05 14:36:34 +00006025static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6026 int authmode,
6027 mbedtls_x509_crt *chain,
6028 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006029{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006030 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006031 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006032 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006033 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006034
Hanno Becker8927c832019-04-03 12:52:50 +01006035 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6036 void *p_vrfy;
6037
Hanno Becker68636192019-02-05 14:36:34 +00006038 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6039 return( 0 );
6040
Hanno Becker8927c832019-04-03 12:52:50 +01006041 if( ssl->f_vrfy != NULL )
6042 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006044 f_vrfy = ssl->f_vrfy;
6045 p_vrfy = ssl->p_vrfy;
6046 }
6047 else
6048 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006050 f_vrfy = ssl->conf->f_vrfy;
6051 p_vrfy = ssl->conf->p_vrfy;
6052 }
6053
Hanno Becker68636192019-02-05 14:36:34 +00006054 /*
6055 * Main check: verify certificate
6056 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006057#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6058 if( ssl->conf->f_ca_cb != NULL )
6059 {
6060 ((void) rs_ctx);
6061 have_ca_chain = 1;
6062
6063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006064 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006065 chain,
6066 ssl->conf->f_ca_cb,
6067 ssl->conf->p_ca_cb,
6068 ssl->conf->cert_profile,
6069 ssl->hostname,
6070 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006071 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006072 }
6073 else
6074#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6075 {
6076 mbedtls_x509_crt *ca_chain;
6077 mbedtls_x509_crl *ca_crl;
6078
6079#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6080 if( ssl->handshake->sni_ca_chain != NULL )
6081 {
6082 ca_chain = ssl->handshake->sni_ca_chain;
6083 ca_crl = ssl->handshake->sni_ca_crl;
6084 }
6085 else
6086#endif
6087 {
6088 ca_chain = ssl->conf->ca_chain;
6089 ca_crl = ssl->conf->ca_crl;
6090 }
6091
6092 if( ca_chain != NULL )
6093 have_ca_chain = 1;
6094
6095 ret = mbedtls_x509_crt_verify_restartable(
6096 chain,
6097 ca_chain, ca_crl,
6098 ssl->conf->cert_profile,
6099 ssl->hostname,
6100 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006101 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006102 }
Hanno Becker68636192019-02-05 14:36:34 +00006103
6104 if( ret != 0 )
6105 {
6106 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6107 }
6108
6109#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6110 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6111 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6112#endif
6113
6114 /*
6115 * Secondary checks: always done, but change 'ret' only if it was 0
6116 */
6117
6118#if defined(MBEDTLS_ECP_C)
6119 {
6120 const mbedtls_pk_context *pk = &chain->pk;
6121
6122 /* If certificate uses an EC key, make sure the curve is OK */
6123 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6124 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6125 {
6126 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6127
6128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6129 if( ret == 0 )
6130 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6131 }
6132 }
6133#endif /* MBEDTLS_ECP_C */
6134
6135 if( mbedtls_ssl_check_cert_usage( chain,
6136 ciphersuite_info,
6137 ! ssl->conf->endpoint,
6138 &ssl->session_negotiate->verify_result ) != 0 )
6139 {
6140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6141 if( ret == 0 )
6142 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6143 }
6144
6145 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6146 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6147 * with details encoded in the verification flags. All other kinds
6148 * of error codes, including those from the user provided f_vrfy
6149 * functions, are treated as fatal and lead to a failure of
6150 * ssl_parse_certificate even if verification was optional. */
6151 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6152 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6153 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6154 {
6155 ret = 0;
6156 }
6157
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006158 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6161 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6162 }
6163
6164 if( ret != 0 )
6165 {
6166 uint8_t alert;
6167
6168 /* The certificate may have been rejected for several reasons.
6169 Pick one and send the corresponding alert. Which alert to send
6170 may be a subject of debate in some cases. */
6171 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6172 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6173 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6174 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6175 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6176 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6177 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6178 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6179 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6180 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6181 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6182 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6183 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6184 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6185 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6186 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6187 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6188 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6189 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6190 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6191 else
6192 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6193 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6194 alert );
6195 }
6196
6197#if defined(MBEDTLS_DEBUG_C)
6198 if( ssl->session_negotiate->verify_result != 0 )
6199 {
6200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6201 ssl->session_negotiate->verify_result ) );
6202 }
6203 else
6204 {
6205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6206 }
6207#endif /* MBEDTLS_DEBUG_C */
6208
6209 return( ret );
6210}
6211
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006212#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6213static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6214 unsigned char *start, size_t len )
6215{
6216 int ret;
6217 /* Remember digest of the peer's end-CRT. */
6218 ssl->session_negotiate->peer_cert_digest =
6219 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6220 if( ssl->session_negotiate->peer_cert_digest == NULL )
6221 {
6222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6223 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6224 mbedtls_ssl_send_alert_message( ssl,
6225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6226 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6227
6228 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6229 }
6230
6231 ret = mbedtls_md( mbedtls_md_info_from_type(
6232 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6233 start, len,
6234 ssl->session_negotiate->peer_cert_digest );
6235
6236 ssl->session_negotiate->peer_cert_digest_type =
6237 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6238 ssl->session_negotiate->peer_cert_digest_len =
6239 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6240
6241 return( ret );
6242}
6243
6244static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6245 unsigned char *start, size_t len )
6246{
6247 unsigned char *end = start + len;
6248 int ret;
6249
6250 /* Make a copy of the peer's raw public key. */
6251 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6252 ret = mbedtls_pk_parse_subpubkey( &start, end,
6253 &ssl->handshake->peer_pubkey );
6254 if( ret != 0 )
6255 {
6256 /* We should have parsed the public key before. */
6257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6258 }
6259
6260 return( 0 );
6261}
6262#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6263
Hanno Becker68636192019-02-05 14:36:34 +00006264int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6265{
6266 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006267 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006268#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6269 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6270 ? ssl->handshake->sni_authmode
6271 : ssl->conf->authmode;
6272#else
6273 const int authmode = ssl->conf->authmode;
6274#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006275 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006276 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006277
6278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6279
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006280 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6281 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006284 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006285 }
6286
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006287#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6288 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006289 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006290 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006291 chain = ssl->handshake->ecrs_peer_cert;
6292 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006293 goto crt_verify;
6294 }
6295#endif
6296
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006297 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006298 {
6299 /* mbedtls_ssl_read_record may have sent an alert already. We
6300 let it decide whether to alert. */
6301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006302 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006303 }
6304
Hanno Becker4a55f632019-02-05 12:49:06 +00006305#if defined(MBEDTLS_SSL_SRV_C)
6306 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6307 {
6308 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006309
6310 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006311 ret = 0;
6312 else
6313 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006314
Hanno Becker6bdfab22019-02-05 13:11:17 +00006315 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006316 }
6317#endif /* MBEDTLS_SSL_SRV_C */
6318
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006319 /* Clear existing peer CRT structure in case we tried to
6320 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006321 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006322
6323 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6324 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6327 sizeof( mbedtls_x509_crt ) ) );
6328 mbedtls_ssl_send_alert_message( ssl,
6329 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6330 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006331
Hanno Becker3dad3112019-02-05 17:19:52 +00006332 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6333 goto exit;
6334 }
6335 mbedtls_x509_crt_init( chain );
6336
6337 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006338 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006339 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006340
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006341#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6342 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006343 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006344
6345crt_verify:
6346 if( ssl->handshake->ecrs_enabled)
6347 rs_ctx = &ssl->handshake->ecrs_ctx;
6348#endif
6349
Hanno Becker68636192019-02-05 14:36:34 +00006350 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006351 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006352 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006353 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006354
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006355#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006356 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006357 unsigned char *crt_start, *pk_start;
6358 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006359
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006360 /* We parse the CRT chain without copying, so
6361 * these pointers point into the input buffer,
6362 * and are hence still valid after freeing the
6363 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006364
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006365 crt_start = chain->raw.p;
6366 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006367
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006368 pk_start = chain->pk_raw.p;
6369 pk_len = chain->pk_raw.len;
6370
6371 /* Free the CRT structures before computing
6372 * digest and copying the peer's public key. */
6373 mbedtls_x509_crt_free( chain );
6374 mbedtls_free( chain );
6375 chain = NULL;
6376
6377 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006378 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006379 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006380
6381 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6382 if( ret != 0 )
6383 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006384 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006385#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6386 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006387 ssl->session_negotiate->peer_cert = chain;
6388 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006389#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006392
Hanno Becker6bdfab22019-02-05 13:11:17 +00006393exit:
6394
Hanno Becker3dad3112019-02-05 17:19:52 +00006395 if( ret == 0 )
6396 ssl->state++;
6397
6398#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6399 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6400 {
6401 ssl->handshake->ecrs_peer_cert = chain;
6402 chain = NULL;
6403 }
6404#endif
6405
6406 if( chain != NULL )
6407 {
6408 mbedtls_x509_crt_free( chain );
6409 mbedtls_free( chain );
6410 }
6411
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 return( ret );
6413}
Hanno Becker21489932019-02-05 13:20:55 +00006414#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006417{
6418 int ret;
6419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 ssl->out_msglen = 1;
6424 ssl->out_msg[0] = 1;
6425
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 ssl->state++;
6427
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006428 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 return( ret );
6432 }
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
6436 return( 0 );
6437}
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006440{
6441 int ret;
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006444
Hanno Becker327c93b2018-08-15 13:56:18 +01006445 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006448 return( ret );
6449 }
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006454 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6455 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 }
6458
Hanno Beckere678eaa2018-08-21 14:57:46 +01006459 /* CCS records are only accepted if they have length 1 and content '1',
6460 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006461
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006462 /*
6463 * Switch to our negotiated transform and session parameters for inbound
6464 * data.
6465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006467 ssl->transform_in = ssl->transform_negotiate;
6468 ssl->session_in = ssl->session_negotiate;
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006474 ssl_dtls_replay_reset( ssl );
6475#endif
6476
6477 /* Increment epoch */
6478 if( ++ssl->in_epoch == 0 )
6479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006481 /* This is highly unlikely to happen for legitimate reasons, so
6482 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006484 }
6485 }
6486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006488 memset( ssl->in_ctr, 0, 8 );
6489
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006490 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6493 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006498 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6499 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006501 }
6502 }
6503#endif
6504
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 ssl->state++;
6506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006508
6509 return( 0 );
6510}
6511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6513 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006514{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006515 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6518 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6519 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006520 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006521 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6524#if defined(MBEDTLS_SHA512_C)
6525 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006526 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6527 else
6528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SHA256_C)
6530 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006531 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532 else
6533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006537 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006538 }
Paul Bakker380da532012-04-18 16:10:25 +00006539}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6544 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006545 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6546 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6549#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006550#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006551 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006552 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6553#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006554 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006555#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006558#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006559 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006560 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006561#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006562 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006563#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006566}
6567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006569 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6572 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006573 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6574 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6577#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006578#if defined(MBEDTLS_USE_PSA_CRYPTO)
6579 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6580#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006581 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006582#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006585#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006586 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006587#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006588 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006589#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006592}
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6595 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6596static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006597 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006598{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006599 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6600 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006601}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006602#endif
Paul Bakker380da532012-04-18 16:10:25 +00006603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6605#if defined(MBEDTLS_SHA256_C)
6606static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006607 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006608{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006609#if defined(MBEDTLS_USE_PSA_CRYPTO)
6610 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6611#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006612 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006613#endif
Paul Bakker380da532012-04-18 16:10:25 +00006614}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006615#endif
Paul Bakker380da532012-04-18 16:10:25 +00006616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#if defined(MBEDTLS_SHA512_C)
6618static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006619 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006620{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006621#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006622 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006623#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006624 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006625#endif
Paul Bakker380da532012-04-18 16:10:25 +00006626}
Paul Bakker769075d2012-11-24 11:26:46 +01006627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006631static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006633{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006634 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006635 mbedtls_md5_context md5;
6636 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006637
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 unsigned char padbuf[48];
6639 unsigned char md5sum[16];
6640 unsigned char sha1sum[20];
6641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006643 if( !session )
6644 session = ssl->session;
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006647
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006648 mbedtls_md5_init( &md5 );
6649 mbedtls_sha1_init( &sha1 );
6650
6651 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6652 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
6654 /*
6655 * SSLv3:
6656 * hash =
6657 * MD5( master + pad2 +
6658 * MD5( handshake + sender + master + pad1 ) )
6659 * + SHA1( master + pad2 +
6660 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 */
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006664 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6665 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006669 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6670 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006671#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006674 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Paul Bakker1ef83d62012-04-11 12:09:53 +00006676 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006678 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6679 mbedtls_md5_update_ret( &md5, session->master, 48 );
6680 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6681 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006682
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006683 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6684 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6685 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6686 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006687
Paul Bakker1ef83d62012-04-11 12:09:53 +00006688 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006689
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006690 mbedtls_md5_starts_ret( &md5 );
6691 mbedtls_md5_update_ret( &md5, session->master, 48 );
6692 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6693 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6694 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006696 mbedtls_sha1_starts_ret( &sha1 );
6697 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6698 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6699 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6700 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006703
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006704 mbedtls_md5_free( &md5 );
6705 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006706
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006707 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6708 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6709 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006716static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006718{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006719 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006720 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006721 mbedtls_md5_context md5;
6722 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006723 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006726 if( !session )
6727 session = ssl->session;
6728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006730
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006731 mbedtls_md5_init( &md5 );
6732 mbedtls_sha1_init( &sha1 );
6733
6734 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6735 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006736
Paul Bakker1ef83d62012-04-11 12:09:53 +00006737 /*
6738 * TLSv1:
6739 * hash = PRF( master, finished_label,
6740 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6741 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006744 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6745 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006746#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006749 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6750 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006751#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006754 ? "client finished"
6755 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006757 mbedtls_md5_finish_ret( &md5, padbuf );
6758 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006759
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006760 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006761 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006764
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006765 mbedtls_md5_free( &md5 );
6766 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006768 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6775#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006776static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006778{
6779 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006780 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006781 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006782#if defined(MBEDTLS_USE_PSA_CRYPTO)
6783 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006784 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006785 psa_status_t status;
6786#else
6787 mbedtls_sha256_context sha256;
6788#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006791 if( !session )
6792 session = ssl->session;
6793
Andrzej Kurekeb342242019-01-29 09:14:33 -05006794 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6795 ? "client finished"
6796 : "server finished";
6797
6798#if defined(MBEDTLS_USE_PSA_CRYPTO)
6799 sha256_psa = psa_hash_operation_init();
6800
6801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6802
6803 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6804 if( status != PSA_SUCCESS )
6805 {
6806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6807 return;
6808 }
6809
6810 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6811 if( status != PSA_SUCCESS )
6812 {
6813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6814 return;
6815 }
6816 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6817#else
6818
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006819 mbedtls_sha256_init( &sha256 );
6820
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006822
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006823 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006824
6825 /*
6826 * TLSv1.2:
6827 * hash = PRF( master, finished_label,
6828 * Hash( handshake ) )[0.11]
6829 */
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if !defined(MBEDTLS_SHA256_ALT)
6832 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006833 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006834#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006835
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006836 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006837 mbedtls_sha256_free( &sha256 );
6838#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006840 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006841 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006844
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006845 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006852static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006854{
6855 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006856 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006857 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006858#if defined(MBEDTLS_USE_PSA_CRYPTO)
6859 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006860 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006861 psa_status_t status;
6862#else
6863 mbedtls_sha512_context sha512;
6864#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006867 if( !session )
6868 session = ssl->session;
6869
Andrzej Kurekeb342242019-01-29 09:14:33 -05006870 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6871 ? "client finished"
6872 : "server finished";
6873
6874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006875 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006876
6877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6878
Andrzej Kurek972fba52019-01-30 03:29:12 -05006879 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006880 if( status != PSA_SUCCESS )
6881 {
6882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6883 return;
6884 }
6885
Andrzej Kurek972fba52019-01-30 03:29:12 -05006886 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006887 if( status != PSA_SUCCESS )
6888 {
6889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6890 return;
6891 }
6892 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6893#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006894 mbedtls_sha512_init( &sha512 );
6895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006897
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006898 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006899
6900 /*
6901 * TLSv1.2:
6902 * hash = PRF( master, finished_label,
6903 * Hash( handshake ) )[0.11]
6904 */
6905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006907 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6908 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006909#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006910
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006911 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006912 mbedtls_sha512_free( &sha512 );
6913#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006914
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006915 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006916 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006919
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006920 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#endif /* MBEDTLS_SHA512_C */
6925#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006930
6931 /*
6932 * Free our handshake params
6933 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006934 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006936 ssl->handshake = NULL;
6937
6938 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006939 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006940 */
6941 if( ssl->transform )
6942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 mbedtls_ssl_transform_free( ssl->transform );
6944 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006945 }
6946 ssl->transform = ssl->transform_negotiate;
6947 ssl->transform_negotiate = NULL;
6948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006950}
6951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006953{
6954 int resume = ssl->handshake->resume;
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#if defined(MBEDTLS_SSL_RENEGOTIATION)
6959 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006962 ssl->renego_records_seen = 0;
6963 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006964#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006965
6966 /*
6967 * Free the previous session and switch in the current one
6968 */
Paul Bakker0a597072012-09-25 21:55:46 +00006969 if( ssl->session )
6970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006972 /* RFC 7366 3.1: keep the EtM state */
6973 ssl->session_negotiate->encrypt_then_mac =
6974 ssl->session->encrypt_then_mac;
6975#endif
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 mbedtls_ssl_session_free( ssl->session );
6978 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006979 }
6980 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006981 ssl->session_negotiate = NULL;
6982
Paul Bakker0a597072012-09-25 21:55:46 +00006983 /*
6984 * Add cache entry
6985 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006986 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006987 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006988 resume == 0 )
6989 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006990 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006992 }
Paul Bakker0a597072012-09-25 21:55:46 +00006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006996 ssl->handshake->flight != NULL )
6997 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006998 /* Cancel handshake timer */
6999 ssl_set_timer( ssl, 0 );
7000
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007001 /* Keep last flight around in case we need to resend it:
7002 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007004 }
7005 else
7006#endif
7007 ssl_handshake_wrapup_free_hs_transform( ssl );
7008
Paul Bakker48916f92012-09-16 19:57:18 +00007009 ssl->state++;
7010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007012}
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007015{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007016 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007019
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007020 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007021
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007022 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007023
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007024 /*
7025 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7026 * may define some other value. Currently (early 2016), no defined
7027 * ciphersuite does this (and this is unlikely to change as activity has
7028 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007033 ssl->verify_data_len = hash_len;
7034 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007035#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007036
Paul Bakker5121ce52009-01-03 21:22:43 +00007037 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7039 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
7041 /*
7042 * In case of session resuming, invert the client and server
7043 * ChangeCipherSpec messages order.
7044 */
Paul Bakker0a597072012-09-25 21:55:46 +00007045 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007048 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007052 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007055 }
7056 else
7057 ssl->state++;
7058
Paul Bakker48916f92012-09-16 19:57:18 +00007059 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007060 * Switch to our negotiated transform and session parameters for outbound
7061 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007067 {
7068 unsigned char i;
7069
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007070 /* Remember current epoch settings for resending */
7071 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007072 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007073
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007074 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007075 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007076
7077 /* Increment epoch */
7078 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007079 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007080 break;
7081
7082 /* The loop goes to its end iff the counter is wrapping */
7083 if( i == 0 )
7084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7086 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007087 }
7088 }
7089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007091 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007093 ssl->transform_out = ssl->transform_negotiate;
7094 ssl->session_out = ssl->session_negotiate;
7095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7097 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7102 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007103 }
7104 }
7105#endif
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007110#endif
7111
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007112 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007115 return( ret );
7116 }
7117
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007118#if defined(MBEDTLS_SSL_PROTO_DTLS)
7119 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7120 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7121 {
7122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7123 return( ret );
7124 }
7125#endif
7126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007128
7129 return( 0 );
7130}
7131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007133#define SSL_MAX_HASH_LEN 36
7134#else
7135#define SSL_MAX_HASH_LEN 12
7136#endif
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007139{
Paul Bakker23986e52011-04-24 08:57:21 +00007140 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007141 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007142 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007145
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007146 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007147
Hanno Becker327c93b2018-08-15 13:56:18 +01007148 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007151 return( ret );
7152 }
7153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007157 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7158 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007160 }
7161
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007162 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_PROTO_SSL3)
7164 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007165 hash_len = 36;
7166 else
7167#endif
7168 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7171 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007174 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7175 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 }
7178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007180 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007183 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7184 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 }
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007189 ssl->verify_data_len = hash_len;
7190 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007191#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007192
Paul Bakker0a597072012-09-25 21:55:46 +00007193 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007196 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007200 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 }
7204 else
7205 ssl->state++;
7206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007210#endif
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007213
7214 return( 0 );
7215}
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7222 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7223 mbedtls_md5_init( &handshake->fin_md5 );
7224 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007225 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7226 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7229#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007230#if defined(MBEDTLS_USE_PSA_CRYPTO)
7231 handshake->fin_sha256_psa = psa_hash_operation_init();
7232 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7233#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007236#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007239#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007240 handshake->fin_sha384_psa = psa_hash_operation_init();
7241 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007244 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007245#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007248
7249 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007250
7251#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7252 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7253 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7254#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#if defined(MBEDTLS_DHM_C)
7257 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_ECDH_C)
7260 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007261#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007262#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007263 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007264#if defined(MBEDTLS_SSL_CLI_C)
7265 handshake->ecjpake_cache = NULL;
7266 handshake->ecjpake_cache_len = 0;
7267#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007268#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007269
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007270#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007271 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007272#endif
7273
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007274#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7275 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7276#endif
Hanno Becker75173122019-02-06 16:18:31 +00007277
7278#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7279 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7280 mbedtls_pk_init( &handshake->peer_pubkey );
7281#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007282}
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7289 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 mbedtls_md_init( &transform->md_ctx_enc );
7292 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007293}
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007298}
7299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007301{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007302 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007303 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007305 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007307 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007308 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007309
7310 /*
7311 * Either the pointers are now NULL or cleared properly and can be freed.
7312 * Now allocate missing structures.
7313 */
7314 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007315 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007316 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007317 }
Paul Bakker48916f92012-09-16 19:57:18 +00007318
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007319 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007320 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007321 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007322 }
Paul Bakker48916f92012-09-16 19:57:18 +00007323
Paul Bakker82788fb2014-10-20 13:59:19 +02007324 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007325 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007326 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007327 }
Paul Bakker48916f92012-09-16 19:57:18 +00007328
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007329 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007330 if( ssl->handshake == NULL ||
7331 ssl->transform_negotiate == NULL ||
7332 ssl->session_negotiate == NULL )
7333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 mbedtls_free( ssl->handshake );
7337 mbedtls_free( ssl->transform_negotiate );
7338 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007339
7340 ssl->handshake = NULL;
7341 ssl->transform_negotiate = NULL;
7342 ssl->session_negotiate = NULL;
7343
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007344 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007345 }
7346
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007347 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007349 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007350 ssl_handshake_params_init( ssl->handshake );
7351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7354 {
7355 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007356
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7358 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7359 else
7360 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007361
7362 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007363 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007364#endif
7365
Paul Bakker48916f92012-09-16 19:57:18 +00007366 return( 0 );
7367}
7368
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007369#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007370/* Dummy cookie callbacks for defaults */
7371static int ssl_cookie_write_dummy( void *ctx,
7372 unsigned char **p, unsigned char *end,
7373 const unsigned char *cli_id, size_t cli_id_len )
7374{
7375 ((void) ctx);
7376 ((void) p);
7377 ((void) end);
7378 ((void) cli_id);
7379 ((void) cli_id_len);
7380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007382}
7383
7384static int ssl_cookie_check_dummy( void *ctx,
7385 const unsigned char *cookie, size_t cookie_len,
7386 const unsigned char *cli_id, size_t cli_id_len )
7387{
7388 ((void) ctx);
7389 ((void) cookie);
7390 ((void) cookie_len);
7391 ((void) cli_id);
7392 ((void) cli_id_len);
7393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007395}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007396#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007397
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007398/* Once ssl->out_hdr as the address of the beginning of the
7399 * next outgoing record is set, deduce the other pointers.
7400 *
7401 * Note: For TLS, we save the implicit record sequence number
7402 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7403 * and the caller has to make sure there's space for this.
7404 */
7405
7406static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7407 mbedtls_ssl_transform *transform )
7408{
7409#if defined(MBEDTLS_SSL_PROTO_DTLS)
7410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7411 {
7412 ssl->out_ctr = ssl->out_hdr + 3;
7413 ssl->out_len = ssl->out_hdr + 11;
7414 ssl->out_iv = ssl->out_hdr + 13;
7415 }
7416 else
7417#endif
7418 {
7419 ssl->out_ctr = ssl->out_hdr - 8;
7420 ssl->out_len = ssl->out_hdr + 3;
7421 ssl->out_iv = ssl->out_hdr + 5;
7422 }
7423
7424 /* Adjust out_msg to make space for explicit IV, if used. */
7425 if( transform != NULL &&
7426 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7427 {
7428 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7429 }
7430 else
7431 ssl->out_msg = ssl->out_iv;
7432}
7433
7434/* Once ssl->in_hdr as the address of the beginning of the
7435 * next incoming record is set, deduce the other pointers.
7436 *
7437 * Note: For TLS, we save the implicit record sequence number
7438 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7439 * and the caller has to make sure there's space for this.
7440 */
7441
7442static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7443 mbedtls_ssl_transform *transform )
7444{
7445#if defined(MBEDTLS_SSL_PROTO_DTLS)
7446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7447 {
7448 ssl->in_ctr = ssl->in_hdr + 3;
7449 ssl->in_len = ssl->in_hdr + 11;
7450 ssl->in_iv = ssl->in_hdr + 13;
7451 }
7452 else
7453#endif
7454 {
7455 ssl->in_ctr = ssl->in_hdr - 8;
7456 ssl->in_len = ssl->in_hdr + 3;
7457 ssl->in_iv = ssl->in_hdr + 5;
7458 }
7459
7460 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7461 if( transform != NULL &&
7462 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7463 {
7464 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7465 }
7466 else
7467 ssl->in_msg = ssl->in_iv;
7468}
7469
Paul Bakker5121ce52009-01-03 21:22:43 +00007470/*
7471 * Initialize an SSL context
7472 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007473void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7474{
7475 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7476}
7477
7478/*
7479 * Setup an SSL context
7480 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007481
7482static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7483{
7484 /* Set the incoming and outgoing record pointers. */
7485#if defined(MBEDTLS_SSL_PROTO_DTLS)
7486 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7487 {
7488 ssl->out_hdr = ssl->out_buf;
7489 ssl->in_hdr = ssl->in_buf;
7490 }
7491 else
7492#endif /* MBEDTLS_SSL_PROTO_DTLS */
7493 {
7494 ssl->out_hdr = ssl->out_buf + 8;
7495 ssl->in_hdr = ssl->in_buf + 8;
7496 }
7497
7498 /* Derive other internal pointers. */
7499 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7500 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7501}
7502
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007503int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007504 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505{
Paul Bakker48916f92012-09-16 19:57:18 +00007506 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007507
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007508 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007509
7510 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007511 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007512 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007513
7514 /* Set to NULL in case of an error condition */
7515 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007516
Angus Grattond8213d02016-05-25 20:56:48 +10007517 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7518 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007519 {
Angus Grattond8213d02016-05-25 20:56:48 +10007520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007521 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007522 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007523 }
7524
7525 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7526 if( ssl->out_buf == NULL )
7527 {
7528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007529 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007530 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007531 }
7532
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007533 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007534
Paul Bakker48916f92012-09-16 19:57:18 +00007535 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007536 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007537
7538 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007539
7540error:
7541 mbedtls_free( ssl->in_buf );
7542 mbedtls_free( ssl->out_buf );
7543
7544 ssl->conf = NULL;
7545
7546 ssl->in_buf = NULL;
7547 ssl->out_buf = NULL;
7548
7549 ssl->in_hdr = NULL;
7550 ssl->in_ctr = NULL;
7551 ssl->in_len = NULL;
7552 ssl->in_iv = NULL;
7553 ssl->in_msg = NULL;
7554
7555 ssl->out_hdr = NULL;
7556 ssl->out_ctr = NULL;
7557 ssl->out_len = NULL;
7558 ssl->out_iv = NULL;
7559 ssl->out_msg = NULL;
7560
k-stachowiak9f7798e2018-07-31 16:52:32 +02007561 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007562}
7563
7564/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007565 * Reset an initialized and used SSL context for re-use while retaining
7566 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007567 *
7568 * If partial is non-zero, keep data in the input buffer and client ID.
7569 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007570 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007571static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007572{
Paul Bakker48916f92012-09-16 19:57:18 +00007573 int ret;
7574
Hanno Becker7e772132018-08-10 12:38:21 +01007575#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7576 !defined(MBEDTLS_SSL_SRV_C)
7577 ((void) partial);
7578#endif
7579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007581
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007582 /* Cancel any possibly running timer */
7583 ssl_set_timer( ssl, 0 );
7584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#if defined(MBEDTLS_SSL_RENEGOTIATION)
7586 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007587 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007588
7589 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7591 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007594
Paul Bakker7eb013f2011-10-06 12:37:39 +00007595 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007596 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007597
7598 ssl->in_msgtype = 0;
7599 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007601 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007602 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007605 ssl_dtls_replay_reset( ssl );
7606#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007607
7608 ssl->in_hslen = 0;
7609 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007610
7611 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007612
7613 ssl->out_msgtype = 0;
7614 ssl->out_msglen = 0;
7615 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7617 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007618 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007619#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007620
Hanno Becker19859472018-08-06 09:40:20 +01007621 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7622
Paul Bakker48916f92012-09-16 19:57:18 +00007623 ssl->transform_in = NULL;
7624 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007625
Hanno Becker78640902018-08-13 16:35:15 +01007626 ssl->session_in = NULL;
7627 ssl->session_out = NULL;
7628
Angus Grattond8213d02016-05-25 20:56:48 +10007629 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007630
7631#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007632 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007633#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7634 {
7635 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007636 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007637 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7640 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7643 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007647 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007648 }
7649#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007650
Paul Bakker48916f92012-09-16 19:57:18 +00007651 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_ssl_transform_free( ssl->transform );
7654 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007655 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007656 }
Paul Bakker48916f92012-09-16 19:57:18 +00007657
Paul Bakkerc0463502013-02-14 11:19:38 +01007658 if( ssl->session )
7659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 mbedtls_ssl_session_free( ssl->session );
7661 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007662 ssl->session = NULL;
7663 }
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007666 ssl->alpn_chosen = NULL;
7667#endif
7668
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007669#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007670#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007671 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007672#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007673 {
7674 mbedtls_free( ssl->cli_id );
7675 ssl->cli_id = NULL;
7676 ssl->cli_id_len = 0;
7677 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007678#endif
7679
Paul Bakker48916f92012-09-16 19:57:18 +00007680 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7681 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007682
7683 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007684}
7685
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007686/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007687 * Reset an initialized and used SSL context for re-use while retaining
7688 * all application-set variables, function pointers and data.
7689 */
7690int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7691{
7692 return( ssl_session_reset_int( ssl, 0 ) );
7693}
7694
7695/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007696 * SSL set accessors
7697 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007698void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007699{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007700 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007701}
7702
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007703void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007704{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007705 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007706}
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007709void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007711 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007712}
7713#endif
7714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007716void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007717{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007718 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007719}
7720#endif
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007723
Hanno Becker1841b0a2018-08-24 11:13:57 +01007724void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7725 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007726{
7727 ssl->disable_datagram_packing = !allow_packing;
7728}
7729
7730void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7731 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007733 conf->hs_timeout_min = min;
7734 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007735}
7736#endif
7737
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007738void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007739{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007740 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007741}
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007744void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007745 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007746 void *p_vrfy )
7747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007748 conf->f_vrfy = f_vrfy;
7749 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007752
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007753void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007754 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 void *p_rng )
7756{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007757 conf->f_rng = f_rng;
7758 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007759}
7760
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007761void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007762 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007763 void *p_dbg )
7764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007765 conf->f_dbg = f_dbg;
7766 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007767}
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007770 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007771 mbedtls_ssl_send_t *f_send,
7772 mbedtls_ssl_recv_t *f_recv,
7773 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007774{
7775 ssl->p_bio = p_bio;
7776 ssl->f_send = f_send;
7777 ssl->f_recv = f_recv;
7778 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007779}
7780
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007781#if defined(MBEDTLS_SSL_PROTO_DTLS)
7782void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7783{
7784 ssl->mtu = mtu;
7785}
7786#endif
7787
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007788void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007789{
7790 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007791}
7792
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007793void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7794 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007795 mbedtls_ssl_set_timer_t *f_set_timer,
7796 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007797{
7798 ssl->p_timer = p_timer;
7799 ssl->f_set_timer = f_set_timer;
7800 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007801
7802 /* Make sure we start with no timer running */
7803 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007804}
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007807void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007808 void *p_cache,
7809 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7810 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007811{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007812 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007813 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007814 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#if defined(MBEDTLS_SSL_CLI_C)
7819int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007820{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007821 int ret;
7822
7823 if( ssl == NULL ||
7824 session == NULL ||
7825 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007826 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007829 }
7830
Hanno Becker52055ae2019-02-06 14:30:46 +00007831 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7832 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007833 return( ret );
7834
Paul Bakker0a597072012-09-25 21:55:46 +00007835 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007836
7837 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007840
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007841void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007842 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007844 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7845 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7846 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7847 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007848}
7849
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007850void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007851 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007852 int major, int minor )
7853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007855 return;
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007858 return;
7859
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007860 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007861}
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007864void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007865 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007866{
7867 conf->cert_profile = profile;
7868}
7869
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007870/* Append a new keycert entry to a (possibly empty) list */
7871static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7872 mbedtls_x509_crt *cert,
7873 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007874{
niisato8ee24222018-06-25 19:05:48 +09007875 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007876
niisato8ee24222018-06-25 19:05:48 +09007877 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7878 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007879 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007880
niisato8ee24222018-06-25 19:05:48 +09007881 new_cert->cert = cert;
7882 new_cert->key = key;
7883 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007884
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007885 /* Update head is the list was null, else add to the end */
7886 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007887 {
niisato8ee24222018-06-25 19:05:48 +09007888 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007889 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007890 else
7891 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007892 mbedtls_ssl_key_cert *cur = *head;
7893 while( cur->next != NULL )
7894 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007895 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007896 }
7897
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007898 return( 0 );
7899}
7900
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007901int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007902 mbedtls_x509_crt *own_cert,
7903 mbedtls_pk_context *pk_key )
7904{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007905 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007906}
7907
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007908void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007909 mbedtls_x509_crt *ca_chain,
7910 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007911{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007912 conf->ca_chain = ca_chain;
7913 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00007914
7915#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7916 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7917 * cannot be used together. */
7918 conf->f_ca_cb = NULL;
7919 conf->p_ca_cb = NULL;
7920#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00007921}
Hanno Becker5adaad92019-03-27 16:54:37 +00007922
7923#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7924void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007925 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00007926 void *p_ca_cb )
7927{
7928 conf->f_ca_cb = f_ca_cb;
7929 conf->p_ca_cb = p_ca_cb;
7930
7931 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7932 * cannot be used together. */
7933 conf->ca_chain = NULL;
7934 conf->ca_crl = NULL;
7935}
7936#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007938
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007939#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7940int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7941 mbedtls_x509_crt *own_cert,
7942 mbedtls_pk_context *pk_key )
7943{
7944 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7945 own_cert, pk_key ) );
7946}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007947
7948void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7949 mbedtls_x509_crt *ca_chain,
7950 mbedtls_x509_crl *ca_crl )
7951{
7952 ssl->handshake->sni_ca_chain = ca_chain;
7953 ssl->handshake->sni_ca_crl = ca_crl;
7954}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007955
7956void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7957 int authmode )
7958{
7959 ssl->handshake->sni_authmode = authmode;
7960}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007961#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7962
Hanno Becker8927c832019-04-03 12:52:50 +01007963#if defined(MBEDTLS_X509_CRT_PARSE_C)
7964void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
7965 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
7966 void *p_vrfy )
7967{
7968 ssl->f_vrfy = f_vrfy;
7969 ssl->p_vrfy = p_vrfy;
7970}
7971#endif
7972
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007973#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007974/*
7975 * Set EC J-PAKE password for current handshake
7976 */
7977int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7978 const unsigned char *pw,
7979 size_t pw_len )
7980{
7981 mbedtls_ecjpake_role role;
7982
Janos Follath8eb64132016-06-03 15:40:57 +01007983 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7985
7986 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7987 role = MBEDTLS_ECJPAKE_SERVER;
7988 else
7989 role = MBEDTLS_ECJPAKE_CLIENT;
7990
7991 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7992 role,
7993 MBEDTLS_MD_SHA256,
7994 MBEDTLS_ECP_DP_SECP256R1,
7995 pw, pw_len ) );
7996}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007997#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008000
8001static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8002{
8003 /* Remove reference to existing PSK, if any. */
8004#if defined(MBEDTLS_USE_PSA_CRYPTO)
8005 if( conf->psk_opaque != 0 )
8006 {
8007 /* The maintenance of the PSK key slot is the
8008 * user's responsibility. */
8009 conf->psk_opaque = 0;
8010 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008011 /* This and the following branch should never
8012 * be taken simultaenously as we maintain the
8013 * invariant that raw and opaque PSKs are never
8014 * configured simultaneously. As a safeguard,
8015 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008016#endif /* MBEDTLS_USE_PSA_CRYPTO */
8017 if( conf->psk != NULL )
8018 {
8019 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8020
8021 mbedtls_free( conf->psk );
8022 conf->psk = NULL;
8023 conf->psk_len = 0;
8024 }
8025
8026 /* Remove reference to PSK identity, if any. */
8027 if( conf->psk_identity != NULL )
8028 {
8029 mbedtls_free( conf->psk_identity );
8030 conf->psk_identity = NULL;
8031 conf->psk_identity_len = 0;
8032 }
8033}
8034
Hanno Becker7390c712018-11-15 13:33:04 +00008035/* This function assumes that PSK identity in the SSL config is unset.
8036 * It checks that the provided identity is well-formed and attempts
8037 * to make a copy of it in the SSL config.
8038 * On failure, the PSK identity in the config remains unset. */
8039static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8040 unsigned char const *psk_identity,
8041 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008042{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008043 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008044 if( psk_identity == NULL ||
8045 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008046 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008047 {
8048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8049 }
8050
Hanno Becker7390c712018-11-15 13:33:04 +00008051 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8052 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008053 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008054
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008055 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008056 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008057
8058 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008059}
8060
Hanno Becker7390c712018-11-15 13:33:04 +00008061int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8062 const unsigned char *psk, size_t psk_len,
8063 const unsigned char *psk_identity, size_t psk_identity_len )
8064{
8065 int ret;
8066 /* Remove opaque/raw PSK + PSK Identity */
8067 ssl_conf_remove_psk( conf );
8068
8069 /* Check and set raw PSK */
8070 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8072 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8074 conf->psk_len = psk_len;
8075 memcpy( conf->psk, psk, conf->psk_len );
8076
8077 /* Check and set PSK Identity */
8078 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8079 if( ret != 0 )
8080 ssl_conf_remove_psk( conf );
8081
8082 return( ret );
8083}
8084
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008085static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8086{
8087#if defined(MBEDTLS_USE_PSA_CRYPTO)
8088 if( ssl->handshake->psk_opaque != 0 )
8089 {
8090 ssl->handshake->psk_opaque = 0;
8091 }
8092 else
8093#endif /* MBEDTLS_USE_PSA_CRYPTO */
8094 if( ssl->handshake->psk != NULL )
8095 {
8096 mbedtls_platform_zeroize( ssl->handshake->psk,
8097 ssl->handshake->psk_len );
8098 mbedtls_free( ssl->handshake->psk );
8099 ssl->handshake->psk_len = 0;
8100 }
8101}
8102
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008103int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8104 const unsigned char *psk, size_t psk_len )
8105{
8106 if( psk == NULL || ssl->handshake == NULL )
8107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8108
8109 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8111
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008112 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008113
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008114 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008116
8117 ssl->handshake->psk_len = psk_len;
8118 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8119
8120 return( 0 );
8121}
8122
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008123#if defined(MBEDTLS_USE_PSA_CRYPTO)
8124int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008125 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008126 const unsigned char *psk_identity,
8127 size_t psk_identity_len )
8128{
Hanno Becker7390c712018-11-15 13:33:04 +00008129 int ret;
8130 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008131 ssl_conf_remove_psk( conf );
8132
Hanno Becker7390c712018-11-15 13:33:04 +00008133 /* Check and set opaque PSK */
8134 if( psk_slot == 0 )
8135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008136 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008137
8138 /* Check and set PSK Identity */
8139 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8140 psk_identity_len );
8141 if( ret != 0 )
8142 ssl_conf_remove_psk( conf );
8143
8144 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008145}
8146
8147int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008148 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008149{
8150 if( psk_slot == 0 || ssl->handshake == NULL )
8151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8152
8153 ssl_remove_psk( ssl );
8154 ssl->handshake->psk_opaque = psk_slot;
8155 return( 0 );
8156}
8157#endif /* MBEDTLS_USE_PSA_CRYPTO */
8158
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008159void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008161 size_t),
8162 void *p_psk )
8163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008164 conf->f_psk = f_psk;
8165 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008168
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008169#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008170
8171#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008173{
8174 int ret;
8175
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008176 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8177 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8178 {
8179 mbedtls_mpi_free( &conf->dhm_P );
8180 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008181 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008183
8184 return( 0 );
8185}
Hanno Becker470a8c42017-10-04 15:28:46 +01008186#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008187
Hanno Beckera90658f2017-10-04 15:29:08 +01008188int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8189 const unsigned char *dhm_P, size_t P_len,
8190 const unsigned char *dhm_G, size_t G_len )
8191{
8192 int ret;
8193
8194 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8195 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8196 {
8197 mbedtls_mpi_free( &conf->dhm_P );
8198 mbedtls_mpi_free( &conf->dhm_G );
8199 return( ret );
8200 }
8201
8202 return( 0 );
8203}
Paul Bakker5121ce52009-01-03 21:22:43 +00008204
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008205int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008206{
8207 int ret;
8208
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008209 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8210 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8211 {
8212 mbedtls_mpi_free( &conf->dhm_P );
8213 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008214 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008215 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008216
8217 return( 0 );
8218}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008219#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008220
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008221#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8222/*
8223 * Set the minimum length for Diffie-Hellman parameters
8224 */
8225void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8226 unsigned int bitlen )
8227{
8228 conf->dhm_min_bitlen = bitlen;
8229}
8230#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8231
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008232#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008233/*
8234 * Set allowed/preferred hashes for handshake signatures
8235 */
8236void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8237 const int *hashes )
8238{
8239 conf->sig_hashes = hashes;
8240}
Hanno Becker947194e2017-04-07 13:25:49 +01008241#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008242
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008243#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008244/*
8245 * Set the allowed elliptic curves
8246 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008247void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008248 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008250 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008251}
Hanno Becker947194e2017-04-07 13:25:49 +01008252#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008253
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008254#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008256{
Hanno Becker947194e2017-04-07 13:25:49 +01008257 /* Initialize to suppress unnecessary compiler warning */
8258 size_t hostname_len = 0;
8259
8260 /* Check if new hostname is valid before
8261 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008262 if( hostname != NULL )
8263 {
8264 hostname_len = strlen( hostname );
8265
8266 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8268 }
8269
8270 /* Now it's clear that we will overwrite the old hostname,
8271 * so we can free it safely */
8272
8273 if( ssl->hostname != NULL )
8274 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008275 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008276 mbedtls_free( ssl->hostname );
8277 }
8278
8279 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008280
Paul Bakker5121ce52009-01-03 21:22:43 +00008281 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008282 {
8283 ssl->hostname = NULL;
8284 }
8285 else
8286 {
8287 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008288 if( ssl->hostname == NULL )
8289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008290
Hanno Becker947194e2017-04-07 13:25:49 +01008291 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008292
Hanno Becker947194e2017-04-07 13:25:49 +01008293 ssl->hostname[hostname_len] = '\0';
8294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
8296 return( 0 );
8297}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008298#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008299
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008300#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008301void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008303 const unsigned char *, size_t),
8304 void *p_sni )
8305{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008306 conf->f_sni = f_sni;
8307 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008312int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008313{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008314 size_t cur_len, tot_len;
8315 const char **p;
8316
8317 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008318 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8319 * MUST NOT be truncated."
8320 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008321 */
8322 tot_len = 0;
8323 for( p = protos; *p != NULL; p++ )
8324 {
8325 cur_len = strlen( *p );
8326 tot_len += cur_len;
8327
8328 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008330 }
8331
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008332 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008333
8334 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008335}
8336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008338{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008339 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008342
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008343void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008344{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008345 conf->max_major_ver = major;
8346 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008347}
8348
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008349void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008351 conf->min_major_ver = major;
8352 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008353}
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008356void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008357{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008358 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008359}
8360#endif
8361
Janos Follath088ce432017-04-10 12:42:31 +01008362#if defined(MBEDTLS_SSL_SRV_C)
8363void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8364 char cert_req_ca_list )
8365{
8366 conf->cert_req_ca_list = cert_req_ca_list;
8367}
8368#endif
8369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008371void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008373 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008374}
8375#endif
8376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008378void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008380 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008381}
8382#endif
8383
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008384#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008385void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008386{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008387 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008388}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008389#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008392int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008395 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008398 }
8399
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008400 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008401
8402 return( 0 );
8403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008407void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008415{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008416 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008417}
8418#endif
8419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008422 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008423}
8424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008428 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008429}
8430
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008431void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008434}
8435
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008436void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008437 const unsigned char period[8] )
8438{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008439 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008444#if defined(MBEDTLS_SSL_CLI_C)
8445void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008446{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008447 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008448}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008449#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008450
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008451#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008452void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8453 mbedtls_ssl_ticket_write_t *f_ticket_write,
8454 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8455 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008456{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008457 conf->f_ticket_write = f_ticket_write;
8458 conf->f_ticket_parse = f_ticket_parse;
8459 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008460}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008463
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008464#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8465void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8466 mbedtls_ssl_export_keys_t *f_export_keys,
8467 void *p_export_keys )
8468{
8469 conf->f_export_keys = f_export_keys;
8470 conf->p_export_keys = p_export_keys;
8471}
8472#endif
8473
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008474#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008475void mbedtls_ssl_conf_async_private_cb(
8476 mbedtls_ssl_config *conf,
8477 mbedtls_ssl_async_sign_t *f_async_sign,
8478 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8479 mbedtls_ssl_async_resume_t *f_async_resume,
8480 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008481 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008482{
8483 conf->f_async_sign_start = f_async_sign;
8484 conf->f_async_decrypt_start = f_async_decrypt;
8485 conf->f_async_resume = f_async_resume;
8486 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008487 conf->p_async_config_data = async_config_data;
8488}
8489
Gilles Peskine8f97af72018-04-26 11:46:10 +02008490void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8491{
8492 return( conf->p_async_config_data );
8493}
8494
Gilles Peskine1febfef2018-04-30 11:54:39 +02008495void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008496{
8497 if( ssl->handshake == NULL )
8498 return( NULL );
8499 else
8500 return( ssl->handshake->user_async_ctx );
8501}
8502
Gilles Peskine1febfef2018-04-30 11:54:39 +02008503void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008504 void *ctx )
8505{
8506 if( ssl->handshake != NULL )
8507 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008508}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008509#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008510
Paul Bakker5121ce52009-01-03 21:22:43 +00008511/*
8512 * SSL get accessors
8513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008515{
8516 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8517}
8518
Hanno Becker8b170a02017-10-10 11:51:19 +01008519int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8520{
8521 /*
8522 * Case A: We're currently holding back
8523 * a message for further processing.
8524 */
8525
8526 if( ssl->keep_current_message == 1 )
8527 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008529 return( 1 );
8530 }
8531
8532 /*
8533 * Case B: Further records are pending in the current datagram.
8534 */
8535
8536#if defined(MBEDTLS_SSL_PROTO_DTLS)
8537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8538 ssl->in_left > ssl->next_record_offset )
8539 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008541 return( 1 );
8542 }
8543#endif /* MBEDTLS_SSL_PROTO_DTLS */
8544
8545 /*
8546 * Case C: A handshake message is being processed.
8547 */
8548
Hanno Becker8b170a02017-10-10 11:51:19 +01008549 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8550 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008552 return( 1 );
8553 }
8554
8555 /*
8556 * Case D: An application data message is being processed
8557 */
8558 if( ssl->in_offt != NULL )
8559 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008561 return( 1 );
8562 }
8563
8564 /*
8565 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008566 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008567 * we implement support for multiple alerts in single records.
8568 */
8569
8570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8571 return( 0 );
8572}
8573
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008574uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008575{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008576 if( ssl->session != NULL )
8577 return( ssl->session->verify_result );
8578
8579 if( ssl->session_negotiate != NULL )
8580 return( ssl->session_negotiate->verify_result );
8581
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008582 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008583}
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008586{
Paul Bakker926c8e42013-03-06 10:23:34 +01008587 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008588 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008591}
8592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008597 {
8598 switch( ssl->minor_ver )
8599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008601 return( "DTLSv1.0" );
8602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008604 return( "DTLSv1.2" );
8605
8606 default:
8607 return( "unknown (DTLS)" );
8608 }
8609 }
8610#endif
8611
Paul Bakker43ca69c2011-01-15 17:35:19 +00008612 switch( ssl->minor_ver )
8613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008615 return( "SSLv3.0" );
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008618 return( "TLSv1.0" );
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008621 return( "TLSv1.1" );
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008624 return( "TLSv1.2" );
8625
Paul Bakker43ca69c2011-01-15 17:35:19 +00008626 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008627 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008628 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008629}
8630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008632{
Hanno Becker3136ede2018-08-17 15:28:19 +01008633 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008635 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008636
Hanno Becker78640902018-08-13 16:35:15 +01008637 if( transform == NULL )
8638 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640#if defined(MBEDTLS_ZLIB_SUPPORT)
8641 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8642 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008643#endif
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 case MBEDTLS_MODE_GCM:
8648 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008649 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008651 transform_expansion = transform->minlen;
8652 break;
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008655
8656 block_size = mbedtls_cipher_get_block_size(
8657 &transform->cipher_ctx_enc );
8658
Hanno Becker3136ede2018-08-17 15:28:19 +01008659 /* Expansion due to the addition of the MAC. */
8660 transform_expansion += transform->maclen;
8661
8662 /* Expansion due to the addition of CBC padding;
8663 * Theoretically up to 256 bytes, but we never use
8664 * more than the block size of the underlying cipher. */
8665 transform_expansion += block_size;
8666
8667 /* For TLS 1.1 or higher, an explicit IV is added
8668 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008669#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8670 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008671 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008672#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008673
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008674 break;
8675
8676 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008679 }
8680
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008681 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008682}
8683
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008684#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8685size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8686{
8687 size_t max_len;
8688
8689 /*
8690 * Assume mfl_code is correct since it was checked when set
8691 */
Angus Grattond8213d02016-05-25 20:56:48 +10008692 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008693
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008694 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008695 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008696 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008697 {
Angus Grattond8213d02016-05-25 20:56:48 +10008698 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008699 }
8700
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008701 /* During a handshake, use the value being negotiated */
8702 if( ssl->session_negotiate != NULL &&
8703 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8704 {
8705 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8706 }
8707
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008708 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008709}
8710#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8711
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008712#if defined(MBEDTLS_SSL_PROTO_DTLS)
8713static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8714{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008715 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8717 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8718 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8719 return ( 0 );
8720
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008721 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8722 return( ssl->mtu );
8723
8724 if( ssl->mtu == 0 )
8725 return( ssl->handshake->mtu );
8726
8727 return( ssl->mtu < ssl->handshake->mtu ?
8728 ssl->mtu : ssl->handshake->mtu );
8729}
8730#endif /* MBEDTLS_SSL_PROTO_DTLS */
8731
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008732int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8733{
8734 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8735
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008736#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8737 !defined(MBEDTLS_SSL_PROTO_DTLS)
8738 (void) ssl;
8739#endif
8740
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008741#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8742 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8743
8744 if( max_len > mfl )
8745 max_len = mfl;
8746#endif
8747
8748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008749 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008750 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008751 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008752 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8753 const size_t overhead = (size_t) ret;
8754
8755 if( ret < 0 )
8756 return( ret );
8757
8758 if( mtu <= overhead )
8759 {
8760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8761 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8762 }
8763
8764 if( max_len > mtu - overhead )
8765 max_len = mtu - overhead;
8766 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008767#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008768
Hanno Becker0defedb2018-08-10 12:35:02 +01008769#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8770 !defined(MBEDTLS_SSL_PROTO_DTLS)
8771 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008772#endif
8773
8774 return( (int) max_len );
8775}
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777#if defined(MBEDTLS_X509_CRT_PARSE_C)
8778const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008779{
8780 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008781 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008782
Hanno Beckere6824572019-02-07 13:18:46 +00008783#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008784 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008785#else
8786 return( NULL );
8787#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008792int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8793 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008794{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008795 if( ssl == NULL ||
8796 dst == NULL ||
8797 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008798 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008801 }
8802
Hanno Becker52055ae2019-02-06 14:30:46 +00008803 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008806
Paul Bakker5121ce52009-01-03 21:22:43 +00008807/*
Paul Bakker1961b702013-01-25 14:49:24 +01008808 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008813
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008814 if( ssl == NULL || ssl->conf == NULL )
8815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008824#endif
8825
Paul Bakker1961b702013-01-25 14:49:24 +01008826 return( ret );
8827}
8828
8829/*
8830 * Perform the SSL handshake
8831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008833{
8834 int ret = 0;
8835
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008836 if( ssl == NULL || ssl->conf == NULL )
8837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008844
8845 if( ret != 0 )
8846 break;
8847 }
8848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008850
8851 return( ret );
8852}
8853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_SSL_RENEGOTIATION)
8855#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008856/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008857 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008860{
8861 int ret;
8862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008864
8865 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8867 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008868
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008869 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008870 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008872 return( ret );
8873 }
8874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008876
8877 return( 0 );
8878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008880
8881/*
8882 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883 * - any side: calling mbedtls_ssl_renegotiate(),
8884 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8885 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008886 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008887 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008891{
8892 int ret;
8893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008895
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008896 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8897 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008898
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008899 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8900 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008904 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008905 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008906 ssl->handshake->out_msg_seq = 1;
8907 else
8908 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008909 }
8910#endif
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8913 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008918 return( ret );
8919 }
8920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008922
8923 return( 0 );
8924}
8925
8926/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008927 * Renegotiate current connection on client,
8928 * or request renegotiation on server
8929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008933
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008934 if( ssl == NULL || ssl->conf == NULL )
8935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008938 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008939 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008945
8946 /* Did we already try/start sending HelloRequest? */
8947 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008949
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008950 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008955 /*
8956 * On client, either start the renegotiation process or,
8957 * if already in progress, continue the handshake
8958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008963
8964 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008967 return( ret );
8968 }
8969 }
8970 else
8971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008975 return( ret );
8976 }
8977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008979
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008980 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008981}
8982
8983/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008984 * Check record counters and renegotiate if they're above the limit.
8985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008987{
Andres AG2196c7f2016-12-15 17:01:16 +00008988 size_t ep_len = ssl_ep_len( ssl );
8989 int in_ctr_cmp;
8990 int out_ctr_cmp;
8991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8993 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008994 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008995 {
8996 return( 0 );
8997 }
8998
Andres AG2196c7f2016-12-15 17:01:16 +00008999 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9000 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009001 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009002 ssl->conf->renego_period + ep_len, 8 - ep_len );
9003
9004 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009005 {
9006 return( 0 );
9007 }
9008
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009013
9014/*
9015 * Receive application data decrypted from the SSL layer
9016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009018{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009019 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009020 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009021
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009022 if( ssl == NULL || ssl->conf == NULL )
9023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009031 return( ret );
9032
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009033 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009035 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009036 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009037 return( ret );
9038 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009039 }
9040#endif
9041
Hanno Becker4a810fb2017-05-24 16:27:30 +01009042 /*
9043 * Check if renegotiation is necessary and/or handshake is
9044 * in process. If yes, perform/continue, and fall through
9045 * if an unexpected packet is received while the client
9046 * is waiting for the ServerHello.
9047 *
9048 * (There is no equivalent to the last condition on
9049 * the server-side as it is not treated as within
9050 * a handshake while waiting for the ClientHello
9051 * after a renegotiation request.)
9052 */
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009055 ret = ssl_check_ctr_renegotiate( ssl );
9056 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9057 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009060 return( ret );
9061 }
9062#endif
9063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009067 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9068 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009071 return( ret );
9072 }
9073 }
9074
Hanno Beckere41158b2017-10-23 13:30:32 +01009075 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009076 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009077 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009078 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009079 if( ssl->f_get_timer != NULL &&
9080 ssl->f_get_timer( ssl->p_timer ) == -1 )
9081 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009082 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009083 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009084
Hanno Becker327c93b2018-08-15 13:56:18 +01009085 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009086 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009087 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9088 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009089
Hanno Becker4a810fb2017-05-24 16:27:30 +01009090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9091 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009092 }
9093
9094 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009096 {
9097 /*
9098 * OpenSSL sends empty messages to randomize the IV
9099 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009100 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009103 return( 0 );
9104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009106 return( ret );
9107 }
9108 }
9109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009113
Hanno Becker4a810fb2017-05-24 16:27:30 +01009114 /*
9115 * - For client-side, expect SERVER_HELLO_REQUEST.
9116 * - For server-side, expect CLIENT_HELLO.
9117 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9118 */
9119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009121 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009123 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009126
9127 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009129 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009130 {
9131 continue;
9132 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009135 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009136#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009137
Hanno Becker4a810fb2017-05-24 16:27:30 +01009138#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009139 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009143
9144 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009147 {
9148 continue;
9149 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009152 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009153#endif /* MBEDTLS_SSL_SRV_C */
9154
Hanno Becker21df7f92017-10-17 11:03:26 +01009155#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009156 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009157 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9158 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9159 ssl->conf->allow_legacy_renegotiation ==
9160 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9161 {
9162 /*
9163 * Accept renegotiation request
9164 */
Paul Bakker48916f92012-09-16 19:57:18 +00009165
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009166 /* DTLS clients need to know renego is server-initiated */
9167#if defined(MBEDTLS_SSL_PROTO_DTLS)
9168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9169 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9170 {
9171 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9172 }
9173#endif
9174 ret = ssl_start_renegotiation( ssl );
9175 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9176 ret != 0 )
9177 {
9178 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9179 return( ret );
9180 }
9181 }
9182 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009183#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009184 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009185 /*
9186 * Refuse renegotiation
9187 */
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191#if defined(MBEDTLS_SSL_PROTO_SSL3)
9192 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009193 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009194 /* SSLv3 does not have a "no_renegotiation" warning, so
9195 we send a fatal alert and abort the connection. */
9196 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9197 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9198 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009199 }
9200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9202#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9203 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9204 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9207 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9208 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009209 {
9210 return( ret );
9211 }
Paul Bakker48916f92012-09-16 19:57:18 +00009212 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009213 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9215 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009219 }
Paul Bakker48916f92012-09-16 19:57:18 +00009220 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009221
Hanno Becker90333da2017-10-10 11:27:13 +01009222 /* At this point, we don't know whether the renegotiation has been
9223 * completed or not. The cases to consider are the following:
9224 * 1) The renegotiation is complete. In this case, no new record
9225 * has been read yet.
9226 * 2) The renegotiation is incomplete because the client received
9227 * an application data record while awaiting the ServerHello.
9228 * 3) The renegotiation is incomplete because the client received
9229 * a non-handshake, non-application data message while awaiting
9230 * the ServerHello.
9231 * In each of these case, looping will be the proper action:
9232 * - For 1), the next iteration will read a new record and check
9233 * if it's application data.
9234 * - For 2), the loop condition isn't satisfied as application data
9235 * is present, hence continue is the same as break
9236 * - For 3), the loop condition is satisfied and read_record
9237 * will re-deliver the message that was held back by the client
9238 * when expecting the ServerHello.
9239 */
9240 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009241 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009242#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009244 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009245 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009246 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009247 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009250 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009252 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009253 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9258 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009261 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009262 }
9263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9267 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009268 }
9269
9270 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009271
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009272 /* We're going to return something now, cancel timer,
9273 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009275 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009276
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009278 /* If we requested renego but received AppData, resend HelloRequest.
9279 * Do it now, after setting in_offt, to avoid taking this branch
9280 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009282 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009284 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009285 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009288 return( ret );
9289 }
9290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009292#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009293 }
9294
9295 n = ( len < ssl->in_msglen )
9296 ? len : ssl->in_msglen;
9297
9298 memcpy( buf, ssl->in_offt, n );
9299 ssl->in_msglen -= n;
9300
9301 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009302 {
9303 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009304 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009305 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009307 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009308 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009309 /* more data available */
9310 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009314
Paul Bakker23986e52011-04-24 08:57:21 +00009315 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009316}
9317
9318/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009319 * Send application data to be encrypted by the SSL layer, taking care of max
9320 * fragment length and buffer size.
9321 *
9322 * According to RFC 5246 Section 6.2.1:
9323 *
9324 * Zero-length fragments of Application data MAY be sent as they are
9325 * potentially useful as a traffic analysis countermeasure.
9326 *
9327 * Therefore, it is possible that the input message length is 0 and the
9328 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009329 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009330static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009331 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009332{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009333 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9334 const size_t max_len = (size_t) ret;
9335
9336 if( ret < 0 )
9337 {
9338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9339 return( ret );
9340 }
9341
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009342 if( len > max_len )
9343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009348 "maximum fragment length: %d > %d",
9349 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009351 }
9352 else
9353#endif
9354 len = max_len;
9355 }
Paul Bakker887bd502011-06-08 13:10:54 +00009356
Paul Bakker5121ce52009-01-03 21:22:43 +00009357 if( ssl->out_left != 0 )
9358 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009359 /*
9360 * The user has previously tried to send the data and
9361 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9362 * written. In this case, we expect the high-level write function
9363 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009368 return( ret );
9369 }
9370 }
Paul Bakker887bd502011-06-08 13:10:54 +00009371 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009372 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009373 /*
9374 * The user is trying to send a message the first time, so we need to
9375 * copy the data into the internal buffers and setup the data structure
9376 * to keep track of partial writes
9377 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009378 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009380 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009381
Hanno Becker67bc7c32018-08-06 11:33:50 +01009382 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009385 return( ret );
9386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009387 }
9388
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009389 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009390}
9391
9392/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009393 * Write application data, doing 1/n-1 splitting if necessary.
9394 *
9395 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009396 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009397 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009400static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009401 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009402{
9403 int ret;
9404
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009405 if( ssl->conf->cbc_record_splitting ==
9406 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009407 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9409 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9410 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009411 {
9412 return( ssl_write_real( ssl, buf, len ) );
9413 }
9414
9415 if( ssl->split_done == 0 )
9416 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009417 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009418 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009419 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009420 }
9421
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009422 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9423 return( ret );
9424 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009425
9426 return( ret + 1 );
9427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009429
9430/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009431 * Write application data (public-facing wrapper)
9432 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009433int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009434{
9435 int ret;
9436
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009438
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009439 if( ssl == NULL || ssl->conf == NULL )
9440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9441
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009442#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009443 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9444 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009445 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009446 return( ret );
9447 }
9448#endif
9449
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009450 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009451 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009452 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009453 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009455 return( ret );
9456 }
9457 }
9458
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009459#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009460 ret = ssl_write_split( ssl, buf, len );
9461#else
9462 ret = ssl_write_real( ssl, buf, len );
9463#endif
9464
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009466
9467 return( ret );
9468}
9469
9470/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009471 * Notify the peer that the connection is being closed
9472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009474{
9475 int ret;
9476
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009477 if( ssl == NULL || ssl->conf == NULL )
9478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009481
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009482 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9488 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9489 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009492 return( ret );
9493 }
9494 }
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009497
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009498 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009499}
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009502{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009503 if( transform == NULL )
9504 return;
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009507 deflateEnd( &transform->ctx_deflate );
9508 inflateEnd( &transform->ctx_inflate );
9509#endif
9510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9512 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 mbedtls_md_free( &transform->md_ctx_enc );
9515 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009516
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009517 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009518}
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_X509_CRT_PARSE_C)
9521static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009524
9525 while( cur != NULL )
9526 {
9527 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009529 cur = next;
9530 }
9531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009533
Hanno Becker0271f962018-08-16 13:23:47 +01009534#if defined(MBEDTLS_SSL_PROTO_DTLS)
9535
9536static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9537{
9538 unsigned offset;
9539 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9540
9541 if( hs == NULL )
9542 return;
9543
Hanno Becker283f5ef2018-08-24 09:34:47 +01009544 ssl_free_buffered_record( ssl );
9545
Hanno Becker0271f962018-08-16 13:23:47 +01009546 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009547 ssl_buffering_free_slot( ssl, offset );
9548}
9549
9550static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9551 uint8_t slot )
9552{
9553 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9554 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009555
9556 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9557 return;
9558
Hanno Beckere605b192018-08-21 15:59:07 +01009559 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009560 {
Hanno Beckere605b192018-08-21 15:59:07 +01009561 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009562 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009563 mbedtls_free( hs_buf->data );
9564 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009565 }
9566}
9567
9568#endif /* MBEDTLS_SSL_PROTO_DTLS */
9569
Gilles Peskine9b562d52018-04-25 20:32:43 +02009570void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009571{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009572 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9573
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009574 if( handshake == NULL )
9575 return;
9576
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009577#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9578 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9579 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009580 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009581 handshake->async_in_progress = 0;
9582 }
9583#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9584
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009585#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9586 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9587 mbedtls_md5_free( &handshake->fin_md5 );
9588 mbedtls_sha1_free( &handshake->fin_sha1 );
9589#endif
9590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9591#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009592#if defined(MBEDTLS_USE_PSA_CRYPTO)
9593 psa_hash_abort( &handshake->fin_sha256_psa );
9594#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009595 mbedtls_sha256_free( &handshake->fin_sha256 );
9596#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009597#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009598#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009599#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009600 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009601#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009602 mbedtls_sha512_free( &handshake->fin_sha512 );
9603#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009604#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_DHM_C)
9608 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610#if defined(MBEDTLS_ECDH_C)
9611 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009612#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009613#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009614 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009615#if defined(MBEDTLS_SSL_CLI_C)
9616 mbedtls_free( handshake->ecjpake_cache );
9617 handshake->ecjpake_cache = NULL;
9618 handshake->ecjpake_cache_len = 0;
9619#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009620#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009621
Janos Follath4ae5c292016-02-10 11:27:43 +00009622#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9623 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009624 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009626#endif
9627
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009628#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9629 if( handshake->psk != NULL )
9630 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009631 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009632 mbedtls_free( handshake->psk );
9633 }
9634#endif
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9637 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009638 /*
9639 * Free only the linked list wrapper, not the keys themselves
9640 * since the belong to the SNI callback
9641 */
9642 if( handshake->sni_key_cert != NULL )
9643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009645
9646 while( cur != NULL )
9647 {
9648 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009650 cur = next;
9651 }
9652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009654
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009655#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009656 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009657 if( handshake->ecrs_peer_cert != NULL )
9658 {
9659 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9660 mbedtls_free( handshake->ecrs_peer_cert );
9661 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009662#endif
9663
Hanno Becker75173122019-02-06 16:18:31 +00009664#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9665 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9666 mbedtls_pk_free( &handshake->peer_pubkey );
9667#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SSL_PROTO_DTLS)
9670 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009671 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009672 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009673#endif
9674
Hanno Becker4a63ed42019-01-08 11:39:35 +00009675#if defined(MBEDTLS_ECDH_C) && \
9676 defined(MBEDTLS_USE_PSA_CRYPTO)
9677 psa_destroy_key( handshake->ecdh_psa_privkey );
9678#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9679
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009680 mbedtls_platform_zeroize( handshake,
9681 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009682}
9683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009685{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009686 if( session == NULL )
9687 return;
9688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009690 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009691#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009692
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009693#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009695#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009696
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009697 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009698}
9699
Paul Bakker5121ce52009-01-03 21:22:43 +00009700/*
9701 * Free an SSL context
9702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009704{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009705 if( ssl == NULL )
9706 return;
9707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009709
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009710 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009711 {
Angus Grattond8213d02016-05-25 20:56:48 +10009712 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009714 }
9715
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009716 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 {
Angus Grattond8213d02016-05-25 20:56:48 +10009718 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009720 }
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009723 if( ssl->compress_buf != NULL )
9724 {
Angus Grattond8213d02016-05-25 20:56:48 +10009725 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009727 }
9728#endif
9729
Paul Bakker48916f92012-09-16 19:57:18 +00009730 if( ssl->transform )
9731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 mbedtls_ssl_transform_free( ssl->transform );
9733 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009734 }
9735
9736 if( ssl->handshake )
9737 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009738 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9740 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 mbedtls_free( ssl->handshake );
9743 mbedtls_free( ssl->transform_negotiate );
9744 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009745 }
9746
Paul Bakkerc0463502013-02-14 11:19:38 +01009747 if( ssl->session )
9748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 mbedtls_ssl_session_free( ssl->session );
9750 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009751 }
9752
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009753#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009754 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009755 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009756 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009758 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009759#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9762 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9765 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009766 }
9767#endif
9768
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009769#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009771#endif
9772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009774
Paul Bakker86f04f42013-02-14 11:20:09 +01009775 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009776 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009777}
9778
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009779/*
9780 * Initialze mbedtls_ssl_config
9781 */
9782void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9783{
9784 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9785}
9786
Simon Butcherc97b6972015-12-27 23:48:17 +00009787#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009788static int ssl_preset_default_hashes[] = {
9789#if defined(MBEDTLS_SHA512_C)
9790 MBEDTLS_MD_SHA512,
9791 MBEDTLS_MD_SHA384,
9792#endif
9793#if defined(MBEDTLS_SHA256_C)
9794 MBEDTLS_MD_SHA256,
9795 MBEDTLS_MD_SHA224,
9796#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009797#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009798 MBEDTLS_MD_SHA1,
9799#endif
9800 MBEDTLS_MD_NONE
9801};
Simon Butcherc97b6972015-12-27 23:48:17 +00009802#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009803
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009804static int ssl_preset_suiteb_ciphersuites[] = {
9805 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9806 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9807 0
9808};
9809
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009810#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009811static int ssl_preset_suiteb_hashes[] = {
9812 MBEDTLS_MD_SHA256,
9813 MBEDTLS_MD_SHA384,
9814 MBEDTLS_MD_NONE
9815};
9816#endif
9817
9818#if defined(MBEDTLS_ECP_C)
9819static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9820 MBEDTLS_ECP_DP_SECP256R1,
9821 MBEDTLS_ECP_DP_SECP384R1,
9822 MBEDTLS_ECP_DP_NONE
9823};
9824#endif
9825
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009826/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009827 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009828 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009829int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009830 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009831{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009832#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009833 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009834#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009835
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009836 /* Use the functions here so that they are covered in tests,
9837 * but otherwise access member directly for efficiency */
9838 mbedtls_ssl_conf_endpoint( conf, endpoint );
9839 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009840
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009841 /*
9842 * Things that are common to all presets
9843 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009844#if defined(MBEDTLS_SSL_CLI_C)
9845 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9846 {
9847 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9848#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9849 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9850#endif
9851 }
9852#endif
9853
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009854#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009855 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009856#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009857
9858#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9859 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9860#endif
9861
9862#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9863 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9864#endif
9865
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009866#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9867 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9868#endif
9869
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009870#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009871 conf->f_cookie_write = ssl_cookie_write_dummy;
9872 conf->f_cookie_check = ssl_cookie_check_dummy;
9873#endif
9874
9875#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9876 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9877#endif
9878
Janos Follath088ce432017-04-10 12:42:31 +01009879#if defined(MBEDTLS_SSL_SRV_C)
9880 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9881#endif
9882
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009883#if defined(MBEDTLS_SSL_PROTO_DTLS)
9884 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9885 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9886#endif
9887
9888#if defined(MBEDTLS_SSL_RENEGOTIATION)
9889 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009890 memset( conf->renego_period, 0x00, 2 );
9891 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009892#endif
9893
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009894#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9895 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9896 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009897 const unsigned char dhm_p[] =
9898 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9899 const unsigned char dhm_g[] =
9900 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9901
Hanno Beckera90658f2017-10-04 15:29:08 +01009902 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9903 dhm_p, sizeof( dhm_p ),
9904 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009905 {
9906 return( ret );
9907 }
9908 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009909#endif
9910
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009911 /*
9912 * Preset-specific defaults
9913 */
9914 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009915 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009916 /*
9917 * NSA Suite B
9918 */
9919 case MBEDTLS_SSL_PRESET_SUITEB:
9920 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9921 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9922 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9923 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9924
9925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9927 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9928 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9929 ssl_preset_suiteb_ciphersuites;
9930
9931#if defined(MBEDTLS_X509_CRT_PARSE_C)
9932 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009933#endif
9934
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009935#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009936 conf->sig_hashes = ssl_preset_suiteb_hashes;
9937#endif
9938
9939#if defined(MBEDTLS_ECP_C)
9940 conf->curve_list = ssl_preset_suiteb_curves;
9941#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009942 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009943
9944 /*
9945 * Default
9946 */
9947 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009948 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9949 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9950 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9951 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9952 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9953 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9954 MBEDTLS_SSL_MIN_MINOR_VERSION :
9955 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009956 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9957 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9958
9959#if defined(MBEDTLS_SSL_PROTO_DTLS)
9960 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9961 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9962#endif
9963
9964 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9966 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9968 mbedtls_ssl_list_ciphersuites();
9969
9970#if defined(MBEDTLS_X509_CRT_PARSE_C)
9971 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9972#endif
9973
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009974#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009975 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009976#endif
9977
9978#if defined(MBEDTLS_ECP_C)
9979 conf->curve_list = mbedtls_ecp_grp_id_list();
9980#endif
9981
9982#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9983 conf->dhm_min_bitlen = 1024;
9984#endif
9985 }
9986
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009987 return( 0 );
9988}
9989
9990/*
9991 * Free mbedtls_ssl_config
9992 */
9993void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9994{
9995#if defined(MBEDTLS_DHM_C)
9996 mbedtls_mpi_free( &conf->dhm_P );
9997 mbedtls_mpi_free( &conf->dhm_G );
9998#endif
9999
10000#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10001 if( conf->psk != NULL )
10002 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010003 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010004 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010005 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010006 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010007 }
10008
10009 if( conf->psk_identity != NULL )
10010 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010011 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010012 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010013 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010014 conf->psk_identity_len = 0;
10015 }
10016#endif
10017
10018#if defined(MBEDTLS_X509_CRT_PARSE_C)
10019 ssl_key_cert_free( conf->key_cert );
10020#endif
10021
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010022 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010023}
10024
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010025#if defined(MBEDTLS_PK_C) && \
10026 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010027/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#if defined(MBEDTLS_RSA_C)
10033 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10034 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036#if defined(MBEDTLS_ECDSA_C)
10037 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10038 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010041}
10042
Hanno Becker7e5437a2017-04-28 17:15:26 +010010043unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10044{
10045 switch( type ) {
10046 case MBEDTLS_PK_RSA:
10047 return( MBEDTLS_SSL_SIG_RSA );
10048 case MBEDTLS_PK_ECDSA:
10049 case MBEDTLS_PK_ECKEY:
10050 return( MBEDTLS_SSL_SIG_ECDSA );
10051 default:
10052 return( MBEDTLS_SSL_SIG_ANON );
10053 }
10054}
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010057{
10058 switch( sig )
10059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060#if defined(MBEDTLS_RSA_C)
10061 case MBEDTLS_SSL_SIG_RSA:
10062 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064#if defined(MBEDTLS_ECDSA_C)
10065 case MBEDTLS_SSL_SIG_ECDSA:
10066 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010067#endif
10068 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010070 }
10071}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010072#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010073
Hanno Becker7e5437a2017-04-28 17:15:26 +010010074#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10075 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10076
10077/* Find an entry in a signature-hash set matching a given hash algorithm. */
10078mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10079 mbedtls_pk_type_t sig_alg )
10080{
10081 switch( sig_alg )
10082 {
10083 case MBEDTLS_PK_RSA:
10084 return( set->rsa );
10085 case MBEDTLS_PK_ECDSA:
10086 return( set->ecdsa );
10087 default:
10088 return( MBEDTLS_MD_NONE );
10089 }
10090}
10091
10092/* Add a signature-hash-pair to a signature-hash set */
10093void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10094 mbedtls_pk_type_t sig_alg,
10095 mbedtls_md_type_t md_alg )
10096{
10097 switch( sig_alg )
10098 {
10099 case MBEDTLS_PK_RSA:
10100 if( set->rsa == MBEDTLS_MD_NONE )
10101 set->rsa = md_alg;
10102 break;
10103
10104 case MBEDTLS_PK_ECDSA:
10105 if( set->ecdsa == MBEDTLS_MD_NONE )
10106 set->ecdsa = md_alg;
10107 break;
10108
10109 default:
10110 break;
10111 }
10112}
10113
10114/* Allow exactly one hash algorithm for each signature. */
10115void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10116 mbedtls_md_type_t md_alg )
10117{
10118 set->rsa = md_alg;
10119 set->ecdsa = md_alg;
10120}
10121
10122#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10123 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10124
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010125/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010126 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010129{
10130 switch( hash )
10131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132#if defined(MBEDTLS_MD5_C)
10133 case MBEDTLS_SSL_HASH_MD5:
10134 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#if defined(MBEDTLS_SHA1_C)
10137 case MBEDTLS_SSL_HASH_SHA1:
10138 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140#if defined(MBEDTLS_SHA256_C)
10141 case MBEDTLS_SSL_HASH_SHA224:
10142 return( MBEDTLS_MD_SHA224 );
10143 case MBEDTLS_SSL_HASH_SHA256:
10144 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146#if defined(MBEDTLS_SHA512_C)
10147 case MBEDTLS_SSL_HASH_SHA384:
10148 return( MBEDTLS_MD_SHA384 );
10149 case MBEDTLS_SSL_HASH_SHA512:
10150 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010151#endif
10152 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010154 }
10155}
10156
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010157/*
10158 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10159 */
10160unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10161{
10162 switch( md )
10163 {
10164#if defined(MBEDTLS_MD5_C)
10165 case MBEDTLS_MD_MD5:
10166 return( MBEDTLS_SSL_HASH_MD5 );
10167#endif
10168#if defined(MBEDTLS_SHA1_C)
10169 case MBEDTLS_MD_SHA1:
10170 return( MBEDTLS_SSL_HASH_SHA1 );
10171#endif
10172#if defined(MBEDTLS_SHA256_C)
10173 case MBEDTLS_MD_SHA224:
10174 return( MBEDTLS_SSL_HASH_SHA224 );
10175 case MBEDTLS_MD_SHA256:
10176 return( MBEDTLS_SSL_HASH_SHA256 );
10177#endif
10178#if defined(MBEDTLS_SHA512_C)
10179 case MBEDTLS_MD_SHA384:
10180 return( MBEDTLS_SSL_HASH_SHA384 );
10181 case MBEDTLS_MD_SHA512:
10182 return( MBEDTLS_SSL_HASH_SHA512 );
10183#endif
10184 default:
10185 return( MBEDTLS_SSL_HASH_NONE );
10186 }
10187}
10188
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010189#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010190/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010191 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010192 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010193 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010194int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010197
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010198 if( ssl->conf->curve_list == NULL )
10199 return( -1 );
10200
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010201 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010202 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010203 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010204
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010205 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010206}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010207#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010208
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010209#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010210/*
10211 * Check if a hash proposed by the peer is in our list.
10212 * Return 0 if we're willing to use it, -1 otherwise.
10213 */
10214int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10215 mbedtls_md_type_t md )
10216{
10217 const int *cur;
10218
10219 if( ssl->conf->sig_hashes == NULL )
10220 return( -1 );
10221
10222 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10223 if( *cur == (int) md )
10224 return( 0 );
10225
10226 return( -1 );
10227}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010228#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#if defined(MBEDTLS_X509_CRT_PARSE_C)
10231int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10232 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010233 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010234 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010235{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010236 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010238 int usage = 0;
10239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010241 const char *ext_oid;
10242 size_t ext_len;
10243#endif
10244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10246 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010247 ((void) cert);
10248 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010249 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010250#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10253 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010254 {
10255 /* Server part of the key exchange */
10256 switch( ciphersuite->key_exchange )
10257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258 case MBEDTLS_KEY_EXCHANGE_RSA:
10259 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010260 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010261 break;
10262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10264 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10265 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10266 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010267 break;
10268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10270 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010271 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010272 break;
10273
10274 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 case MBEDTLS_KEY_EXCHANGE_NONE:
10276 case MBEDTLS_KEY_EXCHANGE_PSK:
10277 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10278 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010279 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010280 usage = 0;
10281 }
10282 }
10283 else
10284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10286 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010287 }
10288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010290 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010291 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010292 ret = -1;
10293 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010294#else
10295 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10299 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010301 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10302 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010303 }
10304 else
10305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10307 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010308 }
10309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010311 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010312 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010313 ret = -1;
10314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010316
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010317 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010320
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010321/*
10322 * Convert version numbers to/from wire format
10323 * and, for DTLS, to/from TLS equivalent.
10324 *
10325 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010326 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010327 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10328 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010331 unsigned char ver[2] )
10332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333#if defined(MBEDTLS_SSL_PROTO_DTLS)
10334 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010337 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10338
10339 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10340 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10341 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010342 else
10343#else
10344 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010345#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010346 {
10347 ver[0] = (unsigned char) major;
10348 ver[1] = (unsigned char) minor;
10349 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010350}
10351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010353 const unsigned char ver[2] )
10354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_SSL_PROTO_DTLS)
10356 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010357 {
10358 *major = 255 - ver[0] + 2;
10359 *minor = 255 - ver[1] + 1;
10360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010362 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10363 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010364 else
10365#else
10366 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010367#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010368 {
10369 *major = ver[0];
10370 *minor = ver[1];
10371 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010372}
10373
Simon Butcher99000142016-10-13 17:21:01 +010010374int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10375{
10376#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10377 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10378 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10379
10380 switch( md )
10381 {
10382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10383#if defined(MBEDTLS_MD5_C)
10384 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010385 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010386#endif
10387#if defined(MBEDTLS_SHA1_C)
10388 case MBEDTLS_SSL_HASH_SHA1:
10389 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10390 break;
10391#endif
10392#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10393#if defined(MBEDTLS_SHA512_C)
10394 case MBEDTLS_SSL_HASH_SHA384:
10395 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10396 break;
10397#endif
10398#if defined(MBEDTLS_SHA256_C)
10399 case MBEDTLS_SSL_HASH_SHA256:
10400 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10401 break;
10402#endif
10403 default:
10404 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10405 }
10406
10407 return 0;
10408#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10409 (void) ssl;
10410 (void) md;
10411
10412 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10413#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10414}
10415
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010416#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10417 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10418int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10419 unsigned char *output,
10420 unsigned char *data, size_t data_len )
10421{
10422 int ret = 0;
10423 mbedtls_md5_context mbedtls_md5;
10424 mbedtls_sha1_context mbedtls_sha1;
10425
10426 mbedtls_md5_init( &mbedtls_md5 );
10427 mbedtls_sha1_init( &mbedtls_sha1 );
10428
10429 /*
10430 * digitally-signed struct {
10431 * opaque md5_hash[16];
10432 * opaque sha_hash[20];
10433 * };
10434 *
10435 * md5_hash
10436 * MD5(ClientHello.random + ServerHello.random
10437 * + ServerParams);
10438 * sha_hash
10439 * SHA(ClientHello.random + ServerHello.random
10440 * + ServerParams);
10441 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010442 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010443 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010445 goto exit;
10446 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010447 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010448 ssl->handshake->randbytes, 64 ) ) != 0 )
10449 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010451 goto exit;
10452 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010453 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010454 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010456 goto exit;
10457 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010458 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010459 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010461 goto exit;
10462 }
10463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010464 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010465 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010467 goto exit;
10468 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010469 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010470 ssl->handshake->randbytes, 64 ) ) != 0 )
10471 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010473 goto exit;
10474 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010475 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010476 data_len ) ) != 0 )
10477 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010479 goto exit;
10480 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010481 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010482 output + 16 ) ) != 0 )
10483 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010485 goto exit;
10486 }
10487
10488exit:
10489 mbedtls_md5_free( &mbedtls_md5 );
10490 mbedtls_sha1_free( &mbedtls_sha1 );
10491
10492 if( ret != 0 )
10493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10494 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10495
10496 return( ret );
10497
10498}
10499#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10500 MBEDTLS_SSL_PROTO_TLS1_1 */
10501
10502#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10503 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010504
10505#if defined(MBEDTLS_USE_PSA_CRYPTO)
10506int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10507 unsigned char *hash, size_t *hashlen,
10508 unsigned char *data, size_t data_len,
10509 mbedtls_md_type_t md_alg )
10510{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010511 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010512 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010513 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10514
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010516
10517 if( ( status = psa_hash_setup( &hash_operation,
10518 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010519 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010520 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010521 goto exit;
10522 }
10523
Andrzej Kurek814feff2019-01-14 04:35:19 -050010524 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10525 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010526 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010527 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010528 goto exit;
10529 }
10530
Andrzej Kurek814feff2019-01-14 04:35:19 -050010531 if( ( status = psa_hash_update( &hash_operation,
10532 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010533 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010534 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010535 goto exit;
10536 }
10537
Andrzej Kurek814feff2019-01-14 04:35:19 -050010538 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10539 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010540 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010541 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010542 goto exit;
10543 }
10544
10545exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010546 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010547 {
10548 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10549 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010550 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010551 {
10552 case PSA_ERROR_NOT_SUPPORTED:
10553 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010554 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010555 case PSA_ERROR_BUFFER_TOO_SMALL:
10556 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10557 case PSA_ERROR_INSUFFICIENT_MEMORY:
10558 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10559 default:
10560 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10561 }
10562 }
10563 return( 0 );
10564}
10565
10566#else
10567
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010568int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010569 unsigned char *hash, size_t *hashlen,
10570 unsigned char *data, size_t data_len,
10571 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010572{
10573 int ret = 0;
10574 mbedtls_md_context_t ctx;
10575 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010576 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010577
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010579
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010580 mbedtls_md_init( &ctx );
10581
10582 /*
10583 * digitally-signed struct {
10584 * opaque client_random[32];
10585 * opaque server_random[32];
10586 * ServerDHParams params;
10587 * };
10588 */
10589 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10590 {
10591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10592 goto exit;
10593 }
10594 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10595 {
10596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10597 goto exit;
10598 }
10599 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10600 {
10601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10602 goto exit;
10603 }
10604 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10605 {
10606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10607 goto exit;
10608 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010609 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010610 {
10611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10612 goto exit;
10613 }
10614
10615exit:
10616 mbedtls_md_free( &ctx );
10617
10618 if( ret != 0 )
10619 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10620 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10621
10622 return( ret );
10623}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010624#endif /* MBEDTLS_USE_PSA_CRYPTO */
10625
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010626#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10627 MBEDTLS_SSL_PROTO_TLS1_2 */
10628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629#endif /* MBEDTLS_SSL_TLS_C */