blob: 3064b524914d945a915f3bae9f1b38472ef88f6a [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 Beckerf704bef2018-11-16 15:21:18 +0000741 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 const mbedtls_cipher_info_t *cipher_info;
743 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100744
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000745 /* cf. RFC 5246, Section 8.1:
746 * "The master secret is always exactly 48 bytes in length." */
747 size_t const master_secret_len = 48;
748
Hanno Becker35b23c72018-10-23 12:10:41 +0100749#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
750 unsigned char session_hash[48];
751#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_ssl_session *session = ssl->session_negotiate;
754 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
755 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100760 if( cipher_info == NULL )
761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100763 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100765 }
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100768 if( md_info == NULL )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100771 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100773 }
774
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000776 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_PROTO_SSL3)
779 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780 {
Paul Bakker48916f92012-09-16 19:57:18 +0000781 handshake->tls_prf = ssl3_prf;
782 handshake->calc_verify = ssl_calc_verify_ssl;
783 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000784 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785 else
786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
788 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 {
Paul Bakker48916f92012-09-16 19:57:18 +0000790 handshake->tls_prf = tls1_prf;
791 handshake->calc_verify = ssl_calc_verify_tls;
792 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794 else
795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
797#if defined(MBEDTLS_SHA512_C)
798 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
799 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 {
Paul Bakker48916f92012-09-16 19:57:18 +0000801 handshake->tls_prf = tls_prf_sha384;
802 handshake->calc_verify = ssl_calc_verify_tls_sha384;
803 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000804 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SHA256_C)
808 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000809 {
Paul Bakker48916f92012-09-16 19:57:18 +0000810 handshake->tls_prf = tls_prf_sha256;
811 handshake->calc_verify = ssl_calc_verify_tls_sha256;
812 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000813 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200814 else
815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200820 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
822 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 * SSLv3:
824 * master =
825 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
826 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
827 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200828 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200829 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 * master = PRF( premaster, "master secret", randbytes )[0..47]
831 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100832 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
835 }
836 else
837 {
838 /* The label for the KDF used for key expansion.
839 * This is either "master secret" or "extended master secret"
840 * depending on whether the Extended Master Secret extension
841 * is used. */
842 char const *lbl = "master secret";
843
844 /* The salt for the KDF used for key expansion.
845 * - If the Extended Master Secret extension is not used,
846 * this is ClientHello.Random + ServerHello.Random
847 * (see Sect. 8.1 in RFC 5246).
848 * - If the Extended Master Secret extension is used,
849 * this is the transcript of the handshake so far.
850 * (see Sect. 4 in RFC 7627). */
851 unsigned char const *salt = handshake->randbytes;
852 size_t salt_len = 64;
853
854#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
855 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
856 ssl->transform_negotiate->ciphersuite_info;
857 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
858#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
861 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200864
Hanno Becker35b23c72018-10-23 12:10:41 +0100865 lbl = "extended master secret";
866 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200867 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100872 if( md_type == MBEDTLS_MD_SHA384 )
873 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200874 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100875#endif /* MBEDTLS_SHA512_C */
876 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200877 }
878 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100880 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200881
Hanno Becker35b23c72018-10-23 12:10:41 +0100882 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200883 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100884#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
885
Hanno Becker7d0a5692018-10-23 15:26:22 +0100886#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
887 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
888 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
889 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
890 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100891 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100892 /* Perform PSK-to-MS expansion in a single step. */
893 psa_status_t status;
894 psa_algorithm_t alg;
895 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500896 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100897
898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
899
900 psk = ssl->conf->psk_opaque;
901 if( ssl->handshake->psk_opaque != 0 )
902 psk = ssl->handshake->psk_opaque;
903
904 if( md_type == MBEDTLS_MD_SHA384 )
905 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
906 else
907 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
908
909 status = psa_key_derivation( &generator, psk, alg,
910 salt, salt_len,
911 (unsigned char const *) lbl,
912 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000913 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100914 if( status != PSA_SUCCESS )
915 {
916 psa_generator_abort( &generator );
917 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
918 }
919
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000920 status = psa_generator_read( &generator, session->master,
921 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100922 if( status != PSA_SUCCESS )
923 {
924 psa_generator_abort( &generator );
925 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
926 }
927
928 status = psa_generator_abort( &generator );
929 if( status != PSA_SUCCESS )
930 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100931 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100932 else
933#endif
934 {
935 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
936 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000937 session->master,
938 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100939 if( ret != 0 )
940 {
941 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
942 return( ret );
943 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200944
Hanno Becker7d0a5692018-10-23 15:26:22 +0100945 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
946 handshake->premaster,
947 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100948
Hanno Becker7d0a5692018-10-23 15:26:22 +0100949 mbedtls_platform_zeroize( handshake->premaster,
950 sizeof(handshake->premaster) );
951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
954 /*
955 * Swap the client and server random values.
956 */
Paul Bakker48916f92012-09-16 19:57:18 +0000957 memcpy( tmp, handshake->randbytes, 64 );
958 memcpy( handshake->randbytes, tmp + 32, 32 );
959 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500960 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
962 /*
963 * SSLv3:
964 * key block =
965 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
966 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
967 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
968 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
969 * ...
970 *
971 * TLSv1:
972 * key block = PRF( master, "key expansion", randbytes )
973 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100974 ret = handshake->tls_prf( session->master, 48, "key expansion",
975 handshake->randbytes, 64, keyblk, 256 );
976 if( ret != 0 )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100979 return( ret );
980 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
983 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
984 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
985 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
986 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500988 mbedtls_platform_zeroize( handshake->randbytes,
989 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991 /*
992 * Determine the appropriate key, IV and MAC length.
993 */
Paul Bakker68884e32013-01-07 18:20:04 +0100994
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200995 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200998 cipher_info->mode == MBEDTLS_MODE_CCM ||
999 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001001 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001002
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001003 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001004 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001005
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001006 /* All modes haves 96-bit IVs;
1007 * GCM and CCM has 4 implicit and 8 explicit bytes
1008 * ChachaPoly has all 12 bytes implicit
1009 */
Paul Bakker68884e32013-01-07 18:20:04 +01001010 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001011 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1012 transform->fixed_ivlen = 12;
1013 else
1014 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001015
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001016 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1017 taglen = transform->ciphersuite_info->flags &
1018 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1019
1020
1021 /* Minimum length of encrypted record */
1022 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1023 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025 else
1026 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001027 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1029 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001032 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001035 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001036 mac_key_len = mbedtls_md_get_size( md_info );
1037 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001040 /*
1041 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1042 * (rfc 6066 page 13 or rfc 2104 section 4),
1043 * so we only need to adjust the length here.
1044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001048
1049#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1050 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001051 * HMAC implementation which also truncates the key
1052 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001053 mac_key_len = transform->maclen;
1054#endif
1055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001057
1058 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001059 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001061 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001063 transform->minlen = transform->maclen;
1064 else
Paul Bakker68884e32013-01-07 18:20:04 +01001065 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001066 /*
1067 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001068 * 1. if EtM is in use: one block plus MAC
1069 * otherwise: * first multiple of blocklen greater than maclen
1070 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1073 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001074 {
1075 transform->minlen = transform->maclen
1076 + cipher_info->block_size;
1077 }
1078 else
1079#endif
1080 {
1081 transform->minlen = transform->maclen
1082 + cipher_info->block_size
1083 - transform->maclen % cipher_info->block_size;
1084 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1087 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1088 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001089 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001090 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1093 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1094 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001095 {
1096 transform->minlen += transform->ivlen;
1097 }
1098 else
1099#endif
1100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001103 }
Paul Bakker68884e32013-01-07 18:20:04 +01001104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 }
1106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001108 transform->keylen, transform->minlen, transform->ivlen,
1109 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111 /*
1112 * Finally setup the cipher contexts, IVs and MAC secrets.
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001117 key1 = keyblk + mac_key_len * 2;
1118 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Paul Bakker68884e32013-01-07 18:20:04 +01001120 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001121 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001123 /*
1124 * This is not used in TLS v1.1.
1125 */
Paul Bakker48916f92012-09-16 19:57:18 +00001126 iv_copy_len = ( transform->fixed_ivlen ) ?
1127 transform->fixed_ivlen : transform->ivlen;
1128 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1129 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 }
1132 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#endif /* MBEDTLS_SSL_CLI_C */
1134#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001135 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001137 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1138 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Hanno Becker81c7b182017-11-09 18:39:33 +00001140 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001141 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001143 /*
1144 * This is not used in TLS v1.1.
1145 */
Paul Bakker48916f92012-09-16 19:57:18 +00001146 iv_copy_len = ( transform->fixed_ivlen ) ?
1147 transform->fixed_ivlen : transform->ivlen;
1148 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1149 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_SSL_PROTO_SSL3)
1160 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001161 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001162 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001166 }
1167
Hanno Becker81c7b182017-11-09 18:39:33 +00001168 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1169 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001170 }
1171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1173#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1174 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1175 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001176 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001177 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1178 For AEAD-based ciphersuites, there is nothing to do here. */
1179 if( mac_key_len != 0 )
1180 {
1181 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1182 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1183 }
Paul Bakker68884e32013-01-07 18:20:04 +01001184 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001185 else
1186#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1189 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001190 }
Paul Bakker68884e32013-01-07 18:20:04 +01001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1193 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001194 {
1195 int ret = 0;
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001200 transform->iv_enc, transform->iv_dec,
1201 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001202 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001203 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1206 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001207 }
1208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001210
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001211#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1212 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001213 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001214 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1215 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001216 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001217 iv_copy_len );
1218 }
1219#endif
1220
Hanno Beckerf704bef2018-11-16 15:21:18 +00001221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001222
1223 /* Only use PSA-based ciphers for TLS-1.2.
1224 * That's relevant at least for TLS-1.0, where
1225 * we assume that mbedtls_cipher_crypt() updates
1226 * the structure field for the IV, which the PSA-based
1227 * implementation currently doesn't. */
1228#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1229 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001230 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001231 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1232 cipher_info, taglen );
1233 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1234 {
1235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1236 return( ret );
1237 }
1238
1239 if( ret == 0 )
1240 {
1241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1242 psa_fallthrough = 0;
1243 }
1244 else
1245 {
1246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1247 psa_fallthrough = 1;
1248 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001250 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001251 psa_fallthrough = 1;
1252#else
1253 psa_fallthrough = 1;
1254#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001255
Hanno Beckercb1cc802018-11-17 22:27:38 +00001256 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001257#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001258 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001259 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001262 return( ret );
1263 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001264
Hanno Beckerf704bef2018-11-16 15:21:18 +00001265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001266 /* Only use PSA-based ciphers for TLS-1.2.
1267 * That's relevant at least for TLS-1.0, where
1268 * we assume that mbedtls_cipher_crypt() updates
1269 * the structure field for the IV, which the PSA-based
1270 * implementation currently doesn't. */
1271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001273 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001274 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1275 cipher_info, taglen );
1276 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1277 {
1278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1279 return( ret );
1280 }
1281
1282 if( ret == 0 )
1283 {
1284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1285 psa_fallthrough = 0;
1286 }
1287 else
1288 {
1289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1290 psa_fallthrough = 1;
1291 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001292 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001293 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001294 psa_fallthrough = 1;
1295#else
1296 psa_fallthrough = 1;
1297#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001298
Hanno Beckercb1cc802018-11-17 22:27:38 +00001299 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001300#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001301 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001302 cipher_info ) ) != 0 )
1303 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001305 return( ret );
1306 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001309 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001313 return( ret );
1314 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001317 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001321 return( ret );
1322 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_CIPHER_MODE_CBC)
1325 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1328 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001331 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001332 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1335 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001338 return( ret );
1339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001343 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001346 // Initialize compression
1347 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001349 {
Paul Bakker16770332013-10-11 09:59:44 +02001350 if( ssl->compress_buf == NULL )
1351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001353 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001354 if( ssl->compress_buf == NULL )
1355 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001357 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001358 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001359 }
1360 }
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001363
Paul Bakker48916f92012-09-16 19:57:18 +00001364 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1365 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001366
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001367 if( deflateInit( &transform->ctx_deflate,
1368 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001369 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1372 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001373 }
1374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
1379 return( 0 );
1380}
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_PROTO_SSL3)
1383void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001385 mbedtls_md5_context md5;
1386 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char pad_1[48];
1388 unsigned char pad_2[48];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001392 mbedtls_md5_init( &md5 );
1393 mbedtls_sha1_init( &sha1 );
1394
1395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker380da532012-04-18 16:10:25 +00001398 memset( pad_1, 0x36, 48 );
1399 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_md5_starts_ret( &md5 );
1406 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1407 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1408 mbedtls_md5_update_ret( &md5, hash, 16 );
1409 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001415 mbedtls_sha1_starts_ret( &sha1 );
1416 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1417 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1418 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1419 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001423
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001424 mbedtls_md5_free( &md5 );
1425 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001426
Paul Bakker380da532012-04-18 16:10:25 +00001427 return;
1428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1432void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001433{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001434 mbedtls_md5_context md5;
1435 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001438
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001439 mbedtls_md5_init( &md5 );
1440 mbedtls_sha1_init( &sha1 );
1441
1442 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1443 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001444
Andrzej Kurekeb342242019-01-29 09:14:33 -05001445 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001446 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001450
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001451 mbedtls_md5_free( &md5 );
1452 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001453
Paul Bakker380da532012-04-18 16:10:25 +00001454 return;
1455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1459#if defined(MBEDTLS_SHA256_C)
1460void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001461{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001462#if defined(MBEDTLS_USE_PSA_CRYPTO)
1463 size_t hash_size;
1464 psa_status_t status;
1465 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1466
1467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1468 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1469 if( status != PSA_SUCCESS )
1470 {
1471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1472 return;
1473 }
1474
1475 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1476 if( status != PSA_SUCCESS )
1477 {
1478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1479 return;
1480 }
1481 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1483#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001484 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001485
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001486 mbedtls_sha256_init( &sha256 );
1487
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001490 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001491 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001497#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001498 return;
1499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SHA512_C)
1503void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001504{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001505#if defined(MBEDTLS_USE_PSA_CRYPTO)
1506 size_t hash_size;
1507 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001508 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001509
1510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001511 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001512 if( status != PSA_SUCCESS )
1513 {
1514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1515 return;
1516 }
1517
Andrzej Kurek972fba52019-01-30 03:29:12 -05001518 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001519 if( status != PSA_SUCCESS )
1520 {
1521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1522 return;
1523 }
1524 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1526#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001527 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001528
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001529 mbedtls_sha512_init( &sha512 );
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001532
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001533 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001534 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001539 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001540#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 return;
1542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_SHA512_C */
1544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1547int 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 +02001548{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001549 unsigned char *p = ssl->handshake->premaster;
1550 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001551 const unsigned char *psk = ssl->conf->psk;
1552 size_t psk_len = ssl->conf->psk_len;
1553
1554 /* If the psk callback was called, use its result */
1555 if( ssl->handshake->psk != NULL )
1556 {
1557 psk = ssl->handshake->psk;
1558 psk_len = ssl->handshake->psk_len;
1559 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001560
1561 /*
1562 * PMS = struct {
1563 * opaque other_secret<0..2^16-1>;
1564 * opaque psk<0..2^16-1>;
1565 * };
1566 * with "other_secret" depending on the particular key exchange
1567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1569 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001570 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001571 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001574 *(p++) = (unsigned char)( psk_len >> 8 );
1575 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001576
1577 if( end < p || (size_t)( end - p ) < psk_len )
1578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1579
1580 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001581 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 }
1583 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1585#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1586 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001587 {
1588 /*
1589 * other_secret already set by the ClientKeyExchange message,
1590 * and is 48 bytes long
1591 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001592 if( end - p < 2 )
1593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1594
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001595 *p++ = 0;
1596 *p++ = 48;
1597 p += 48;
1598 }
1599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1601#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1602 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001604 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001605 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001606
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001607 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001609 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001610 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 return( ret );
1614 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001615 *(p++) = (unsigned char)( len >> 8 );
1616 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 p += len;
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001620 }
1621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1623#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1624 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001626 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627 size_t zlen;
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001630 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001631 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 return( ret );
1635 }
1636
1637 *(p++) = (unsigned char)( zlen >> 8 );
1638 *(p++) = (unsigned char)( zlen );
1639 p += zlen;
1640
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001641 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1642 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001643 }
1644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1648 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001649 }
1650
1651 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001652 if( end - p < 2 )
1653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001654
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001655 *(p++) = (unsigned char)( psk_len >> 8 );
1656 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001657
1658 if( end < p || (size_t)( end - p ) < psk_len )
1659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1660
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001661 memcpy( p, psk, psk_len );
1662 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001663
1664 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1665
1666 return( 0 );
1667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001671/*
1672 * SSLv3.0 MAC functions
1673 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001674#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001675static void ssl_mac( mbedtls_md_context_t *md_ctx,
1676 const unsigned char *secret,
1677 const unsigned char *buf, size_t len,
1678 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001679 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001680{
1681 unsigned char header[11];
1682 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001683 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1685 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001686
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001687 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001689 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001690 else
Paul Bakker68884e32013-01-07 18:20:04 +01001691 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
1693 memcpy( header, ctr, 8 );
1694 header[ 8] = (unsigned char) type;
1695 header[ 9] = (unsigned char)( len >> 8 );
1696 header[10] = (unsigned char)( len );
1697
Paul Bakker68884e32013-01-07 18:20:04 +01001698 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_md_starts( md_ctx );
1700 mbedtls_md_update( md_ctx, secret, md_size );
1701 mbedtls_md_update( md_ctx, padding, padlen );
1702 mbedtls_md_update( md_ctx, header, 11 );
1703 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001704 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Paul Bakker68884e32013-01-07 18:20:04 +01001706 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 mbedtls_md_starts( md_ctx );
1708 mbedtls_md_update( md_ctx, secret, md_size );
1709 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001710 mbedtls_md_update( md_ctx, out, md_size );
1711 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1716 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001717 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001718#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001719#endif
1720
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001721/* The function below is only used in the Lucky 13 counter-measure in
1722 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1723#if defined(SSL_SOME_MODES_USE_MAC) && \
1724 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1725 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1726 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1727/* This function makes sure every byte in the memory region is accessed
1728 * (in ascending addresses order) */
1729static void ssl_read_memory( unsigned char *p, size_t len )
1730{
1731 unsigned char acc = 0;
1732 volatile unsigned char force;
1733
1734 for( ; len != 0; p++, len-- )
1735 acc ^= *p;
1736
1737 force = acc;
1738 (void) force;
1739}
1740#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1741
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001742/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001748 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001756 }
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001761 ssl->out_msg, ssl->out_msglen );
1762
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001764 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001766#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 if( mode == MBEDTLS_MODE_STREAM ||
1768 ( mode == MBEDTLS_MODE_CBC
1769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1770 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001771#endif
1772 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#if defined(MBEDTLS_SSL_PROTO_SSL3)
1775 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001776 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001777 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001778
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001779 ssl_mac( &ssl->transform_out->md_ctx_enc,
1780 ssl->transform_out->mac_enc,
1781 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001782 ssl->out_ctr, ssl->out_msgtype,
1783 mac );
1784
1785 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 }
1787 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1790 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1791 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001792 {
Hanno Becker992b6872017-11-09 18:57:39 +00001793 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1796 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1797 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1798 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001799 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001800 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001802
1803 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001804 }
1805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001806#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001810 }
1811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001813 ssl->out_msg + ssl->out_msglen,
1814 ssl->transform_out->maclen );
1815
1816 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001817 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001818 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001819#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001821 /*
1822 * Encrypt
1823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1825 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001827 int ret;
1828 size_t olen = 0;
1829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 "including %d bytes of padding",
1832 ssl->out_msglen, 0 ) );
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001835 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001836 ssl->transform_out->ivlen,
1837 ssl->out_msg, ssl->out_msglen,
1838 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001841 return( ret );
1842 }
1843
1844 if( ssl->out_msglen != olen )
1845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1847 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
Paul Bakker68884e32013-01-07 18:20:04 +01001850 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001852#if defined(MBEDTLS_GCM_C) || \
1853 defined(MBEDTLS_CCM_C) || \
1854 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001856 mode == MBEDTLS_MODE_CCM ||
1857 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001858 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001859 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001860 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001861 unsigned char *enc_msg;
1862 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 unsigned char iv[12];
1864 mbedtls_ssl_transform *transform = ssl->transform_out;
1865 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001867 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001869 /*
1870 * Prepare additional authenticated data
1871 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001872 memcpy( add_data, ssl->out_ctr, 8 );
1873 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001875 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001876 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1877 add_data[12] = ssl->out_msglen & 0xFF;
1878
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001880
Paul Bakker68884e32013-01-07 18:20:04 +01001881 /*
1882 * Generate IV
1883 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001884 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1885 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001886 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001887 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1888 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1889 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1890
1891 }
1892 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1893 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001894 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001895 unsigned char i;
1896
1897 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1898
1899 for( i = 0; i < 8; i++ )
1900 iv[i+4] ^= ssl->out_ctr[i];
1901 }
1902 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001903 {
1904 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001907 }
1908
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001909 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1910 iv, transform->ivlen );
1911 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1912 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001913
Paul Bakker68884e32013-01-07 18:20:04 +01001914 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001915 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001916 */
1917 enc_msg = ssl->out_msg;
1918 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001919 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001922 "including 0 bytes of padding",
1923 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001924
Paul Bakker68884e32013-01-07 18:20:04 +01001925 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001926 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001927 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001928 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1929 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001930 add_data, 13,
1931 enc_msg, enc_msglen,
1932 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001933 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001936 return( ret );
1937 }
1938
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001939 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001943 }
1944
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001945 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001946 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1952#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001953 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001956 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001957 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001958 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001959
Paul Bakker48916f92012-09-16 19:57:18 +00001960 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1961 ssl->transform_out->ivlen;
1962 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 padlen = 0;
1964
1965 for( i = 0; i <= padlen; i++ )
1966 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1967
1968 ssl->out_msglen += padlen + 1;
1969
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001970 enc_msglen = ssl->out_msglen;
1971 enc_msg = ssl->out_msg;
1972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001974 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001975 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1976 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001979 {
1980 /*
1981 * Generate IV
1982 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001983 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001984 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001985 if( ret != 0 )
1986 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001987
Paul Bakker92be97b2013-01-02 17:30:03 +01001988 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001989 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001990
1991 /*
1992 * Fix pointer positions and message length with added IV
1993 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001994 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001995 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001996 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002001 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002002 ssl->out_msglen, ssl->transform_out->ivlen,
2003 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02002006 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002007 ssl->transform_out->ivlen,
2008 enc_msg, enc_msglen,
2009 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002012 return( ret );
2013 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002014
Paul Bakkercca5b812013-08-31 17:40:26 +02002015 if( enc_msglen != olen )
2016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002019 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2022 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002023 {
2024 /*
2025 * Save IV in SSL3 and TLS1
2026 */
2027 memcpy( ssl->transform_out->iv_enc,
2028 ssl->transform_out->cipher_ctx_enc.iv,
2029 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002031#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002034 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002035 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002036 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2037
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002038 /*
2039 * MAC(MAC_write_key, seq_num +
2040 * TLSCipherText.type +
2041 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002042 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002043 * IV + // except for TLS 1.0
2044 * ENC(content + padding + padding_length));
2045 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046 unsigned char pseudo_hdr[13];
2047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002049
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002050 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2051 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002052 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2053 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2058 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002059 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002060 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002062
Hanno Becker3d8c9072018-01-05 16:24:22 +00002063 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2064 ssl->transform_out->maclen );
2065
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002066 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002067 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002073 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002079 /* Make extra sure authentication was performed, exactly once */
2080 if( auth_done != 1 )
2081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002084 }
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 return( 0 );
2089}
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002094 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002095#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002096 size_t padlen = 0, correct = 1;
2097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002101 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002105 }
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002108
Paul Bakker48916f92012-09-16 19:57:18 +00002109 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002112 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
2115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2117 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002118 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002119 int ret;
2120 size_t olen = 0;
2121
Paul Bakker68884e32013-01-07 18:20:04 +01002122 padlen = 0;
2123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002125 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002126 ssl->transform_in->ivlen,
2127 ssl->in_msg, ssl->in_msglen,
2128 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002131 return( ret );
2132 }
2133
2134 if( ssl->in_msglen != olen )
2135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
Paul Bakker68884e32013-01-07 18:20:04 +01002140 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002142#if defined(MBEDTLS_GCM_C) || \
2143 defined(MBEDTLS_CCM_C) || \
2144 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002146 mode == MBEDTLS_MODE_CCM ||
2147 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002148 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002149 int ret;
2150 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002151 unsigned char *dec_msg;
2152 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002153 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002154 unsigned char iv[12];
2155 mbedtls_ssl_transform *transform = ssl->transform_in;
2156 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002158 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002159
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002160 /*
2161 * Compute and update sizes
2162 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002163 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002166 "+ taglen (%d)", ssl->in_msglen,
2167 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002169 }
2170 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2171
Paul Bakker68884e32013-01-07 18:20:04 +01002172 dec_msg = ssl->in_msg;
2173 dec_msg_result = ssl->in_msg;
2174 ssl->in_msglen = dec_msglen;
2175
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002176 /*
2177 * Prepare additional authenticated data
2178 */
Paul Bakker68884e32013-01-07 18:20:04 +01002179 memcpy( add_data, ssl->in_ctr, 8 );
2180 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002182 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002183 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2184 add_data[12] = ssl->in_msglen & 0xFF;
2185
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002186 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002187
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002188 /*
2189 * Prepare IV
2190 */
2191 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2192 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002193 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002194 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2195 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002196
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002197 }
2198 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2199 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002200 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002201 unsigned char i;
2202
2203 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2204
2205 for( i = 0; i < 8; i++ )
2206 iv[i+4] ^= ssl->in_ctr[i];
2207 }
2208 else
2209 {
2210 /* Reminder if we ever add an AEAD mode with a different size */
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2213 }
2214
2215 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002217
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002218 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002219 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002222 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002223 add_data, 13,
2224 dec_msg, dec_msglen,
2225 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002226 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2231 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002232
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002233 return( ret );
2234 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002235 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002236
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002237 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002241 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2245#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002246 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
Paul Bakker45829992013-01-03 14:52:21 +01002249 /*
2250 * Decrypt and check the padding
2251 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002252 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002253 unsigned char *dec_msg;
2254 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002255 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002256 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002257 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002258
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 /*
Paul Bakker45829992013-01-03 14:52:21 +01002260 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2263 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002264 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002265#endif
Paul Bakker45829992013-01-03 14:52:21 +01002266
2267 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2268 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002271 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2272 ssl->transform_in->ivlen,
2273 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002275 }
2276
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002277 dec_msglen = ssl->in_msglen;
2278 dec_msg = ssl->in_msg;
2279 dec_msg_result = ssl->in_msg;
2280
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002281 /*
2282 * Authenticate before decrypt if enabled
2283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2285 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002286 {
Hanno Becker992b6872017-11-09 18:57:39 +00002287 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002288 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002291
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002292 dec_msglen -= ssl->transform_in->maclen;
2293 ssl->in_msglen -= ssl->transform_in->maclen;
2294
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002295 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2296 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2297 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2298 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2303 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002304 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002305 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002310 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311 ssl->transform_in->maclen );
2312
Hanno Becker992b6872017-11-09 18:57:39 +00002313 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2314 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002319 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002320 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002323
2324 /*
2325 * Check length sanity
2326 */
2327 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002330 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002332 }
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002335 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002336 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002339 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002341 dec_msglen -= ssl->transform_in->ivlen;
2342 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002343
Paul Bakker48916f92012-09-16 19:57:18 +00002344 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002345 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002350 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002351 ssl->transform_in->ivlen,
2352 dec_msg, dec_msglen,
2353 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002356 return( ret );
2357 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002358
Paul Bakkercca5b812013-08-31 17:40:26 +02002359 if( dec_msglen != olen )
2360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002363 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2366 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002367 {
2368 /*
2369 * Save IV in SSL3 and TLS1
2370 */
2371 memcpy( ssl->transform_in->iv_dec,
2372 ssl->transform_in->cipher_ctx_dec.iv,
2373 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002378
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002379 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002380 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_DEBUG_ALL)
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002384 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002385#endif
Paul Bakker45829992013-01-03 14:52:21 +01002386 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002387 correct = 0;
2388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_PROTO_SSL3)
2391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 {
Paul Bakker48916f92012-09-16 19:57:18 +00002393 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_SSL_DEBUG_ALL)
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002398 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002399#endif
Paul Bakker45829992013-01-03 14:52:21 +01002400 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402 }
2403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2405#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2406 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2407 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 /*
Paul Bakker45829992013-01-03 14:52:21 +01002410 * TLSv1+: always check the padding up to the first failure
2411 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002413 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002414 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002415 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002416
Paul Bakker956c9e02013-12-19 14:42:28 +01002417 /*
2418 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002419 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002420 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002421 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002422 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002423 *
2424 * In both cases we reset padding_idx to a safe value (0) to
2425 * prevent out-of-buffer reads.
2426 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002427 correct &= ( padlen <= ssl->in_msglen );
2428 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002429 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002430
2431 padding_idx *= correct;
2432
Angus Grattonb512bc12018-06-19 15:57:50 +10002433 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002434 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002435 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002436 pad_count += real_count *
2437 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2438 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002439
2440 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002443 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002445#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002446 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2450 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002454 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002455
2456 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002460 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002466#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
2471 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472 * Authenticate if not done yet.
2473 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002475#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002476 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002477 {
Hanno Becker992b6872017-11-09 18:57:39 +00002478 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002479
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002480 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002482 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2483 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#if defined(MBEDTLS_SSL_PROTO_SSL3)
2486 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002487 {
2488 ssl_mac( &ssl->transform_in->md_ctx_dec,
2489 ssl->transform_in->mac_dec,
2490 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002491 ssl->in_ctr, ssl->in_msgtype,
2492 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002493 }
2494 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2496#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2497 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2498 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002499 {
2500 /*
2501 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002502 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002503 *
2504 * Known timing attacks:
2505 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2506 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002507 * To compensate for different timings for the MAC calculation
2508 * depending on how much padding was removed (which is determined
2509 * by padlen), process extra_run more blocks through the hash
2510 * function.
2511 *
2512 * The formula in the paper is
2513 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2514 * where L1 is the size of the header plus the decrypted message
2515 * plus CBC padding and L2 is the size of the header plus the
2516 * decrypted message. This is for an underlying hash function
2517 * with 64-byte blocks.
2518 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2519 * correctly. We round down instead of up, so -56 is the correct
2520 * value for our calculations instead of -55.
2521 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002522 * Repeat the formula rather than defining a block_size variable.
2523 * This avoids requiring division by a variable at runtime
2524 * (which would be marginally less efficient and would require
2525 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 */
2527 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002528
2529 /*
2530 * The next two sizes are the minimum and maximum values of
2531 * in_msglen over all padlen values.
2532 *
2533 * They're independent of padlen, since we previously did
2534 * in_msglen -= padlen.
2535 *
2536 * Note that max_len + maclen is never more than the buffer
2537 * length, as we previously did in_msglen -= maclen too.
2538 */
2539 const size_t max_len = ssl->in_msglen + padlen;
2540 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2541
Gilles Peskine20b44082018-05-29 14:06:49 +02002542 switch( ssl->transform_in->ciphersuite_info->mac )
2543 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002544#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2545 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002546 case MBEDTLS_MD_MD5:
2547 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002548 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002549 /* 8 bytes of message size, 64-byte compression blocks */
2550 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2551 ( 13 + ssl->in_msglen + 8 ) / 64;
2552 break;
2553#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002554#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002555 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002556 /* 16 bytes of message size, 128-byte compression blocks */
2557 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2558 ( 13 + ssl->in_msglen + 16 ) / 128;
2559 break;
2560#endif
2561 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2564 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002565
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002566 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2569 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2570 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2571 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002572 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002573 /* Make sure we access everything even when padlen > 0. This
2574 * makes the synchronisation requirements for just-in-time
2575 * Prime+Probe attacks much tighter and hopefully impractical. */
2576 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002577 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002578
2579 /* Call mbedtls_md_process at least once due to cache attacks
2580 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002581 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002585
2586 /* Make sure we access all the memory that could contain the MAC,
2587 * before we check it in the next code block. This makes the
2588 * synchronisation requirements for just-in-time Prime+Probe
2589 * attacks much tighter and hopefully impractical. */
2590 ssl_read_memory( ssl->in_msg + min_len,
2591 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002592 }
2593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2595 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2598 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002601#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002602 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2603 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2604 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002605#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Hanno Becker992b6872017-11-09 18:57:39 +00002607 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2608 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#if defined(MBEDTLS_SSL_DEBUG_ALL)
2611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002612#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002613 correct = 0;
2614 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002615 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002616 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002617
2618 /*
2619 * Finally check the correct flag
2620 */
2621 if( correct == 0 )
2622 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002623#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002624
2625 /* Make extra sure authentication was performed, exactly once */
2626 if( auth_done != 1 )
2627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 if( ssl->in_msglen == 0 )
2633 {
Angus Gratton34817922018-06-19 15:58:22 +10002634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2635 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2636 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2637 {
2638 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2640 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2641 }
2642#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2643
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 ssl->nb_zero++;
2645
2646 /*
2647 * Three or more empty messages may be a DoS attack
2648 * (excessive CPU consumption).
2649 */
2650 if( ssl->nb_zero > 3 )
2651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656 }
2657 else
2658 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002662 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002663 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002664 }
2665 else
2666#endif
2667 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002668 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002669 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2670 if( ++ssl->in_ctr[i - 1] != 0 )
2671 break;
2672
2673 /* The loop goes to its end iff the counter is wrapping */
2674 if( i == ssl_ep_len( ssl ) )
2675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2677 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002678 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002679 }
2680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
2683 return( 0 );
2684}
2685
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002686#undef MAC_NONE
2687#undef MAC_PLAINTEXT
2688#undef MAC_CIPHERTEXT
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691/*
2692 * Compression/decompression functions
2693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002695{
2696 int ret;
2697 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002698 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002699 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002700 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002703
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002704 if( len_pre == 0 )
2705 return( 0 );
2706
Paul Bakker2770fbd2012-07-03 13:30:23 +00002707 memcpy( msg_pre, ssl->out_msg, len_pre );
2708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002710 ssl->out_msglen ) );
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713 ssl->out_msg, ssl->out_msglen );
2714
Paul Bakker48916f92012-09-16 19:57:18 +00002715 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2716 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2717 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002718 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002719
Paul Bakker48916f92012-09-16 19:57:18 +00002720 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002721 if( ret != Z_OK )
2722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2724 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002725 }
2726
Angus Grattond8213d02016-05-25 20:56:48 +10002727 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002728 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002731 ssl->out_msglen ) );
2732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734 ssl->out_msg, ssl->out_msglen );
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002737
2738 return( 0 );
2739}
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002742{
2743 int ret;
2744 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002745 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002747 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002750
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002751 if( len_pre == 0 )
2752 return( 0 );
2753
Paul Bakker2770fbd2012-07-03 13:30:23 +00002754 memcpy( msg_pre, ssl->in_msg, len_pre );
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757 ssl->in_msglen ) );
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002760 ssl->in_msg, ssl->in_msglen );
2761
Paul Bakker48916f92012-09-16 19:57:18 +00002762 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2763 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2764 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002765 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002766 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767
Paul Bakker48916f92012-09-16 19:57:18 +00002768 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769 if( ret != Z_OK )
2770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2772 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773 }
2774
Angus Grattond8213d02016-05-25 20:56:48 +10002775 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002776 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779 ssl->in_msglen ) );
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002782 ssl->in_msg, ssl->in_msglen );
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002785
2786 return( 0 );
2787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2791static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_PROTO_DTLS)
2794static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002795{
2796 /* If renegotiation is not enforced, retransmit until we would reach max
2797 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002798 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002799 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002800 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002801 unsigned char doublings = 1;
2802
2803 while( ratio != 0 )
2804 {
2805 ++doublings;
2806 ratio >>= 1;
2807 }
2808
2809 if( ++ssl->renego_records_seen > doublings )
2810 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002812 return( 0 );
2813 }
2814 }
2815
2816 return( ssl_write_hello_request( ssl ) );
2817}
2818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002820
Paul Bakker5121ce52009-01-03 21:22:43 +00002821/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002822 * Fill the input message buffer by appending data to it.
2823 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002824 *
2825 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2826 * available (from this read and/or a previous one). Otherwise, an error code
2827 * is returned (possibly EOF or WANT_READ).
2828 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002829 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2830 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2831 * since we always read a whole datagram at once.
2832 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002833 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002834 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002837{
Paul Bakker23986e52011-04-24 08:57:21 +00002838 int ret;
2839 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002843 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002846 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002848 }
2849
Angus Grattond8213d02016-05-25 20:56:48 +10002850 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002854 }
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002859 uint32_t timeout;
2860
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002861 /* Just to be sure */
2862 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2863 {
2864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2865 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2867 }
2868
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002869 /*
2870 * The point is, we need to always read a full datagram at once, so we
2871 * sometimes read more then requested, and handle the additional data.
2872 * It could be the rest of the current record (while fetching the
2873 * header) and/or some other records in the same datagram.
2874 */
2875
2876 /*
2877 * Move to the next record in the already read datagram if applicable
2878 */
2879 if( ssl->next_record_offset != 0 )
2880 {
2881 if( ssl->in_left < ssl->next_record_offset )
2882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002885 }
2886
2887 ssl->in_left -= ssl->next_record_offset;
2888
2889 if( ssl->in_left != 0 )
2890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002892 ssl->next_record_offset ) );
2893 memmove( ssl->in_hdr,
2894 ssl->in_hdr + ssl->next_record_offset,
2895 ssl->in_left );
2896 }
2897
2898 ssl->next_record_offset = 0;
2899 }
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002903
2904 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002905 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002906 */
2907 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002910 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002911 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002912
2913 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002914 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002915 * are not at the beginning of a new record, the caller did something
2916 * wrong.
2917 */
2918 if( ssl->in_left != 0 )
2919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002922 }
2923
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002924 /*
2925 * Don't even try to read if time's out already.
2926 * This avoids by-passing the timer when repeatedly receiving messages
2927 * that will end up being dropped.
2928 */
2929 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002930 {
2931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002932 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002933 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002934 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002935 {
Angus Grattond8213d02016-05-25 20:56:48 +10002936 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002939 timeout = ssl->handshake->retransmit_timeout;
2940 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002941 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002944
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002945 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002946 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2947 timeout );
2948 else
2949 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002952
2953 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002955 }
2956
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002957 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002960 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002963 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002964 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002967 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002968 }
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002973 return( ret );
2974 }
2975
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002976 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002979 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002981 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002982 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002985 return( ret );
2986 }
2987
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002988 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002991 }
2992
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 if( ret < 0 )
2994 return( ret );
2995
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002996 ssl->in_left = ret;
2997 }
2998 else
2999#endif
3000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003002 ssl->in_left, nb_want ) );
3003
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003004 while( ssl->in_left < nb_want )
3005 {
3006 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003007
3008 if( ssl_check_timer( ssl ) != 0 )
3009 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3010 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003011 {
3012 if( ssl->f_recv_timeout != NULL )
3013 {
3014 ret = ssl->f_recv_timeout( ssl->p_bio,
3015 ssl->in_hdr + ssl->in_left, len,
3016 ssl->conf->read_timeout );
3017 }
3018 else
3019 {
3020 ret = ssl->f_recv( ssl->p_bio,
3021 ssl->in_hdr + ssl->in_left, len );
3022 }
3023 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003026 ssl->in_left, nb_want ) );
3027 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003028
3029 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003031
3032 if( ret < 0 )
3033 return( ret );
3034
mohammad160352aecb92018-03-28 23:41:40 -07003035 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003036 {
Darryl Green11999bb2018-03-13 15:22:58 +00003037 MBEDTLS_SSL_DEBUG_MSG( 1,
3038 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003039 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3041 }
3042
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003043 ssl->in_left += ret;
3044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 }
3046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
3049 return( 0 );
3050}
3051
3052/*
3053 * Flush any data not yet written
3054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003057 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003058 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003062 if( ssl->f_send == NULL )
3063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003065 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003067 }
3068
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003069 /* Avoid incrementing counter if data is flushed */
3070 if( ssl->out_left == 0 )
3071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003073 return( 0 );
3074 }
3075
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 while( ssl->out_left > 0 )
3077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3079 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Hanno Becker2b1e3542018-08-06 11:19:13 +01003081 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003082 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
3086 if( ret <= 0 )
3087 return( ret );
3088
mohammad160352aecb92018-03-28 23:41:40 -07003089 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003090 {
Darryl Green11999bb2018-03-13 15:22:58 +00003091 MBEDTLS_SSL_DEBUG_MSG( 1,
3092 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003093 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3095 }
3096
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 ssl->out_left -= ret;
3098 }
3099
Hanno Becker2b1e3542018-08-06 11:19:13 +01003100#if defined(MBEDTLS_SSL_PROTO_DTLS)
3101 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003102 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003103 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003104 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003105 else
3106#endif
3107 {
3108 ssl->out_hdr = ssl->out_buf + 8;
3109 }
3110 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
3114 return( 0 );
3115}
3116
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003117/*
3118 * Functions to handle the DTLS retransmission state machine
3119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003121/*
3122 * Append current handshake message to current outgoing flight
3123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3128 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3129 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003130
3131 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003132 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003133 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003136 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003137 }
3138
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003139 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003140 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003143 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003144 }
3145
3146 /* Copy current handshake message with headers */
3147 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3148 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003149 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150 msg->next = NULL;
3151
3152 /* Append to the current flight */
3153 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003154 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155 else
3156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158 while( cur->next != NULL )
3159 cur = cur->next;
3160 cur->next = msg;
3161 }
3162
Hanno Becker3b235902018-08-06 09:54:53 +01003163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003164 return( 0 );
3165}
3166
3167/*
3168 * Free the current flight of handshake messages
3169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 mbedtls_ssl_flight_item *cur = flight;
3173 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174
3175 while( cur != NULL )
3176 {
3177 next = cur->next;
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 mbedtls_free( cur->p );
3180 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003181
3182 cur = next;
3183 }
3184}
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3187static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003188#endif
3189
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003190/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003191 * Swap transform_out and out_ctr with the alternative ones
3192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196 unsigned char tmp_out_ctr[8];
3197
3198 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003201 return;
3202 }
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003205
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003206 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003207 tmp_transform = ssl->transform_out;
3208 ssl->transform_out = ssl->handshake->alt_transform_out;
3209 ssl->handshake->alt_transform_out = tmp_transform;
3210
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003211 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003212 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3213 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003214 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003215
3216 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003217 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3220 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3225 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003226 }
3227 }
3228#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003229}
3230
3231/*
3232 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003233 */
3234int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3235{
3236 int ret = 0;
3237
3238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3239
3240 ret = mbedtls_ssl_flight_transmit( ssl );
3241
3242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3243
3244 return( ret );
3245}
3246
3247/*
3248 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003249 *
3250 * Need to remember the current message in case flush_output returns
3251 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003253 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003254int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003255{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003256 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003260 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003262
3263 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003264 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003265 ssl_swap_epochs( ssl );
3266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003268 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003269
3270 while( ssl->handshake->cur_msg != NULL )
3271 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003272 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003273 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003274
Hanno Beckere1dcb032018-08-17 16:47:58 +01003275 int const is_finished =
3276 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3277 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3278
Hanno Becker04da1892018-08-14 13:22:10 +01003279 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3280 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3281
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003282 /* Swap epochs before sending Finished: we can't do it after
3283 * sending ChangeCipherSpec, in case write returns WANT_READ.
3284 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003285 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003286 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003288 ssl_swap_epochs( ssl );
3289 }
3290
Hanno Becker67bc7c32018-08-06 11:33:50 +01003291 ret = ssl_get_remaining_payload_in_datagram( ssl );
3292 if( ret < 0 )
3293 return( ret );
3294 max_frag_len = (size_t) ret;
3295
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003296 /* CCS is copied as is, while HS messages may need fragmentation */
3297 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3298 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003299 if( max_frag_len == 0 )
3300 {
3301 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3302 return( ret );
3303
3304 continue;
3305 }
3306
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003307 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003308 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003309 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003311 /* Update position inside current message */
3312 ssl->handshake->cur_msg_p += cur->len;
3313 }
3314 else
3315 {
3316 const unsigned char * const p = ssl->handshake->cur_msg_p;
3317 const size_t hs_len = cur->len - 12;
3318 const size_t frag_off = p - ( cur->p + 12 );
3319 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003320 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321
Hanno Beckere1dcb032018-08-17 16:47:58 +01003322 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003323 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003324 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003325 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326
Hanno Becker67bc7c32018-08-06 11:33:50 +01003327 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3328 return( ret );
3329
3330 continue;
3331 }
3332 max_hs_frag_len = max_frag_len - 12;
3333
3334 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3335 max_hs_frag_len : rem_len;
3336
3337 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003338 {
3339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003340 (unsigned) cur_hs_frag_len,
3341 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003342 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003343
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003344 /* Messages are stored with handshake headers as if not fragmented,
3345 * copy beginning of headers then fill fragmentation fields.
3346 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3347 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003349 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3350 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3351 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3352
Hanno Becker67bc7c32018-08-06 11:33:50 +01003353 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3354 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3355 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003356
3357 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3358
Hanno Becker3f7b9732018-08-28 09:53:25 +01003359 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003360 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3361 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003362 ssl->out_msgtype = cur->type;
3363
3364 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003365 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003366 }
3367
3368 /* If done with the current message move to the next one if any */
3369 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3370 {
3371 if( cur->next != NULL )
3372 {
3373 ssl->handshake->cur_msg = cur->next;
3374 ssl->handshake->cur_msg_p = cur->next->p + 12;
3375 }
3376 else
3377 {
3378 ssl->handshake->cur_msg = NULL;
3379 ssl->handshake->cur_msg_p = NULL;
3380 }
3381 }
3382
3383 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003384 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003387 return( ret );
3388 }
3389 }
3390
Hanno Becker67bc7c32018-08-06 11:33:50 +01003391 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3392 return( ret );
3393
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003394 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3396 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003397 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3401 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003402
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003404
3405 return( 0 );
3406}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407
3408/*
3409 * To be called when the last message of an incoming flight is received.
3410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003412{
3413 /* We won't need to resend that one any more */
3414 ssl_flight_free( ssl->handshake->flight );
3415 ssl->handshake->flight = NULL;
3416 ssl->handshake->cur_msg = NULL;
3417
3418 /* The next incoming flight will start with this msg_seq */
3419 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3420
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003421 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003422 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003423
Hanno Becker0271f962018-08-16 13:23:47 +01003424 /* Clear future message buffering structure. */
3425 ssl_buffering_free( ssl );
3426
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003427 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003428 ssl_set_timer( ssl, 0 );
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3431 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 }
3435 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003438
3439/*
3440 * To be called when the last message of an outgoing flight is send.
3441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003443{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003444 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003445 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3448 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003451 }
3452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003456
Paul Bakker5121ce52009-01-03 21:22:43 +00003457/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003458 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460
3461/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003462 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003463 *
3464 * - fill in handshake headers
3465 * - update handshake checksum
3466 * - DTLS: save message for resending
3467 * - then pass to the record layer
3468 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003469 * DTLS: except for HelloRequest, messages are only queued, and will only be
3470 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003471 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003472 * Inputs:
3473 * - ssl->out_msglen: 4 + actual handshake message len
3474 * (4 is the size of handshake headers for TLS)
3475 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3476 * - ssl->out_msg + 4: the handshake message body
3477 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003478 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003479 * - ssl->out_msglen: the length of the record contents
3480 * (including handshake headers but excluding record headers)
3481 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003482 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003483int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003485 int ret;
3486 const size_t hs_len = ssl->out_msglen - 4;
3487 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003488
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3490
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003491 /*
3492 * Sanity checks
3493 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003494 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003495 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3496 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003497 /* In SSLv3, the client might send a NoCertificate alert. */
3498#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3499 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3500 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3501 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3502#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3503 {
3504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3506 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003509 /* Whenever we send anything different from a
3510 * HelloRequest we should be in a handshake - double check. */
3511 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3512 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003513 ssl->handshake == NULL )
3514 {
3515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003521 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003523 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003527#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003528
Hanno Beckerb50a2532018-08-06 11:52:54 +01003529 /* Double-check that we did not exceed the bounds
3530 * of the outgoing record buffer.
3531 * This should never fail as the various message
3532 * writing functions must obey the bounds of the
3533 * outgoing record buffer, but better be safe.
3534 *
3535 * Note: We deliberately do not check for the MTU or MFL here.
3536 */
3537 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3538 {
3539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3540 "size %u, maximum %u",
3541 (unsigned) ssl->out_msglen,
3542 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3544 }
3545
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003546 /*
3547 * Fill handshake headers
3548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003551 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3552 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3553 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003555 /*
3556 * DTLS has additional fields in the Handshake layer,
3557 * between the length field and the actual payload:
3558 * uint16 message_seq;
3559 * uint24 fragment_offset;
3560 * uint24 fragment_length;
3561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003564 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003565 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003566 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003567 {
3568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3569 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003570 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003571 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3573 }
3574
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003575 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003576 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003577
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003578 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003579 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003580 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003581 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3582 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3583 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003584 }
3585 else
3586 {
3587 ssl->out_msg[4] = 0;
3588 ssl->out_msg[5] = 0;
3589 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003590
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003591 /* Handshake hashes are computed without fragmentation,
3592 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003593 memset( ssl->out_msg + 6, 0x00, 3 );
3594 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003597
Hanno Becker0207e532018-08-28 10:28:28 +01003598 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003599 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3600 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 }
3602
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003603 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003606 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3607 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608 {
3609 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 return( ret );
3613 }
3614 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003615 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003617 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003618 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003619 {
3620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3621 return( ret );
3622 }
3623 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003624
3625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3626
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003627 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003628}
3629
3630/*
3631 * Record layer functions
3632 */
3633
3634/*
3635 * Write current record.
3636 *
3637 * Uses:
3638 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3639 * - ssl->out_msglen: length of the record content (excl headers)
3640 * - ssl->out_msg: record content
3641 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003642int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003643{
3644 int ret, done = 0;
3645 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003646 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003647
3648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003651 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 {
3654 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003657 return( ret );
3658 }
3659
3660 len = ssl->out_msglen;
3661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3665 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 ret = mbedtls_ssl_hw_record_write( ssl );
3670 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3673 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003674 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003675
3676 if( ret == 0 )
3677 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003678 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003680 if( !done )
3681 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003682 unsigned i;
3683 size_t protected_record_size;
3684
Paul Bakker05ef8352012-05-08 09:17:57 +00003685 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003687 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003688
Hanno Becker19859472018-08-06 09:40:20 +01003689 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003690 ssl->out_len[0] = (unsigned char)( len >> 8 );
3691 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003692
Paul Bakker48916f92012-09-16 19:57:18 +00003693 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003694 {
3695 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003698 return( ret );
3699 }
3700
3701 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003702 ssl->out_len[0] = (unsigned char)( len >> 8 );
3703 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003704 }
3705
Hanno Becker2b1e3542018-08-06 11:19:13 +01003706 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3707
3708#if defined(MBEDTLS_SSL_PROTO_DTLS)
3709 /* In case of DTLS, double-check that we don't exceed
3710 * the remaining space in the datagram. */
3711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3712 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003713 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003714 if( ret < 0 )
3715 return( ret );
3716
3717 if( protected_record_size > (size_t) ret )
3718 {
3719 /* Should never happen */
3720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3721 }
3722 }
3723#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003726 "version = [%d:%d], msglen = %d",
3727 ssl->out_hdr[0], ssl->out_hdr[1],
3728 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003731 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003732
3733 ssl->out_left += protected_record_size;
3734 ssl->out_hdr += protected_record_size;
3735 ssl_update_out_pointers( ssl, ssl->transform_out );
3736
Hanno Becker04484622018-08-06 09:49:38 +01003737 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3738 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3739 break;
3740
3741 /* The loop goes to its end iff the counter is wrapping */
3742 if( i == ssl_ep_len( ssl ) )
3743 {
3744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3745 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 }
3748
Hanno Becker67bc7c32018-08-06 11:33:50 +01003749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3751 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003752 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003753 size_t remaining;
3754 ret = ssl_get_remaining_payload_in_datagram( ssl );
3755 if( ret < 0 )
3756 {
3757 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3758 ret );
3759 return( ret );
3760 }
3761
3762 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003764 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003765 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003766 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 else
3768 {
Hanno Becker513815a2018-08-20 11:56:09 +01003769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003770 }
3771 }
3772#endif /* MBEDTLS_SSL_PROTO_DTLS */
3773
3774 if( ( flush == SSL_FORCE_FLUSH ) &&
3775 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 return( ret );
3779 }
3780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783 return( 0 );
3784}
3785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003787
3788static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3789{
3790 if( ssl->in_msglen < ssl->in_hslen ||
3791 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3792 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3793 {
3794 return( 1 );
3795 }
3796 return( 0 );
3797}
Hanno Becker44650b72018-08-16 12:51:11 +01003798
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003799static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003800{
3801 return( ( ssl->in_msg[9] << 16 ) |
3802 ( ssl->in_msg[10] << 8 ) |
3803 ssl->in_msg[11] );
3804}
3805
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003806static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003807{
3808 return( ( ssl->in_msg[6] << 16 ) |
3809 ( ssl->in_msg[7] << 8 ) |
3810 ssl->in_msg[8] );
3811}
3812
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003813static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003814{
3815 uint32_t msg_len, frag_off, frag_len;
3816
3817 msg_len = ssl_get_hs_total_len( ssl );
3818 frag_off = ssl_get_hs_frag_off( ssl );
3819 frag_len = ssl_get_hs_frag_len( ssl );
3820
3821 if( frag_off > msg_len )
3822 return( -1 );
3823
3824 if( frag_len > msg_len - frag_off )
3825 return( -1 );
3826
3827 if( frag_len + 12 > ssl->in_msglen )
3828 return( -1 );
3829
3830 return( 0 );
3831}
3832
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003833/*
3834 * Mark bits in bitmask (used for DTLS HS reassembly)
3835 */
3836static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3837{
3838 unsigned int start_bits, end_bits;
3839
3840 start_bits = 8 - ( offset % 8 );
3841 if( start_bits != 8 )
3842 {
3843 size_t first_byte_idx = offset / 8;
3844
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003845 /* Special case */
3846 if( len <= start_bits )
3847 {
3848 for( ; len != 0; len-- )
3849 mask[first_byte_idx] |= 1 << ( start_bits - len );
3850
3851 /* Avoid potential issues with offset or len becoming invalid */
3852 return;
3853 }
3854
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003855 offset += start_bits; /* Now offset % 8 == 0 */
3856 len -= start_bits;
3857
3858 for( ; start_bits != 0; start_bits-- )
3859 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3860 }
3861
3862 end_bits = len % 8;
3863 if( end_bits != 0 )
3864 {
3865 size_t last_byte_idx = ( offset + len ) / 8;
3866
3867 len -= end_bits; /* Now len % 8 == 0 */
3868
3869 for( ; end_bits != 0; end_bits-- )
3870 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3871 }
3872
3873 memset( mask + offset / 8, 0xFF, len / 8 );
3874}
3875
3876/*
3877 * Check that bitmask is full
3878 */
3879static int ssl_bitmask_check( unsigned char *mask, size_t len )
3880{
3881 size_t i;
3882
3883 for( i = 0; i < len / 8; i++ )
3884 if( mask[i] != 0xFF )
3885 return( -1 );
3886
3887 for( i = 0; i < len % 8; i++ )
3888 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3889 return( -1 );
3890
3891 return( 0 );
3892}
3893
Hanno Becker56e205e2018-08-16 09:06:12 +01003894/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003895static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003896 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003897{
Hanno Becker56e205e2018-08-16 09:06:12 +01003898 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003899
Hanno Becker56e205e2018-08-16 09:06:12 +01003900 alloc_len = 12; /* Handshake header */
3901 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003902
Hanno Beckerd07df862018-08-16 09:14:58 +01003903 if( add_bitmap )
3904 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003905
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003906 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003907}
Hanno Becker56e205e2018-08-16 09:06:12 +01003908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003910
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003911static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003912{
3913 return( ( ssl->in_msg[1] << 16 ) |
3914 ( ssl->in_msg[2] << 8 ) |
3915 ssl->in_msg[3] );
3916}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003917
Simon Butcher99000142016-10-13 17:21:01 +01003918int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003923 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003925 }
3926
Hanno Becker12555c62018-08-16 12:47:53 +01003927 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003930 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003931 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003934 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003935 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003936 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003937 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003938
Hanno Becker44650b72018-08-16 12:51:11 +01003939 if( ssl_check_hs_header( ssl ) != 0 )
3940 {
3941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3942 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3943 }
3944
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003945 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003946 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3947 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3948 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3949 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003950 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003951 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3952 {
3953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3954 recv_msg_seq,
3955 ssl->handshake->in_msg_seq ) );
3956 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3957 }
3958
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003959 /* Retransmit only on last message from previous flight, to avoid
3960 * too many retransmissions.
3961 * Besides, No sane server ever retransmits HelloVerifyRequest */
3962 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003966 "message_seq = %d, start_of_flight = %d",
3967 recv_msg_seq,
3968 ssl->handshake->in_flight_start_seq ) );
3969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003973 return( ret );
3974 }
3975 }
3976 else
3977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003979 "message_seq = %d, expected = %d",
3980 recv_msg_seq,
3981 ssl->handshake->in_msg_seq ) );
3982 }
3983
Hanno Becker90333da2017-10-10 11:27:13 +01003984 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003985 }
3986 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003987
Hanno Becker6d97ef52018-08-16 13:09:04 +01003988 /* Message reassembly is handled alongside buffering of future
3989 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003990 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003991 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003992 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003995 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003996 }
3997 }
3998 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004000 /* With TLS we don't handle fragmentation (for now) */
4001 if( ssl->in_msglen < ssl->in_hslen )
4002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4004 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004005 }
4006
Simon Butcher99000142016-10-13 17:21:01 +01004007 return( 0 );
4008}
4009
4010void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4011{
Hanno Becker0271f962018-08-16 13:23:47 +01004012 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004013
Hanno Becker0271f962018-08-16 13:23:47 +01004014 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004015 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004016 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004017 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004018
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004019 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004021 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004022 ssl->handshake != NULL )
4023 {
Hanno Becker0271f962018-08-16 13:23:47 +01004024 unsigned offset;
4025 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004026
Hanno Becker0271f962018-08-16 13:23:47 +01004027 /* Increment handshake sequence number */
4028 hs->in_msg_seq++;
4029
4030 /*
4031 * Clear up handshake buffering and reassembly structure.
4032 */
4033
4034 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004035 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004036
4037 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004038 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4039 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004040 offset++, hs_buf++ )
4041 {
4042 *hs_buf = *(hs_buf + 1);
4043 }
4044
4045 /* Create a fresh last entry */
4046 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004047 }
4048#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004049}
4050
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004051/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004052 * DTLS anti-replay: RFC 6347 4.1.2.6
4053 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004054 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4055 * Bit n is set iff record number in_window_top - n has been seen.
4056 *
4057 * Usually, in_window_top is the last record number seen and the lsb of
4058 * in_window is set. The only exception is the initial state (record number 0
4059 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4062static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004063{
4064 ssl->in_window_top = 0;
4065 ssl->in_window = 0;
4066}
4067
4068static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4069{
4070 return( ( (uint64_t) buf[0] << 40 ) |
4071 ( (uint64_t) buf[1] << 32 ) |
4072 ( (uint64_t) buf[2] << 24 ) |
4073 ( (uint64_t) buf[3] << 16 ) |
4074 ( (uint64_t) buf[4] << 8 ) |
4075 ( (uint64_t) buf[5] ) );
4076}
4077
4078/*
4079 * Return 0 if sequence number is acceptable, -1 otherwise
4080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004082{
4083 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4084 uint64_t bit;
4085
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004086 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004087 return( 0 );
4088
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004089 if( rec_seqnum > ssl->in_window_top )
4090 return( 0 );
4091
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004092 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004093
4094 if( bit >= 64 )
4095 return( -1 );
4096
4097 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4098 return( -1 );
4099
4100 return( 0 );
4101}
4102
4103/*
4104 * Update replay window on new validated record
4105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004107{
4108 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4109
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004110 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004111 return;
4112
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004113 if( rec_seqnum > ssl->in_window_top )
4114 {
4115 /* Update window_top and the contents of the window */
4116 uint64_t shift = rec_seqnum - ssl->in_window_top;
4117
4118 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004119 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004120 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004121 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004122 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004123 ssl->in_window |= 1;
4124 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004125
4126 ssl->in_window_top = rec_seqnum;
4127 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004128 else
4129 {
4130 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004131 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004132
4133 if( bit < 64 ) /* Always true, but be extra sure */
4134 ssl->in_window |= (uint64_t) 1 << bit;
4135 }
4136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004138
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004139#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004140/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004141static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4142
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004143/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004144 * Without any SSL context, check if a datagram looks like a ClientHello with
4145 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004146 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004147 *
4148 * - if cookie is valid, return 0
4149 * - if ClientHello looks superficially valid but cookie is not,
4150 * fill obuf and set olen, then
4151 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4152 * - otherwise return a specific error code
4153 */
4154static int ssl_check_dtls_clihlo_cookie(
4155 mbedtls_ssl_cookie_write_t *f_cookie_write,
4156 mbedtls_ssl_cookie_check_t *f_cookie_check,
4157 void *p_cookie,
4158 const unsigned char *cli_id, size_t cli_id_len,
4159 const unsigned char *in, size_t in_len,
4160 unsigned char *obuf, size_t buf_len, size_t *olen )
4161{
4162 size_t sid_len, cookie_len;
4163 unsigned char *p;
4164
4165 if( f_cookie_write == NULL || f_cookie_check == NULL )
4166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4167
4168 /*
4169 * Structure of ClientHello with record and handshake headers,
4170 * and expected values. We don't need to check a lot, more checks will be
4171 * done when actually parsing the ClientHello - skipping those checks
4172 * avoids code duplication and does not make cookie forging any easier.
4173 *
4174 * 0-0 ContentType type; copied, must be handshake
4175 * 1-2 ProtocolVersion version; copied
4176 * 3-4 uint16 epoch; copied, must be 0
4177 * 5-10 uint48 sequence_number; copied
4178 * 11-12 uint16 length; (ignored)
4179 *
4180 * 13-13 HandshakeType msg_type; (ignored)
4181 * 14-16 uint24 length; (ignored)
4182 * 17-18 uint16 message_seq; copied
4183 * 19-21 uint24 fragment_offset; copied, must be 0
4184 * 22-24 uint24 fragment_length; (ignored)
4185 *
4186 * 25-26 ProtocolVersion client_version; (ignored)
4187 * 27-58 Random random; (ignored)
4188 * 59-xx SessionID session_id; 1 byte len + sid_len content
4189 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4190 * ...
4191 *
4192 * Minimum length is 61 bytes.
4193 */
4194 if( in_len < 61 ||
4195 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4196 in[3] != 0 || in[4] != 0 ||
4197 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4198 {
4199 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4200 }
4201
4202 sid_len = in[59];
4203 if( sid_len > in_len - 61 )
4204 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4205
4206 cookie_len = in[60 + sid_len];
4207 if( cookie_len > in_len - 60 )
4208 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4209
4210 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4211 cli_id, cli_id_len ) == 0 )
4212 {
4213 /* Valid cookie */
4214 return( 0 );
4215 }
4216
4217 /*
4218 * If we get here, we've got an invalid cookie, let's prepare HVR.
4219 *
4220 * 0-0 ContentType type; copied
4221 * 1-2 ProtocolVersion version; copied
4222 * 3-4 uint16 epoch; copied
4223 * 5-10 uint48 sequence_number; copied
4224 * 11-12 uint16 length; olen - 13
4225 *
4226 * 13-13 HandshakeType msg_type; hello_verify_request
4227 * 14-16 uint24 length; olen - 25
4228 * 17-18 uint16 message_seq; copied
4229 * 19-21 uint24 fragment_offset; copied
4230 * 22-24 uint24 fragment_length; olen - 25
4231 *
4232 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4233 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4234 *
4235 * Minimum length is 28.
4236 */
4237 if( buf_len < 28 )
4238 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4239
4240 /* Copy most fields and adapt others */
4241 memcpy( obuf, in, 25 );
4242 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4243 obuf[25] = 0xfe;
4244 obuf[26] = 0xff;
4245
4246 /* Generate and write actual cookie */
4247 p = obuf + 28;
4248 if( f_cookie_write( p_cookie,
4249 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4250 {
4251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4252 }
4253
4254 *olen = p - obuf;
4255
4256 /* Go back and fill length fields */
4257 obuf[27] = (unsigned char)( *olen - 28 );
4258
4259 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4260 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4261 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4262
4263 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4264 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4265
4266 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4267}
4268
4269/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004270 * Handle possible client reconnect with the same UDP quadruplet
4271 * (RFC 6347 Section 4.2.8).
4272 *
4273 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4274 * that looks like a ClientHello.
4275 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004276 * - if the input looks like a ClientHello without cookies,
4277 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004278 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004279 * - if the input looks like a ClientHello with a valid cookie,
4280 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004281 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004282 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004283 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004284 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004285 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4286 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004287 */
4288static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4289{
4290 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004291 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004292
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004293 ret = ssl_check_dtls_clihlo_cookie(
4294 ssl->conf->f_cookie_write,
4295 ssl->conf->f_cookie_check,
4296 ssl->conf->p_cookie,
4297 ssl->cli_id, ssl->cli_id_len,
4298 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004299 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004300
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004301 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4302
4303 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004304 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004305 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004306 * If the error is permanent we'll catch it later,
4307 * if it's not, then hopefully it'll work next time. */
4308 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4309
4310 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004311 }
4312
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004313 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004314 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004315 /* Got a valid cookie, partially reset context */
4316 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4317 {
4318 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4319 return( ret );
4320 }
4321
4322 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004323 }
4324
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004325 return( ret );
4326}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004327#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004328
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004329/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004330 * ContentType type;
4331 * ProtocolVersion version;
4332 * uint16 epoch; // DTLS only
4333 * uint48 sequence_number; // DTLS only
4334 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004335 *
4336 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004337 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004338 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4339 *
4340 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004341 * 1. proceed with the record if this function returns 0
4342 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4343 * 3. return CLIENT_RECONNECT if this function return that value
4344 * 4. drop the whole datagram if this function returns anything else.
4345 * Point 2 is needed when the peer is resending, and we have already received
4346 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004350 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 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 +02004353
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004355 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004356 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004360 ssl->in_msgtype,
4361 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004362
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004363 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4365 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4366 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4367 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004370
4371#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004372 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4373 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004374 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4375#endif /* MBEDTLS_SSL_PROTO_DTLS */
4376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4377 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004380 }
4381
4382 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004383 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4386 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 }
4388
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004389 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004393 }
4394
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004395 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004396 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004397 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4400 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004401 }
4402
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004403 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004404 * DTLS-related tests.
4405 * Check epoch before checking length constraint because
4406 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4407 * message gets duplicated before the corresponding Finished message,
4408 * the second ChangeCipherSpec should be discarded because it belongs
4409 * to an old epoch, but not because its length is shorter than
4410 * the minimum record length for packets using the new record transform.
4411 * Note that these two kinds of failures are handled differently,
4412 * as an unexpected record is silently skipped but an invalid
4413 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004414 */
4415#if defined(MBEDTLS_SSL_PROTO_DTLS)
4416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4417 {
4418 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4419
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004420 /* Check epoch (and sequence number) with DTLS */
4421 if( rec_epoch != ssl->in_epoch )
4422 {
4423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4424 "expected %d, received %d",
4425 ssl->in_epoch, rec_epoch ) );
4426
4427#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4428 /*
4429 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4430 * access the first byte of record content (handshake type), as we
4431 * have an active transform (possibly iv_len != 0), so use the
4432 * fact that the record header len is 13 instead.
4433 */
4434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4435 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4436 rec_epoch == 0 &&
4437 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4438 ssl->in_left > 13 &&
4439 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4440 {
4441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4442 "from the same port" ) );
4443 return( ssl_handle_possible_reconnect( ssl ) );
4444 }
4445 else
4446#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004447 {
4448 /* Consider buffering the record. */
4449 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4450 {
4451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4452 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4453 }
4454
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004455 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004456 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004457 }
4458
4459#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4460 /* Replay detection only works for the current epoch */
4461 if( rec_epoch == ssl->in_epoch &&
4462 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4463 {
4464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4465 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4466 }
4467#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004468
Hanno Becker52c6dc62017-05-26 16:07:36 +01004469 /* Drop unexpected ApplicationData records,
4470 * except at the beginning of renegotiations */
4471 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4472 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4473#if defined(MBEDTLS_SSL_RENEGOTIATION)
4474 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4475 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4476#endif
4477 )
4478 {
4479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4480 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4481 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004482 }
4483#endif /* MBEDTLS_SSL_PROTO_DTLS */
4484
Hanno Becker52c6dc62017-05-26 16:07:36 +01004485
4486 /* Check length against bounds of the current transform and version */
4487 if( ssl->transform_in == NULL )
4488 {
4489 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004490 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004491 {
4492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4494 }
4495 }
4496 else
4497 {
4498 if( ssl->in_msglen < ssl->transform_in->minlen )
4499 {
4500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4502 }
4503
4504#if defined(MBEDTLS_SSL_PROTO_SSL3)
4505 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004506 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004507 {
4508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4509 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4510 }
4511#endif
4512#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4513 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4514 /*
4515 * TLS encrypted messages can have up to 256 bytes of padding
4516 */
4517 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4518 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004519 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004520 {
4521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4522 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4523 }
4524#endif
4525 }
4526
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004527 return( 0 );
4528}
Paul Bakker5121ce52009-01-03 21:22:43 +00004529
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004530/*
4531 * If applicable, decrypt (and decompress) record content
4532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004534{
4535 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4538 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4541 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 ret = mbedtls_ssl_hw_record_read( ssl );
4546 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004550 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004551
4552 if( ret == 0 )
4553 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004556 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004557 {
4558 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004561 return( ret );
4562 }
4563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 ssl->in_msg, ssl->in_msglen );
4566
Angus Grattond8213d02016-05-25 20:56:48 +10004567 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4570 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004571 }
4572 }
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004575 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004577 {
4578 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004581 return( ret );
4582 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004590 }
4591#endif
4592
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004593 return( 0 );
4594}
4595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004597
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004598/*
4599 * Read a record.
4600 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004601 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4602 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4603 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004604 */
Hanno Becker1097b342018-08-15 14:09:41 +01004605
4606/* Helper functions for mbedtls_ssl_read_record(). */
4607static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004608static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4609static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004610
Hanno Becker327c93b2018-08-15 13:56:18 +01004611int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004612 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004613{
4614 int ret;
4615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004617
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004618 if( ssl->keep_current_message == 0 )
4619 {
4620 do {
Simon Butcher99000142016-10-13 17:21:01 +01004621
Hanno Becker26994592018-08-15 14:14:59 +01004622 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004623 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004624 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004625
Hanno Beckere74d5562018-08-15 14:26:08 +01004626 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004627 {
Hanno Becker40f50842018-08-15 14:48:01 +01004628#if defined(MBEDTLS_SSL_PROTO_DTLS)
4629 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004630
Hanno Becker40f50842018-08-15 14:48:01 +01004631 /* We only check for buffered messages if the
4632 * current datagram is fully consumed. */
4633 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004634 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004635 {
Hanno Becker40f50842018-08-15 14:48:01 +01004636 if( ssl_load_buffered_message( ssl ) == 0 )
4637 have_buffered = 1;
4638 }
4639
4640 if( have_buffered == 0 )
4641#endif /* MBEDTLS_SSL_PROTO_DTLS */
4642 {
4643 ret = ssl_get_next_record( ssl );
4644 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4645 continue;
4646
4647 if( ret != 0 )
4648 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004649 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004650 return( ret );
4651 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004652 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004653 }
4654
4655 ret = mbedtls_ssl_handle_message_type( ssl );
4656
Hanno Becker40f50842018-08-15 14:48:01 +01004657#if defined(MBEDTLS_SSL_PROTO_DTLS)
4658 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4659 {
4660 /* Buffer future message */
4661 ret = ssl_buffer_message( ssl );
4662 if( ret != 0 )
4663 return( ret );
4664
4665 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4666 }
4667#endif /* MBEDTLS_SSL_PROTO_DTLS */
4668
Hanno Becker90333da2017-10-10 11:27:13 +01004669 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4670 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004671
4672 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004673 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004674 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004675 return( ret );
4676 }
4677
Hanno Becker327c93b2018-08-15 13:56:18 +01004678 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004679 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004680 {
4681 mbedtls_ssl_update_handshake_status( ssl );
4682 }
Simon Butcher99000142016-10-13 17:21:01 +01004683 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004684 else
Simon Butcher99000142016-10-13 17:21:01 +01004685 {
Hanno Becker02f59072018-08-15 14:00:24 +01004686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004687 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004688 }
4689
4690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4691
4692 return( 0 );
4693}
4694
Hanno Becker40f50842018-08-15 14:48:01 +01004695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004696static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004697{
Hanno Becker40f50842018-08-15 14:48:01 +01004698 if( ssl->in_left > ssl->next_record_offset )
4699 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004700
Hanno Becker40f50842018-08-15 14:48:01 +01004701 return( 0 );
4702}
4703
4704static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4705{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004706 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004707 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708 int ret = 0;
4709
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004710 if( hs == NULL )
4711 return( -1 );
4712
Hanno Beckere00ae372018-08-20 09:39:42 +01004713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4714
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004715 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4716 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4717 {
4718 /* Check if we have seen a ChangeCipherSpec before.
4719 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004720 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4723 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004724 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004725 }
4726
Hanno Becker39b8bc92018-08-28 17:17:13 +01004727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004728 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4729 ssl->in_msglen = 1;
4730 ssl->in_msg[0] = 1;
4731
4732 /* As long as they are equal, the exact value doesn't matter. */
4733 ssl->in_left = 0;
4734 ssl->next_record_offset = 0;
4735
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004736 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004737 goto exit;
4738 }
Hanno Becker37f95322018-08-16 13:55:32 +01004739
Hanno Beckerb8f50142018-08-28 10:01:34 +01004740#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004741 /* Debug only */
4742 {
4743 unsigned offset;
4744 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4745 {
4746 hs_buf = &hs->buffering.hs[offset];
4747 if( hs_buf->is_valid == 1 )
4748 {
4749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4750 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004751 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004752 }
4753 }
4754 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004755#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004756
4757 /* Check if we have buffered and/or fully reassembled the
4758 * next handshake message. */
4759 hs_buf = &hs->buffering.hs[0];
4760 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4761 {
4762 /* Synthesize a record containing the buffered HS message. */
4763 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4764 ( hs_buf->data[2] << 8 ) |
4765 hs_buf->data[3];
4766
4767 /* Double-check that we haven't accidentally buffered
4768 * a message that doesn't fit into the input buffer. */
4769 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4770 {
4771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4773 }
4774
4775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4776 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4777 hs_buf->data, msg_len + 12 );
4778
4779 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4780 ssl->in_hslen = msg_len + 12;
4781 ssl->in_msglen = msg_len + 12;
4782 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4783
4784 ret = 0;
4785 goto exit;
4786 }
4787 else
4788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4790 hs->in_msg_seq ) );
4791 }
4792
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004793 ret = -1;
4794
4795exit:
4796
4797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4798 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004799}
4800
Hanno Beckera02b0b42018-08-21 17:20:27 +01004801static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4802 size_t desired )
4803{
4804 int offset;
4805 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4807 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004808
Hanno Becker01315ea2018-08-21 17:22:17 +01004809 /* Get rid of future records epoch first, if such exist. */
4810 ssl_free_buffered_record( ssl );
4811
4812 /* Check if we have enough space available now. */
4813 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4814 hs->buffering.total_bytes_buffered ) )
4815 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004817 return( 0 );
4818 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004819
Hanno Becker4f432ad2018-08-28 10:02:32 +01004820 /* We don't have enough space to buffer the next expected handshake
4821 * message. Remove buffers used for future messages to gain space,
4822 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004823 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4824 offset >= 0; offset-- )
4825 {
4826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4827 offset ) );
4828
Hanno Beckerb309b922018-08-23 13:18:05 +01004829 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004830
4831 /* Check if we have enough space available now. */
4832 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4833 hs->buffering.total_bytes_buffered ) )
4834 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004836 return( 0 );
4837 }
4838 }
4839
4840 return( -1 );
4841}
4842
Hanno Becker40f50842018-08-15 14:48:01 +01004843static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4844{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004845 int ret = 0;
4846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4847
4848 if( hs == NULL )
4849 return( 0 );
4850
4851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4852
4853 switch( ssl->in_msgtype )
4854 {
4855 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004857
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004858 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004859 break;
4860
4861 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004862 {
4863 unsigned recv_msg_seq_offset;
4864 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4865 mbedtls_ssl_hs_buffer *hs_buf;
4866 size_t msg_len = ssl->in_hslen - 12;
4867
4868 /* We should never receive an old handshake
4869 * message - double-check nonetheless. */
4870 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4871 {
4872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4874 }
4875
4876 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4877 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4878 {
4879 /* Silently ignore -- message too far in the future */
4880 MBEDTLS_SSL_DEBUG_MSG( 2,
4881 ( "Ignore future HS message with sequence number %u, "
4882 "buffering window %u - %u",
4883 recv_msg_seq, ssl->handshake->in_msg_seq,
4884 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4885
4886 goto exit;
4887 }
4888
4889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4890 recv_msg_seq, recv_msg_seq_offset ) );
4891
4892 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4893
4894 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004895 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004896 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004897 size_t reassembly_buf_sz;
4898
Hanno Becker37f95322018-08-16 13:55:32 +01004899 hs_buf->is_fragmented =
4900 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4901
4902 /* We copy the message back into the input buffer
4903 * after reassembly, so check that it's not too large.
4904 * This is an implementation-specific limitation
4905 * and not one from the standard, hence it is not
4906 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004907 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004908 {
4909 /* Ignore message */
4910 goto exit;
4911 }
4912
Hanno Beckere0b150f2018-08-21 15:51:03 +01004913 /* Check if we have enough space to buffer the message. */
4914 if( hs->buffering.total_bytes_buffered >
4915 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4916 {
4917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4919 }
4920
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004921 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4922 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004923
4924 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4925 hs->buffering.total_bytes_buffered ) )
4926 {
4927 if( recv_msg_seq_offset > 0 )
4928 {
4929 /* If we can't buffer a future message because
4930 * of space limitations -- ignore. */
4931 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",
4932 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4933 (unsigned) hs->buffering.total_bytes_buffered ) );
4934 goto exit;
4935 }
Hanno Beckere1801392018-08-21 16:51:05 +01004936 else
4937 {
4938 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",
4939 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4940 (unsigned) hs->buffering.total_bytes_buffered ) );
4941 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004942
Hanno Beckera02b0b42018-08-21 17:20:27 +01004943 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004944 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004945 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",
4946 (unsigned) msg_len,
4947 (unsigned) reassembly_buf_sz,
4948 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004949 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004950 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4951 goto exit;
4952 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004953 }
4954
4955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4956 msg_len ) );
4957
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004958 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4959 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004960 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004961 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004962 goto exit;
4963 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004964 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004965
4966 /* Prepare final header: copy msg_type, length and message_seq,
4967 * then add standardised fragment_offset and fragment_length */
4968 memcpy( hs_buf->data, ssl->in_msg, 6 );
4969 memset( hs_buf->data + 6, 0, 3 );
4970 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4971
4972 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004973
4974 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004975 }
4976 else
4977 {
4978 /* Make sure msg_type and length are consistent */
4979 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4982 /* Ignore */
4983 goto exit;
4984 }
4985 }
4986
Hanno Becker4422bbb2018-08-20 09:40:19 +01004987 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004988 {
4989 size_t frag_len, frag_off;
4990 unsigned char * const msg = hs_buf->data + 12;
4991
4992 /*
4993 * Check and copy current fragment
4994 */
4995
4996 /* Validation of header fields already done in
4997 * mbedtls_ssl_prepare_handshake_record(). */
4998 frag_off = ssl_get_hs_frag_off( ssl );
4999 frag_len = ssl_get_hs_frag_len( ssl );
5000
5001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5002 frag_off, frag_len ) );
5003 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5004
5005 if( hs_buf->is_fragmented )
5006 {
5007 unsigned char * const bitmask = msg + msg_len;
5008 ssl_bitmask_set( bitmask, frag_off, frag_len );
5009 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5010 msg_len ) == 0 );
5011 }
5012 else
5013 {
5014 hs_buf->is_complete = 1;
5015 }
5016
5017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5018 hs_buf->is_complete ? "" : "not yet " ) );
5019 }
5020
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005021 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005022 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005023
5024 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005025 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005026 break;
5027 }
5028
5029exit:
5030
5031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5032 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005033}
5034#endif /* MBEDTLS_SSL_PROTO_DTLS */
5035
Hanno Becker1097b342018-08-15 14:09:41 +01005036static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005038 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005039 * Consume last content-layer message and potentially
5040 * update in_msglen which keeps track of the contents'
5041 * consumption state.
5042 *
5043 * (1) Handshake messages:
5044 * Remove last handshake message, move content
5045 * and adapt in_msglen.
5046 *
5047 * (2) Alert messages:
5048 * Consume whole record content, in_msglen = 0.
5049 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005050 * (3) Change cipher spec:
5051 * Consume whole record content, in_msglen = 0.
5052 *
5053 * (4) Application data:
5054 * Don't do anything - the record layer provides
5055 * the application data as a stream transport
5056 * and consumes through mbedtls_ssl_read only.
5057 *
5058 */
5059
5060 /* Case (1): Handshake messages */
5061 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005062 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005063 /* Hard assertion to be sure that no application data
5064 * is in flight, as corrupting ssl->in_msglen during
5065 * ssl->in_offt != NULL is fatal. */
5066 if( ssl->in_offt != NULL )
5067 {
5068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5070 }
5071
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005072 /*
5073 * Get next Handshake message in the current record
5074 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005075
Hanno Becker4a810fb2017-05-24 16:27:30 +01005076 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005077 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005078 * current handshake content: If DTLS handshake
5079 * fragmentation is used, that's the fragment
5080 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005081 * size here is faulty and should be changed at
5082 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005083 * (2) While it doesn't seem to cause problems, one
5084 * has to be very careful not to assume that in_hslen
5085 * is always <= in_msglen in a sensible communication.
5086 * Again, it's wrong for DTLS handshake fragmentation.
5087 * The following check is therefore mandatory, and
5088 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005089 * Additionally, ssl->in_hslen might be arbitrarily out of
5090 * bounds after handling a DTLS message with an unexpected
5091 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005092 */
5093 if( ssl->in_hslen < ssl->in_msglen )
5094 {
5095 ssl->in_msglen -= ssl->in_hslen;
5096 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5097 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005098
Hanno Becker4a810fb2017-05-24 16:27:30 +01005099 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5100 ssl->in_msg, ssl->in_msglen );
5101 }
5102 else
5103 {
5104 ssl->in_msglen = 0;
5105 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005106
Hanno Becker4a810fb2017-05-24 16:27:30 +01005107 ssl->in_hslen = 0;
5108 }
5109 /* Case (4): Application data */
5110 else if( ssl->in_offt != NULL )
5111 {
5112 return( 0 );
5113 }
5114 /* Everything else (CCS & Alerts) */
5115 else
5116 {
5117 ssl->in_msglen = 0;
5118 }
5119
Hanno Becker1097b342018-08-15 14:09:41 +01005120 return( 0 );
5121}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005122
Hanno Beckere74d5562018-08-15 14:26:08 +01005123static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5124{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005125 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005126 return( 1 );
5127
5128 return( 0 );
5129}
5130
Hanno Becker5f066e72018-08-16 14:56:31 +01005131#if defined(MBEDTLS_SSL_PROTO_DTLS)
5132
5133static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5134{
5135 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5136 if( hs == NULL )
5137 return;
5138
Hanno Becker01315ea2018-08-21 17:22:17 +01005139 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005140 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005141 hs->buffering.total_bytes_buffered -=
5142 hs->buffering.future_record.len;
5143
5144 mbedtls_free( hs->buffering.future_record.data );
5145 hs->buffering.future_record.data = NULL;
5146 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005147}
5148
5149static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5150{
5151 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5152 unsigned char * rec;
5153 size_t rec_len;
5154 unsigned rec_epoch;
5155
5156 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5157 return( 0 );
5158
5159 if( hs == NULL )
5160 return( 0 );
5161
Hanno Becker5f066e72018-08-16 14:56:31 +01005162 rec = hs->buffering.future_record.data;
5163 rec_len = hs->buffering.future_record.len;
5164 rec_epoch = hs->buffering.future_record.epoch;
5165
5166 if( rec == NULL )
5167 return( 0 );
5168
Hanno Becker4cb782d2018-08-20 11:19:05 +01005169 /* Only consider loading future records if the
5170 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005171 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005172 return( 0 );
5173
Hanno Becker5f066e72018-08-16 14:56:31 +01005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5175
5176 if( rec_epoch != ssl->in_epoch )
5177 {
5178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5179 goto exit;
5180 }
5181
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5183
5184 /* Double-check that the record is not too large */
5185 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5186 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5187 {
5188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5189 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5190 }
5191
5192 memcpy( ssl->in_hdr, rec, rec_len );
5193 ssl->in_left = rec_len;
5194 ssl->next_record_offset = 0;
5195
5196 ssl_free_buffered_record( ssl );
5197
5198exit:
5199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5200 return( 0 );
5201}
5202
5203static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5204{
5205 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5206 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005207 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005208
5209 /* Don't buffer future records outside handshakes. */
5210 if( hs == NULL )
5211 return( 0 );
5212
5213 /* Only buffer handshake records (we are only interested
5214 * in Finished messages). */
5215 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5216 return( 0 );
5217
5218 /* Don't buffer more than one future epoch record. */
5219 if( hs->buffering.future_record.data != NULL )
5220 return( 0 );
5221
Hanno Becker01315ea2018-08-21 17:22:17 +01005222 /* Don't buffer record if there's not enough buffering space remaining. */
5223 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5224 hs->buffering.total_bytes_buffered ) )
5225 {
5226 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",
5227 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5228 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005229 return( 0 );
5230 }
5231
Hanno Becker5f066e72018-08-16 14:56:31 +01005232 /* Buffer record */
5233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5234 ssl->in_epoch + 1 ) );
5235 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5236 rec_hdr_len + ssl->in_msglen );
5237
5238 /* ssl_parse_record_header() only considers records
5239 * of the next epoch as candidates for buffering. */
5240 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005241 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005242
5243 hs->buffering.future_record.data =
5244 mbedtls_calloc( 1, hs->buffering.future_record.len );
5245 if( hs->buffering.future_record.data == NULL )
5246 {
5247 /* If we run out of RAM trying to buffer a
5248 * record from the next epoch, just ignore. */
5249 return( 0 );
5250 }
5251
Hanno Becker01315ea2018-08-21 17:22:17 +01005252 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005253
Hanno Becker01315ea2018-08-21 17:22:17 +01005254 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005255 return( 0 );
5256}
5257
5258#endif /* MBEDTLS_SSL_PROTO_DTLS */
5259
Hanno Beckere74d5562018-08-15 14:26:08 +01005260static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005261{
5262 int ret;
5263
Hanno Becker5f066e72018-08-16 14:56:31 +01005264#if defined(MBEDTLS_SSL_PROTO_DTLS)
5265 /* We might have buffered a future record; if so,
5266 * and if the epoch matches now, load it.
5267 * On success, this call will set ssl->in_left to
5268 * the length of the buffered record, so that
5269 * the calls to ssl_fetch_input() below will
5270 * essentially be no-ops. */
5271 ret = ssl_load_buffered_record( ssl );
5272 if( ret != 0 )
5273 return( ret );
5274#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005279 return( ret );
5280 }
5281
5282 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5286 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005287 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005288 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5289 {
5290 ret = ssl_buffer_future_record( ssl );
5291 if( ret != 0 )
5292 return( ret );
5293
5294 /* Fall through to handling of unexpected records */
5295 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5296 }
5297
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005298 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5299 {
5300 /* Skip unexpected record (but not whole datagram) */
5301 ssl->next_record_offset = ssl->in_msglen
5302 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005303
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5305 "(header)" ) );
5306 }
5307 else
5308 {
5309 /* Skip invalid record and the rest of the datagram */
5310 ssl->next_record_offset = 0;
5311 ssl->in_left = 0;
5312
5313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5314 "(header)" ) );
5315 }
5316
5317 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005318 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005319 }
5320#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005322 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005323
5324 /*
5325 * Read and optionally decrypt the message contents
5326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5328 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005331 return( ret );
5332 }
5333
5334 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005339 if( ssl->next_record_offset < ssl->in_left )
5340 {
5341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5342 }
5343 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005344 else
5345#endif
5346 ssl->in_left = 0;
5347
5348 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005352 {
5353 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5355 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005356 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005357 /* Except when waiting for Finished as a bad mac here
5358 * probably means something went wrong in the handshake
5359 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5360 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5361 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5362 {
5363#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5364 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5365 {
5366 mbedtls_ssl_send_alert_message( ssl,
5367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5368 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5369 }
5370#endif
5371 return( ret );
5372 }
5373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005375 if( ssl->conf->badmac_limit != 0 &&
5376 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5379 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005380 }
5381#endif
5382
Hanno Becker4a810fb2017-05-24 16:27:30 +01005383 /* As above, invalid records cause
5384 * dismissal of the whole datagram. */
5385
5386 ssl->next_record_offset = 0;
5387 ssl->in_left = 0;
5388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005390 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005391 }
5392
5393 return( ret );
5394 }
5395 else
5396#endif
5397 {
5398 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5400 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 mbedtls_ssl_send_alert_message( ssl,
5403 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5404 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005405 }
5406#endif
5407 return( ret );
5408 }
5409 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005410
Simon Butcher99000142016-10-13 17:21:01 +01005411 return( 0 );
5412}
5413
5414int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5415{
5416 int ret;
5417
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005418 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005419 * Handle particular types of records
5420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 {
Simon Butcher99000142016-10-13 17:21:01 +01005423 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5424 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005425 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005427 }
5428
Hanno Beckere678eaa2018-08-21 14:57:46 +01005429 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005430 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005431 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005432 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5434 ssl->in_msglen ) );
5435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005436 }
5437
Hanno Beckere678eaa2018-08-21 14:57:46 +01005438 if( ssl->in_msg[0] != 1 )
5439 {
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5441 ssl->in_msg[0] ) );
5442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5443 }
5444
5445#if defined(MBEDTLS_SSL_PROTO_DTLS)
5446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5447 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5448 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5449 {
5450 if( ssl->handshake == NULL )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5453 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5454 }
5455
5456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5457 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5458 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005459#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005460 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005464 if( ssl->in_msglen != 2 )
5465 {
5466 /* Note: Standard allows for more than one 2 byte alert
5467 to be packed in a single message, but Mbed TLS doesn't
5468 currently support this. */
5469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5470 ssl->in_msglen ) );
5471 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5472 }
5473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 ssl->in_msg[0], ssl->in_msg[1] ) );
5476
5477 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005478 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005483 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005485 }
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5488 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5491 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005493
5494#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5495 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5496 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5497 {
Hanno Becker90333da2017-10-10 11:27:13 +01005498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005499 /* Will be handled when trying to parse ServerHello */
5500 return( 0 );
5501 }
5502#endif
5503
5504#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5505 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5506 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5507 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5508 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5509 {
5510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5511 /* Will be handled in mbedtls_ssl_parse_certificate() */
5512 return( 0 );
5513 }
5514#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5515
5516 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005517 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 }
5519
Hanno Beckerc76c6192017-06-06 10:03:17 +01005520#if defined(MBEDTLS_SSL_PROTO_DTLS)
5521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5522 ssl->handshake != NULL &&
5523 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5524 {
5525 ssl_handshake_wrapup_free_hs_transform( ssl );
5526 }
5527#endif
5528
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 return( 0 );
5530}
5531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005533{
5534 int ret;
5535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5537 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5538 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005539 {
5540 return( ret );
5541 }
5542
5543 return( 0 );
5544}
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005547 unsigned char level,
5548 unsigned char message )
5549{
5550 int ret;
5551
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005552 if( ssl == NULL || ssl->conf == NULL )
5553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005559 ssl->out_msglen = 2;
5560 ssl->out_msg[0] = level;
5561 ssl->out_msg[1] = message;
5562
Hanno Becker67bc7c32018-08-06 11:33:50 +01005563 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005566 return( ret );
5567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005569
5570 return( 0 );
5571}
5572
Hanno Beckerb9d44792019-02-08 07:19:04 +00005573#if defined(MBEDTLS_X509_CRT_PARSE_C)
5574static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5575{
5576#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5577 if( session->peer_cert != NULL )
5578 {
5579 mbedtls_x509_crt_free( session->peer_cert );
5580 mbedtls_free( session->peer_cert );
5581 session->peer_cert = NULL;
5582 }
5583#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5584 if( session->peer_cert_digest != NULL )
5585 {
5586 /* Zeroization is not necessary. */
5587 mbedtls_free( session->peer_cert_digest );
5588 session->peer_cert_digest = NULL;
5589 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5590 session->peer_cert_digest_len = 0;
5591 }
5592#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5593}
5594#endif /* MBEDTLS_X509_CRT_PARSE_C */
5595
Paul Bakker5121ce52009-01-03 21:22:43 +00005596/*
5597 * Handshake functions
5598 */
Hanno Becker21489932019-02-05 13:20:55 +00005599#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005600/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606
Hanno Becker7177a882019-02-05 13:36:46 +00005607 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005610 ssl->state++;
5611 return( 0 );
5612 }
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005616}
5617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005623
Hanno Becker7177a882019-02-05 13:36:46 +00005624 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005627 ssl->state++;
5628 return( 0 );
5629 }
5630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005633}
Gilles Peskinef9828522017-05-03 12:28:43 +02005634
Hanno Becker21489932019-02-05 13:20:55 +00005635#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005636/* Some certificate support -> implement write and parse */
5637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005641 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 const mbedtls_x509_crt *crt;
5643 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005646
Hanno Becker7177a882019-02-05 13:36:46 +00005647 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005650 ssl->state++;
5651 return( 0 );
5652 }
5653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005656 {
5657 if( ssl->client_auth == 0 )
5658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 ssl->state++;
5661 return( 0 );
5662 }
5663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 /*
5666 * If using SSLv3 and got no cert, send an Alert message
5667 * (otherwise an empty Certificate message will be sent).
5668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5670 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005671 {
5672 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5674 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5675 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 goto write_msg;
5679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#endif /* MBEDTLS_SSL_CLI_C */
5683#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5689 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 }
5691 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005692#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005695
5696 /*
5697 * 0 . 0 handshake type
5698 * 1 . 3 handshake length
5699 * 4 . 6 length of all certs
5700 * 7 . 9 length of cert. 1
5701 * 10 . n-1 peer certificate
5702 * n . n+2 length of cert. 2
5703 * n+3 . ... upper level cert, etc.
5704 */
5705 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707
Paul Bakker29087132010-03-21 21:03:34 +00005708 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 {
5710 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005711 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005714 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 }
5717
5718 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5719 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5720 ssl->out_msg[i + 2] = (unsigned char)( n );
5721
5722 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5723 i += n; crt = crt->next;
5724 }
5725
5726 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5727 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5728 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5729
5730 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5732 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005733
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005734#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005735write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005736#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005737
5738 ssl->state++;
5739
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005740 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005741 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 return( ret );
5744 }
5745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005747
Paul Bakkered27a042013-04-18 22:46:23 +02005748 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005749}
5750
Hanno Becker84879e32019-01-31 07:44:03 +00005751#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005752
5753#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005754static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5755 unsigned char *crt_buf,
5756 size_t crt_buf_len )
5757{
5758 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5759
5760 if( peer_crt == NULL )
5761 return( -1 );
5762
5763 if( peer_crt->raw.len != crt_buf_len )
5764 return( -1 );
5765
Hanno Becker46f34d02019-02-08 14:00:04 +00005766 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005767}
Hanno Becker177475a2019-02-05 17:02:46 +00005768#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5769static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5770 unsigned char *crt_buf,
5771 size_t crt_buf_len )
5772{
5773 int ret;
5774 unsigned char const * const peer_cert_digest =
5775 ssl->session->peer_cert_digest;
5776 mbedtls_md_type_t const peer_cert_digest_type =
5777 ssl->session->peer_cert_digest_type;
5778 mbedtls_md_info_t const * const digest_info =
5779 mbedtls_md_info_from_type( peer_cert_digest_type );
5780 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5781 size_t digest_len;
5782
5783 if( peer_cert_digest == NULL || digest_info == NULL )
5784 return( -1 );
5785
5786 digest_len = mbedtls_md_get_size( digest_info );
5787 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5788 return( -1 );
5789
5790 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5791 if( ret != 0 )
5792 return( -1 );
5793
5794 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5795}
5796#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005797#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005798
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005799/*
5800 * Once the certificate message is read, parse it into a cert chain and
5801 * perform basic checks, but leave actual verification to the caller
5802 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005803static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5804 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005805{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005806 int ret;
5807#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5808 int crt_cnt=0;
5809#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005810 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005811 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5817 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 }
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5822 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5826 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 }
5829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005831
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005835 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005836
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005837 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5842 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 }
5845
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005846 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5847 i += 3;
5848
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005849 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005850 while( i < ssl->in_hslen )
5851 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005852 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005853 if ( i + 3 > ssl->in_hslen ) {
5854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005855 mbedtls_ssl_send_alert_message( ssl,
5856 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005858 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5859 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005860 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5861 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 if( ssl->in_msg[i] != 0 )
5863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005865 mbedtls_ssl_send_alert_message( ssl,
5866 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5867 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 }
5870
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005871 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5873 | (unsigned int) ssl->in_msg[i + 2];
5874 i += 3;
5875
5876 if( n < 128 || i + n > ssl->in_hslen )
5877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005879 mbedtls_ssl_send_alert_message( ssl,
5880 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5881 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 }
5884
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005885 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005886#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5887 if( crt_cnt++ == 0 &&
5888 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5889 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005890 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005891 /* During client-side renegotiation, check that the server's
5892 * end-CRTs hasn't changed compared to the initial handshake,
5893 * mitigating the triple handshake attack. On success, reuse
5894 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5896 if( ssl_check_peer_crt_unchanged( ssl,
5897 &ssl->in_msg[i],
5898 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005899 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5901 mbedtls_ssl_send_alert_message( ssl,
5902 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5903 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5904 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005905 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005906
5907 /* Now we can safely free the original chain. */
5908 ssl_clear_peer_cert( ssl->session );
5909 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005910#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5911
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005912 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00005913#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005914 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00005915#else
Hanno Becker353a6f02019-02-26 11:51:34 +00005916 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00005917 * it in-place from the input buffer instead of making a copy. */
5918 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5919#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005920 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005922 case 0: /*ok*/
5923 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5924 /* Ignore certificate with an unknown algorithm: maybe a
5925 prior certificate was already trusted. */
5926 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005927
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005928 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5929 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5930 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005931
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005932 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5933 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5934 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005935
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005936 default:
5937 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5938 crt_parse_der_failed:
5939 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5940 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5941 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 }
5943
5944 i += n;
5945 }
5946
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005947 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005948 return( 0 );
5949}
5950
Hanno Becker4a55f632019-02-05 12:49:06 +00005951#if defined(MBEDTLS_SSL_SRV_C)
5952static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5953{
5954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5955 return( -1 );
5956
5957#if defined(MBEDTLS_SSL_PROTO_SSL3)
5958 /*
5959 * Check if the client sent an empty certificate
5960 */
5961 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5962 {
5963 if( ssl->in_msglen == 2 &&
5964 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5965 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5966 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5967 {
5968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5969 return( 0 );
5970 }
5971
5972 return( -1 );
5973 }
5974#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5975
5976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5978 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5979 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5980 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5981 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5982 {
5983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5984 return( 0 );
5985 }
5986
5987 return( -1 );
5988#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5989 MBEDTLS_SSL_PROTO_TLS1_2 */
5990}
5991#endif /* MBEDTLS_SSL_SRV_C */
5992
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005993/* Check if a certificate message is expected.
5994 * Return either
5995 * - SSL_CERTIFICATE_EXPECTED, or
5996 * - SSL_CERTIFICATE_SKIP
5997 * indicating whether a Certificate message is expected or not.
5998 */
5999#define SSL_CERTIFICATE_EXPECTED 0
6000#define SSL_CERTIFICATE_SKIP 1
6001static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6002 int authmode )
6003{
6004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6005 ssl->transform_negotiate->ciphersuite_info;
6006
6007 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6008 return( SSL_CERTIFICATE_SKIP );
6009
6010#if defined(MBEDTLS_SSL_SRV_C)
6011 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6012 {
6013 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6014 return( SSL_CERTIFICATE_SKIP );
6015
6016 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6017 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006018 ssl->session_negotiate->verify_result =
6019 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6020 return( SSL_CERTIFICATE_SKIP );
6021 }
6022 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006023#else
6024 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006025#endif /* MBEDTLS_SSL_SRV_C */
6026
6027 return( SSL_CERTIFICATE_EXPECTED );
6028}
6029
Hanno Becker68636192019-02-05 14:36:34 +00006030static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6031 int authmode,
6032 mbedtls_x509_crt *chain,
6033 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006034{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006035 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006036 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6037 ssl->transform_negotiate->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006038 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006039
6040 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6041 return( 0 );
6042
Hanno Becker68636192019-02-05 14:36:34 +00006043 /*
6044 * Main check: verify certificate
6045 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006046#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6047 if( ssl->conf->f_ca_cb != NULL )
6048 {
6049 ((void) rs_ctx);
6050 have_ca_chain = 1;
6051
6052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6053 ret = mbedtls_x509_crt_verify_with_cb(
6054 chain,
6055 ssl->conf->f_ca_cb,
6056 ssl->conf->p_ca_cb,
6057 ssl->conf->cert_profile,
6058 ssl->hostname,
6059 &ssl->session_negotiate->verify_result,
6060 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
6061 }
6062 else
6063#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6064 {
6065 mbedtls_x509_crt *ca_chain;
6066 mbedtls_x509_crl *ca_crl;
6067
6068#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6069 if( ssl->handshake->sni_ca_chain != NULL )
6070 {
6071 ca_chain = ssl->handshake->sni_ca_chain;
6072 ca_crl = ssl->handshake->sni_ca_crl;
6073 }
6074 else
6075#endif
6076 {
6077 ca_chain = ssl->conf->ca_chain;
6078 ca_crl = ssl->conf->ca_crl;
6079 }
6080
6081 if( ca_chain != NULL )
6082 have_ca_chain = 1;
6083
6084 ret = mbedtls_x509_crt_verify_restartable(
6085 chain,
6086 ca_chain, ca_crl,
6087 ssl->conf->cert_profile,
6088 ssl->hostname,
6089 &ssl->session_negotiate->verify_result,
6090 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6091 }
Hanno Becker68636192019-02-05 14:36:34 +00006092
6093 if( ret != 0 )
6094 {
6095 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6096 }
6097
6098#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6099 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6100 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6101#endif
6102
6103 /*
6104 * Secondary checks: always done, but change 'ret' only if it was 0
6105 */
6106
6107#if defined(MBEDTLS_ECP_C)
6108 {
6109 const mbedtls_pk_context *pk = &chain->pk;
6110
6111 /* If certificate uses an EC key, make sure the curve is OK */
6112 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6113 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6114 {
6115 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6116
6117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6118 if( ret == 0 )
6119 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6120 }
6121 }
6122#endif /* MBEDTLS_ECP_C */
6123
6124 if( mbedtls_ssl_check_cert_usage( chain,
6125 ciphersuite_info,
6126 ! ssl->conf->endpoint,
6127 &ssl->session_negotiate->verify_result ) != 0 )
6128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6130 if( ret == 0 )
6131 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6132 }
6133
6134 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6135 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6136 * with details encoded in the verification flags. All other kinds
6137 * of error codes, including those from the user provided f_vrfy
6138 * functions, are treated as fatal and lead to a failure of
6139 * ssl_parse_certificate even if verification was optional. */
6140 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6141 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6142 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6143 {
6144 ret = 0;
6145 }
6146
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006147 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006148 {
6149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6150 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6151 }
6152
6153 if( ret != 0 )
6154 {
6155 uint8_t alert;
6156
6157 /* The certificate may have been rejected for several reasons.
6158 Pick one and send the corresponding alert. Which alert to send
6159 may be a subject of debate in some cases. */
6160 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6161 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6162 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6163 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6164 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6165 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6166 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6167 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6168 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6169 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6170 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6171 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6172 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6173 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6174 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6175 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6176 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6177 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6178 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6179 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6180 else
6181 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6182 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6183 alert );
6184 }
6185
6186#if defined(MBEDTLS_DEBUG_C)
6187 if( ssl->session_negotiate->verify_result != 0 )
6188 {
6189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6190 ssl->session_negotiate->verify_result ) );
6191 }
6192 else
6193 {
6194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6195 }
6196#endif /* MBEDTLS_DEBUG_C */
6197
6198 return( ret );
6199}
6200
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006201#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6202static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6203 unsigned char *start, size_t len )
6204{
6205 int ret;
6206 /* Remember digest of the peer's end-CRT. */
6207 ssl->session_negotiate->peer_cert_digest =
6208 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6209 if( ssl->session_negotiate->peer_cert_digest == NULL )
6210 {
6211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6212 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6213 mbedtls_ssl_send_alert_message( ssl,
6214 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6215 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6216
6217 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6218 }
6219
6220 ret = mbedtls_md( mbedtls_md_info_from_type(
6221 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6222 start, len,
6223 ssl->session_negotiate->peer_cert_digest );
6224
6225 ssl->session_negotiate->peer_cert_digest_type =
6226 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6227 ssl->session_negotiate->peer_cert_digest_len =
6228 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6229
6230 return( ret );
6231}
6232
6233static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6234 unsigned char *start, size_t len )
6235{
6236 unsigned char *end = start + len;
6237 int ret;
6238
6239 /* Make a copy of the peer's raw public key. */
6240 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6241 ret = mbedtls_pk_parse_subpubkey( &start, end,
6242 &ssl->handshake->peer_pubkey );
6243 if( ret != 0 )
6244 {
6245 /* We should have parsed the public key before. */
6246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6247 }
6248
6249 return( 0 );
6250}
6251#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6252
Hanno Becker68636192019-02-05 14:36:34 +00006253int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6254{
6255 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006256 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006257#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6258 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6259 ? ssl->handshake->sni_authmode
6260 : ssl->conf->authmode;
6261#else
6262 const int authmode = ssl->conf->authmode;
6263#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006264 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006265 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006266
6267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6268
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006269 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6270 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006271 {
6272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006273 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006274 }
6275
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006276#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6277 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006278 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006279 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006280 chain = ssl->handshake->ecrs_peer_cert;
6281 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006282 goto crt_verify;
6283 }
6284#endif
6285
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006286 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006287 {
6288 /* mbedtls_ssl_read_record may have sent an alert already. We
6289 let it decide whether to alert. */
6290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006291 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006292 }
6293
Hanno Becker4a55f632019-02-05 12:49:06 +00006294#if defined(MBEDTLS_SSL_SRV_C)
6295 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6296 {
6297 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006298
6299 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006300 ret = 0;
6301 else
6302 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006303
Hanno Becker6bdfab22019-02-05 13:11:17 +00006304 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006305 }
6306#endif /* MBEDTLS_SSL_SRV_C */
6307
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006308 /* Clear existing peer CRT structure in case we tried to
6309 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006310 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006311
6312 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6313 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006314 {
6315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6316 sizeof( mbedtls_x509_crt ) ) );
6317 mbedtls_ssl_send_alert_message( ssl,
6318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006320
Hanno Becker3dad3112019-02-05 17:19:52 +00006321 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6322 goto exit;
6323 }
6324 mbedtls_x509_crt_init( chain );
6325
6326 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006327 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006328 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006329
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006330#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6331 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006332 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006333
6334crt_verify:
6335 if( ssl->handshake->ecrs_enabled)
6336 rs_ctx = &ssl->handshake->ecrs_ctx;
6337#endif
6338
Hanno Becker68636192019-02-05 14:36:34 +00006339 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006340 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006341 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006342 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006344#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006345 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006346 unsigned char *crt_start, *pk_start;
6347 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006348
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006349 /* We parse the CRT chain without copying, so
6350 * these pointers point into the input buffer,
6351 * and are hence still valid after freeing the
6352 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006353
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006354 crt_start = chain->raw.p;
6355 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006356
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006357 pk_start = chain->pk_raw.p;
6358 pk_len = chain->pk_raw.len;
6359
6360 /* Free the CRT structures before computing
6361 * digest and copying the peer's public key. */
6362 mbedtls_x509_crt_free( chain );
6363 mbedtls_free( chain );
6364 chain = NULL;
6365
6366 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006367 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006368 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006369
6370 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6371 if( ret != 0 )
6372 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006373 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006374#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6375 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006376 ssl->session_negotiate->peer_cert = chain;
6377 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006378#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Hanno Becker6bdfab22019-02-05 13:11:17 +00006382exit:
6383
Hanno Becker3dad3112019-02-05 17:19:52 +00006384 if( ret == 0 )
6385 ssl->state++;
6386
6387#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6388 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6389 {
6390 ssl->handshake->ecrs_peer_cert = chain;
6391 chain = NULL;
6392 }
6393#endif
6394
6395 if( chain != NULL )
6396 {
6397 mbedtls_x509_crt_free( chain );
6398 mbedtls_free( chain );
6399 }
6400
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 return( ret );
6402}
Hanno Becker21489932019-02-05 13:20:55 +00006403#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006406{
6407 int ret;
6408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 ssl->out_msglen = 1;
6413 ssl->out_msg[0] = 1;
6414
Paul Bakker5121ce52009-01-03 21:22:43 +00006415 ssl->state++;
6416
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006417 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 return( ret );
6421 }
6422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006424
6425 return( 0 );
6426}
6427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429{
6430 int ret;
6431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006433
Hanno Becker327c93b2018-08-15 13:56:18 +01006434 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 return( ret );
6438 }
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6444 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 }
6447
Hanno Beckere678eaa2018-08-21 14:57:46 +01006448 /* CCS records are only accepted if they have length 1 and content '1',
6449 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006450
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006451 /*
6452 * Switch to our negotiated transform and session parameters for inbound
6453 * data.
6454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006456 ssl->transform_in = ssl->transform_negotiate;
6457 ssl->session_in = ssl->session_negotiate;
6458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006460 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006463 ssl_dtls_replay_reset( ssl );
6464#endif
6465
6466 /* Increment epoch */
6467 if( ++ssl->in_epoch == 0 )
6468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006470 /* This is highly unlikely to happen for legitimate reasons, so
6471 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006473 }
6474 }
6475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006477 memset( ssl->in_ctr, 0, 8 );
6478
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006479 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6482 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6488 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006490 }
6491 }
6492#endif
6493
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 ssl->state++;
6495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006497
6498 return( 0 );
6499}
6500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006503{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006504 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6507 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6508 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006509 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006510 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6513#if defined(MBEDTLS_SHA512_C)
6514 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006515 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6516 else
6517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_SHA256_C)
6519 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006520 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006521 else
6522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006526 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006527 }
Paul Bakker380da532012-04-18 16:10:25 +00006528}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6533 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006534 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6535 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6538#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006539#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006540 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006541 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6542#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006543 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006544#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006548 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006549 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006550#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006551 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006552#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006555}
6556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006558 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6561 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006562 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6563 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6566#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006567#if defined(MBEDTLS_USE_PSA_CRYPTO)
6568 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6569#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006570 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006571#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006574#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006575 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006576#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006577 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006578#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006581}
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6584 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6585static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006586 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006587{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006588 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6589 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006590}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006591#endif
Paul Bakker380da532012-04-18 16:10:25 +00006592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6594#if defined(MBEDTLS_SHA256_C)
6595static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006596 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006597{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006598#if defined(MBEDTLS_USE_PSA_CRYPTO)
6599 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6600#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006601 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006602#endif
Paul Bakker380da532012-04-18 16:10:25 +00006603}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006604#endif
Paul Bakker380da532012-04-18 16:10:25 +00006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606#if defined(MBEDTLS_SHA512_C)
6607static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006608 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006609{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006610#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006611 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006612#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006613 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006614#endif
Paul Bakker380da532012-04-18 16:10:25 +00006615}
Paul Bakker769075d2012-11-24 11:26:46 +01006616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006620static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006622{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006623 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006624 mbedtls_md5_context md5;
6625 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006626
Paul Bakker5121ce52009-01-03 21:22:43 +00006627 unsigned char padbuf[48];
6628 unsigned char md5sum[16];
6629 unsigned char sha1sum[20];
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006632 if( !session )
6633 session = ssl->session;
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006637 mbedtls_md5_init( &md5 );
6638 mbedtls_sha1_init( &sha1 );
6639
6640 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6641 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642
6643 /*
6644 * SSLv3:
6645 * hash =
6646 * MD5( master + pad2 +
6647 * MD5( handshake + sender + master + pad1 ) )
6648 * + SHA1( master + pad2 +
6649 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 */
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006653 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6654 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006658 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6659 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006660#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006663 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Paul Bakker1ef83d62012-04-11 12:09:53 +00006665 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006667 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6668 mbedtls_md5_update_ret( &md5, session->master, 48 );
6669 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6670 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006672 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6673 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6674 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6675 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006676
Paul Bakker1ef83d62012-04-11 12:09:53 +00006677 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006679 mbedtls_md5_starts_ret( &md5 );
6680 mbedtls_md5_update_ret( &md5, session->master, 48 );
6681 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6682 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6683 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006685 mbedtls_sha1_starts_ret( &sha1 );
6686 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6687 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6688 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6689 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006693 mbedtls_md5_free( &md5 );
6694 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006696 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6697 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6698 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006705static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006707{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006708 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006709 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006710 mbedtls_md5_context md5;
6711 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006712 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006715 if( !session )
6716 session = ssl->session;
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006720 mbedtls_md5_init( &md5 );
6721 mbedtls_sha1_init( &sha1 );
6722
6723 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6724 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006725
Paul Bakker1ef83d62012-04-11 12:09:53 +00006726 /*
6727 * TLSv1:
6728 * hash = PRF( master, finished_label,
6729 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6730 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006733 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6734 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006735#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006738 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6739 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006740#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006743 ? "client finished"
6744 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006745
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006746 mbedtls_md5_finish_ret( &md5, padbuf );
6747 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006748
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006749 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006750 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006753
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006754 mbedtls_md5_free( &md5 );
6755 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006757 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6764#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006765static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767{
6768 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006769 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006770 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006771#if defined(MBEDTLS_USE_PSA_CRYPTO)
6772 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006773 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006774 psa_status_t status;
6775#else
6776 mbedtls_sha256_context sha256;
6777#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006780 if( !session )
6781 session = ssl->session;
6782
Andrzej Kurekeb342242019-01-29 09:14:33 -05006783 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6784 ? "client finished"
6785 : "server finished";
6786
6787#if defined(MBEDTLS_USE_PSA_CRYPTO)
6788 sha256_psa = psa_hash_operation_init();
6789
6790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6791
6792 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6793 if( status != PSA_SUCCESS )
6794 {
6795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6796 return;
6797 }
6798
6799 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6800 if( status != PSA_SUCCESS )
6801 {
6802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6803 return;
6804 }
6805 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6806#else
6807
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006808 mbedtls_sha256_init( &sha256 );
6809
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006812 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006813
6814 /*
6815 * TLSv1.2:
6816 * hash = PRF( master, finished_label,
6817 * Hash( handshake ) )[0.11]
6818 */
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820#if !defined(MBEDTLS_SHA256_ALT)
6821 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006822 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006823#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006824
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006825 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006826 mbedtls_sha256_free( &sha256 );
6827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006828
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006829 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006830 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006833
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006834 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006841static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006843{
6844 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006845 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006846 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006847#if defined(MBEDTLS_USE_PSA_CRYPTO)
6848 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006849 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006850 psa_status_t status;
6851#else
6852 mbedtls_sha512_context sha512;
6853#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006856 if( !session )
6857 session = ssl->session;
6858
Andrzej Kurekeb342242019-01-29 09:14:33 -05006859 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6860 ? "client finished"
6861 : "server finished";
6862
6863#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006864 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006865
6866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6867
Andrzej Kurek972fba52019-01-30 03:29:12 -05006868 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006869 if( status != PSA_SUCCESS )
6870 {
6871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6872 return;
6873 }
6874
Andrzej Kurek972fba52019-01-30 03:29:12 -05006875 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006876 if( status != PSA_SUCCESS )
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6879 return;
6880 }
6881 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6882#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006883 mbedtls_sha512_init( &sha512 );
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006886
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006887 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006888
6889 /*
6890 * TLSv1.2:
6891 * hash = PRF( master, finished_label,
6892 * Hash( handshake ) )[0.11]
6893 */
6894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006896 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6897 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006898#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006899
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006900 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006901 mbedtls_sha512_free( &sha512 );
6902#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006903
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006904 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006905 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006908
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006909 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913#endif /* MBEDTLS_SHA512_C */
6914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006919
6920 /*
6921 * Free our handshake params
6922 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006923 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006925 ssl->handshake = NULL;
6926
6927 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006928 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006929 */
6930 if( ssl->transform )
6931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 mbedtls_ssl_transform_free( ssl->transform );
6933 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006934 }
6935 ssl->transform = ssl->transform_negotiate;
6936 ssl->transform_negotiate = NULL;
6937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006939}
6940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006942{
6943 int resume = ssl->handshake->resume;
6944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SSL_RENEGOTIATION)
6948 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006951 ssl->renego_records_seen = 0;
6952 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006953#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006954
6955 /*
6956 * Free the previous session and switch in the current one
6957 */
Paul Bakker0a597072012-09-25 21:55:46 +00006958 if( ssl->session )
6959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006961 /* RFC 7366 3.1: keep the EtM state */
6962 ssl->session_negotiate->encrypt_then_mac =
6963 ssl->session->encrypt_then_mac;
6964#endif
6965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 mbedtls_ssl_session_free( ssl->session );
6967 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006968 }
6969 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006970 ssl->session_negotiate = NULL;
6971
Paul Bakker0a597072012-09-25 21:55:46 +00006972 /*
6973 * Add cache entry
6974 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006975 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006976 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006977 resume == 0 )
6978 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006979 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006981 }
Paul Bakker0a597072012-09-25 21:55:46 +00006982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006984 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006985 ssl->handshake->flight != NULL )
6986 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006987 /* Cancel handshake timer */
6988 ssl_set_timer( ssl, 0 );
6989
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006990 /* Keep last flight around in case we need to resend it:
6991 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006993 }
6994 else
6995#endif
6996 ssl_handshake_wrapup_free_hs_transform( ssl );
6997
Paul Bakker48916f92012-09-16 19:57:18 +00006998 ssl->state++;
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007001}
7002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007005 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007009 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007010
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007011 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007012
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007013 /*
7014 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7015 * may define some other value. Currently (early 2016), no defined
7016 * ciphersuite does this (and this is unlikely to change as activity has
7017 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007022 ssl->verify_data_len = hash_len;
7023 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007024#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007025
Paul Bakker5121ce52009-01-03 21:22:43 +00007026 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7028 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
7030 /*
7031 * In case of session resuming, invert the client and server
7032 * ChangeCipherSpec messages order.
7033 */
Paul Bakker0a597072012-09-25 21:55:46 +00007034 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 }
7045 else
7046 ssl->state++;
7047
Paul Bakker48916f92012-09-16 19:57:18 +00007048 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007049 * Switch to our negotiated transform and session parameters for outbound
7050 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007056 {
7057 unsigned char i;
7058
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007059 /* Remember current epoch settings for resending */
7060 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007061 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007062
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007063 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007064 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007065
7066 /* Increment epoch */
7067 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007068 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007069 break;
7070
7071 /* The loop goes to its end iff the counter is wrapping */
7072 if( i == 0 )
7073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7075 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007076 }
7077 }
7078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007080 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007082 ssl->transform_out = ssl->transform_negotiate;
7083 ssl->session_out = ssl->session_negotiate;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7086 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7091 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007092 }
7093 }
7094#endif
7095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007099#endif
7100
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007101 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 return( ret );
7105 }
7106
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007107#if defined(MBEDTLS_SSL_PROTO_DTLS)
7108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7109 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7110 {
7111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7112 return( ret );
7113 }
7114#endif
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
7118 return( 0 );
7119}
7120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007122#define SSL_MAX_HASH_LEN 36
7123#else
7124#define SSL_MAX_HASH_LEN 12
7125#endif
7126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007128{
Paul Bakker23986e52011-04-24 08:57:21 +00007129 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007130 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007131 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007135 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136
Hanno Becker327c93b2018-08-15 13:56:18 +01007137 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 return( ret );
7141 }
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007146 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7147 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007149 }
7150
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007151 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152#if defined(MBEDTLS_SSL_PROTO_SSL3)
7153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007154 hash_len = 36;
7155 else
7156#endif
7157 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7160 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7164 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 }
7167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007169 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007172 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7173 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007175 }
7176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007178 ssl->verify_data_len = hash_len;
7179 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007180#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007181
Paul Bakker0a597072012-09-25 21:55:46 +00007182 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007185 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007191#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 }
7193 else
7194 ssl->state++;
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007199#endif
7200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007202
7203 return( 0 );
7204}
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7211 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7212 mbedtls_md5_init( &handshake->fin_md5 );
7213 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007214 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7215 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7218#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007219#if defined(MBEDTLS_USE_PSA_CRYPTO)
7220 handshake->fin_sha256_psa = psa_hash_operation_init();
7221 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7222#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007224 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007225#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007228#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007229 handshake->fin_sha384_psa = psa_hash_operation_init();
7230 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007234#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007237
7238 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007239
7240#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7241 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7242 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7243#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#if defined(MBEDTLS_DHM_C)
7246 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if defined(MBEDTLS_ECDH_C)
7249 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007250#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007251#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007252 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007253#if defined(MBEDTLS_SSL_CLI_C)
7254 handshake->ecjpake_cache = NULL;
7255 handshake->ecjpake_cache_len = 0;
7256#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007257#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007258
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007259#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007260 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007261#endif
7262
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007263#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7264 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7265#endif
Hanno Becker75173122019-02-06 16:18:31 +00007266
7267#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7268 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7269 mbedtls_pk_init( &handshake->peer_pubkey );
7270#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007271}
7272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7278 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 mbedtls_md_init( &transform->md_ctx_enc );
7281 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007282}
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007287}
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007290{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007291 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007292 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007294 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007296 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007297 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007298
7299 /*
7300 * Either the pointers are now NULL or cleared properly and can be freed.
7301 * Now allocate missing structures.
7302 */
7303 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007304 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007305 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007306 }
Paul Bakker48916f92012-09-16 19:57:18 +00007307
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007308 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007309 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007310 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007311 }
Paul Bakker48916f92012-09-16 19:57:18 +00007312
Paul Bakker82788fb2014-10-20 13:59:19 +02007313 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007314 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007315 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007316 }
Paul Bakker48916f92012-09-16 19:57:18 +00007317
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007318 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007319 if( ssl->handshake == NULL ||
7320 ssl->transform_negotiate == NULL ||
7321 ssl->session_negotiate == NULL )
7322 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 mbedtls_free( ssl->handshake );
7326 mbedtls_free( ssl->transform_negotiate );
7327 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007328
7329 ssl->handshake = NULL;
7330 ssl->transform_negotiate = NULL;
7331 ssl->session_negotiate = NULL;
7332
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007333 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007334 }
7335
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007336 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007338 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007339 ssl_handshake_params_init( ssl->handshake );
7340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7343 {
7344 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007345
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007346 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7347 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7348 else
7349 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007350
7351 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007352 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007353#endif
7354
Paul Bakker48916f92012-09-16 19:57:18 +00007355 return( 0 );
7356}
7357
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007358#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007359/* Dummy cookie callbacks for defaults */
7360static int ssl_cookie_write_dummy( void *ctx,
7361 unsigned char **p, unsigned char *end,
7362 const unsigned char *cli_id, size_t cli_id_len )
7363{
7364 ((void) ctx);
7365 ((void) p);
7366 ((void) end);
7367 ((void) cli_id);
7368 ((void) cli_id_len);
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007371}
7372
7373static int ssl_cookie_check_dummy( void *ctx,
7374 const unsigned char *cookie, size_t cookie_len,
7375 const unsigned char *cli_id, size_t cli_id_len )
7376{
7377 ((void) ctx);
7378 ((void) cookie);
7379 ((void) cookie_len);
7380 ((void) cli_id);
7381 ((void) cli_id_len);
7382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007384}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007385#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007386
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007387/* Once ssl->out_hdr as the address of the beginning of the
7388 * next outgoing record is set, deduce the other pointers.
7389 *
7390 * Note: For TLS, we save the implicit record sequence number
7391 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7392 * and the caller has to make sure there's space for this.
7393 */
7394
7395static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7396 mbedtls_ssl_transform *transform )
7397{
7398#if defined(MBEDTLS_SSL_PROTO_DTLS)
7399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7400 {
7401 ssl->out_ctr = ssl->out_hdr + 3;
7402 ssl->out_len = ssl->out_hdr + 11;
7403 ssl->out_iv = ssl->out_hdr + 13;
7404 }
7405 else
7406#endif
7407 {
7408 ssl->out_ctr = ssl->out_hdr - 8;
7409 ssl->out_len = ssl->out_hdr + 3;
7410 ssl->out_iv = ssl->out_hdr + 5;
7411 }
7412
7413 /* Adjust out_msg to make space for explicit IV, if used. */
7414 if( transform != NULL &&
7415 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7416 {
7417 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7418 }
7419 else
7420 ssl->out_msg = ssl->out_iv;
7421}
7422
7423/* Once ssl->in_hdr as the address of the beginning of the
7424 * next incoming record is set, deduce the other pointers.
7425 *
7426 * Note: For TLS, we save the implicit record sequence number
7427 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7428 * and the caller has to make sure there's space for this.
7429 */
7430
7431static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7432 mbedtls_ssl_transform *transform )
7433{
7434#if defined(MBEDTLS_SSL_PROTO_DTLS)
7435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7436 {
7437 ssl->in_ctr = ssl->in_hdr + 3;
7438 ssl->in_len = ssl->in_hdr + 11;
7439 ssl->in_iv = ssl->in_hdr + 13;
7440 }
7441 else
7442#endif
7443 {
7444 ssl->in_ctr = ssl->in_hdr - 8;
7445 ssl->in_len = ssl->in_hdr + 3;
7446 ssl->in_iv = ssl->in_hdr + 5;
7447 }
7448
7449 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7450 if( transform != NULL &&
7451 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7452 {
7453 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7454 }
7455 else
7456 ssl->in_msg = ssl->in_iv;
7457}
7458
Paul Bakker5121ce52009-01-03 21:22:43 +00007459/*
7460 * Initialize an SSL context
7461 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007462void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7463{
7464 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7465}
7466
7467/*
7468 * Setup an SSL context
7469 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007470
7471static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7472{
7473 /* Set the incoming and outgoing record pointers. */
7474#if defined(MBEDTLS_SSL_PROTO_DTLS)
7475 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7476 {
7477 ssl->out_hdr = ssl->out_buf;
7478 ssl->in_hdr = ssl->in_buf;
7479 }
7480 else
7481#endif /* MBEDTLS_SSL_PROTO_DTLS */
7482 {
7483 ssl->out_hdr = ssl->out_buf + 8;
7484 ssl->in_hdr = ssl->in_buf + 8;
7485 }
7486
7487 /* Derive other internal pointers. */
7488 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7489 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7490}
7491
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007492int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007493 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007494{
Paul Bakker48916f92012-09-16 19:57:18 +00007495 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007497 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007498
7499 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007500 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007501 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007502
7503 /* Set to NULL in case of an error condition */
7504 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007505
Angus Grattond8213d02016-05-25 20:56:48 +10007506 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7507 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007508 {
Angus Grattond8213d02016-05-25 20:56:48 +10007509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007510 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007511 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007512 }
7513
7514 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7515 if( ssl->out_buf == NULL )
7516 {
7517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007518 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007519 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 }
7521
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007522 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007523
Paul Bakker48916f92012-09-16 19:57:18 +00007524 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007525 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007526
7527 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007528
7529error:
7530 mbedtls_free( ssl->in_buf );
7531 mbedtls_free( ssl->out_buf );
7532
7533 ssl->conf = NULL;
7534
7535 ssl->in_buf = NULL;
7536 ssl->out_buf = NULL;
7537
7538 ssl->in_hdr = NULL;
7539 ssl->in_ctr = NULL;
7540 ssl->in_len = NULL;
7541 ssl->in_iv = NULL;
7542 ssl->in_msg = NULL;
7543
7544 ssl->out_hdr = NULL;
7545 ssl->out_ctr = NULL;
7546 ssl->out_len = NULL;
7547 ssl->out_iv = NULL;
7548 ssl->out_msg = NULL;
7549
k-stachowiak9f7798e2018-07-31 16:52:32 +02007550 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551}
7552
7553/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007554 * Reset an initialized and used SSL context for re-use while retaining
7555 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007556 *
7557 * If partial is non-zero, keep data in the input buffer and client ID.
7558 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007559 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007560static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007561{
Paul Bakker48916f92012-09-16 19:57:18 +00007562 int ret;
7563
Hanno Becker7e772132018-08-10 12:38:21 +01007564#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7565 !defined(MBEDTLS_SSL_SRV_C)
7566 ((void) partial);
7567#endif
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007570
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007571 /* Cancel any possibly running timer */
7572 ssl_set_timer( ssl, 0 );
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#if defined(MBEDTLS_SSL_RENEGOTIATION)
7575 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007576 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007577
7578 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7580 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007583
Paul Bakker7eb013f2011-10-06 12:37:39 +00007584 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007585 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007586
7587 ssl->in_msgtype = 0;
7588 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007590 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007591 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007594 ssl_dtls_replay_reset( ssl );
7595#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007596
7597 ssl->in_hslen = 0;
7598 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007599
7600 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007601
7602 ssl->out_msgtype = 0;
7603 ssl->out_msglen = 0;
7604 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7606 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007607 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007608#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007609
Hanno Becker19859472018-08-06 09:40:20 +01007610 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7611
Paul Bakker48916f92012-09-16 19:57:18 +00007612 ssl->transform_in = NULL;
7613 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007614
Hanno Becker78640902018-08-13 16:35:15 +01007615 ssl->session_in = NULL;
7616 ssl->session_out = NULL;
7617
Angus Grattond8213d02016-05-25 20:56:48 +10007618 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007619
7620#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007621 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007622#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7623 {
7624 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007625 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007626 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7629 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7632 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7635 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007636 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007637 }
7638#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007639
Paul Bakker48916f92012-09-16 19:57:18 +00007640 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 mbedtls_ssl_transform_free( ssl->transform );
7643 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007644 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007645 }
Paul Bakker48916f92012-09-16 19:57:18 +00007646
Paul Bakkerc0463502013-02-14 11:19:38 +01007647 if( ssl->session )
7648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 mbedtls_ssl_session_free( ssl->session );
7650 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007651 ssl->session = NULL;
7652 }
7653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007655 ssl->alpn_chosen = NULL;
7656#endif
7657
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007658#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007659#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007660 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007661#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007662 {
7663 mbedtls_free( ssl->cli_id );
7664 ssl->cli_id = NULL;
7665 ssl->cli_id_len = 0;
7666 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007667#endif
7668
Paul Bakker48916f92012-09-16 19:57:18 +00007669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7670 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007671
7672 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007673}
7674
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007675/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007676 * Reset an initialized and used SSL context for re-use while retaining
7677 * all application-set variables, function pointers and data.
7678 */
7679int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7680{
7681 return( ssl_session_reset_int( ssl, 0 ) );
7682}
7683
7684/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007685 * SSL set accessors
7686 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007687void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007689 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007690}
7691
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007692void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007693{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007694 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007695}
7696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007698void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007699{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007700 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007701}
7702#endif
7703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007705void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007707 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007708}
7709#endif
7710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007712
Hanno Becker1841b0a2018-08-24 11:13:57 +01007713void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7714 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007715{
7716 ssl->disable_datagram_packing = !allow_packing;
7717}
7718
7719void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7720 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007721{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007722 conf->hs_timeout_min = min;
7723 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007724}
7725#endif
7726
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007727void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007729 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007730}
7731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007733void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007734 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007735 void *p_vrfy )
7736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007737 conf->f_vrfy = f_vrfy;
7738 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007741
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007742void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007743 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007744 void *p_rng )
7745{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007746 conf->f_rng = f_rng;
7747 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007748}
7749
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007750void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007751 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 void *p_dbg )
7753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->f_dbg = f_dbg;
7755 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007756}
7757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007759 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007760 mbedtls_ssl_send_t *f_send,
7761 mbedtls_ssl_recv_t *f_recv,
7762 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007763{
7764 ssl->p_bio = p_bio;
7765 ssl->f_send = f_send;
7766 ssl->f_recv = f_recv;
7767 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007768}
7769
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007770#if defined(MBEDTLS_SSL_PROTO_DTLS)
7771void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7772{
7773 ssl->mtu = mtu;
7774}
7775#endif
7776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007777void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007778{
7779 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007780}
7781
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007782void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7783 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007784 mbedtls_ssl_set_timer_t *f_set_timer,
7785 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007786{
7787 ssl->p_timer = p_timer;
7788 ssl->f_set_timer = f_set_timer;
7789 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007790
7791 /* Make sure we start with no timer running */
7792 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007793}
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007796void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007797 void *p_cache,
7798 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7799 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007801 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007802 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007803 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_CLI_C)
7808int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007809{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007810 int ret;
7811
7812 if( ssl == NULL ||
7813 session == NULL ||
7814 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007815 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007818 }
7819
Hanno Becker52055ae2019-02-06 14:30:46 +00007820 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7821 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007822 return( ret );
7823
Paul Bakker0a597072012-09-25 21:55:46 +00007824 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007825
7826 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007830void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007831 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007832{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007833 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7834 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7835 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7836 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007837}
7838
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007839void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007840 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007841 int major, int minor )
7842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007844 return;
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007847 return;
7848
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007849 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007853void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007854 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007855{
7856 conf->cert_profile = profile;
7857}
7858
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007859/* Append a new keycert entry to a (possibly empty) list */
7860static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7861 mbedtls_x509_crt *cert,
7862 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007863{
niisato8ee24222018-06-25 19:05:48 +09007864 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007865
niisato8ee24222018-06-25 19:05:48 +09007866 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7867 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007868 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007869
niisato8ee24222018-06-25 19:05:48 +09007870 new_cert->cert = cert;
7871 new_cert->key = key;
7872 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007873
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007874 /* Update head is the list was null, else add to the end */
7875 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007876 {
niisato8ee24222018-06-25 19:05:48 +09007877 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007878 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007879 else
7880 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007881 mbedtls_ssl_key_cert *cur = *head;
7882 while( cur->next != NULL )
7883 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007884 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007885 }
7886
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007887 return( 0 );
7888}
7889
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007890int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007891 mbedtls_x509_crt *own_cert,
7892 mbedtls_pk_context *pk_key )
7893{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007894 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007895}
7896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007897void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007898 mbedtls_x509_crt *ca_chain,
7899 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007900{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007901 conf->ca_chain = ca_chain;
7902 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00007903
7904#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7905 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7906 * cannot be used together. */
7907 conf->f_ca_cb = NULL;
7908 conf->p_ca_cb = NULL;
7909#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00007910}
Hanno Becker5adaad92019-03-27 16:54:37 +00007911
7912#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7913void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007914 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00007915 void *p_ca_cb )
7916{
7917 conf->f_ca_cb = f_ca_cb;
7918 conf->p_ca_cb = p_ca_cb;
7919
7920 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7921 * cannot be used together. */
7922 conf->ca_chain = NULL;
7923 conf->ca_crl = NULL;
7924}
7925#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007927
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007928#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7929int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7930 mbedtls_x509_crt *own_cert,
7931 mbedtls_pk_context *pk_key )
7932{
7933 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7934 own_cert, pk_key ) );
7935}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007936
7937void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7938 mbedtls_x509_crt *ca_chain,
7939 mbedtls_x509_crl *ca_crl )
7940{
7941 ssl->handshake->sni_ca_chain = ca_chain;
7942 ssl->handshake->sni_ca_crl = ca_crl;
7943}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007944
7945void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7946 int authmode )
7947{
7948 ssl->handshake->sni_authmode = authmode;
7949}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007950#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7951
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007952#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007953/*
7954 * Set EC J-PAKE password for current handshake
7955 */
7956int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7957 const unsigned char *pw,
7958 size_t pw_len )
7959{
7960 mbedtls_ecjpake_role role;
7961
Janos Follath8eb64132016-06-03 15:40:57 +01007962 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7964
7965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7966 role = MBEDTLS_ECJPAKE_SERVER;
7967 else
7968 role = MBEDTLS_ECJPAKE_CLIENT;
7969
7970 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7971 role,
7972 MBEDTLS_MD_SHA256,
7973 MBEDTLS_ECP_DP_SECP256R1,
7974 pw, pw_len ) );
7975}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007976#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007979
7980static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7981{
7982 /* Remove reference to existing PSK, if any. */
7983#if defined(MBEDTLS_USE_PSA_CRYPTO)
7984 if( conf->psk_opaque != 0 )
7985 {
7986 /* The maintenance of the PSK key slot is the
7987 * user's responsibility. */
7988 conf->psk_opaque = 0;
7989 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007990 /* This and the following branch should never
7991 * be taken simultaenously as we maintain the
7992 * invariant that raw and opaque PSKs are never
7993 * configured simultaneously. As a safeguard,
7994 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007995#endif /* MBEDTLS_USE_PSA_CRYPTO */
7996 if( conf->psk != NULL )
7997 {
7998 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7999
8000 mbedtls_free( conf->psk );
8001 conf->psk = NULL;
8002 conf->psk_len = 0;
8003 }
8004
8005 /* Remove reference to PSK identity, if any. */
8006 if( conf->psk_identity != NULL )
8007 {
8008 mbedtls_free( conf->psk_identity );
8009 conf->psk_identity = NULL;
8010 conf->psk_identity_len = 0;
8011 }
8012}
8013
Hanno Becker7390c712018-11-15 13:33:04 +00008014/* This function assumes that PSK identity in the SSL config is unset.
8015 * It checks that the provided identity is well-formed and attempts
8016 * to make a copy of it in the SSL config.
8017 * On failure, the PSK identity in the config remains unset. */
8018static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8019 unsigned char const *psk_identity,
8020 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008021{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008022 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008023 if( psk_identity == NULL ||
8024 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008025 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008026 {
8027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8028 }
8029
Hanno Becker7390c712018-11-15 13:33:04 +00008030 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8031 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008032 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008033
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008034 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008035 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008036
8037 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008038}
8039
Hanno Becker7390c712018-11-15 13:33:04 +00008040int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8041 const unsigned char *psk, size_t psk_len,
8042 const unsigned char *psk_identity, size_t psk_identity_len )
8043{
8044 int ret;
8045 /* Remove opaque/raw PSK + PSK Identity */
8046 ssl_conf_remove_psk( conf );
8047
8048 /* Check and set raw PSK */
8049 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8051 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8052 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8053 conf->psk_len = psk_len;
8054 memcpy( conf->psk, psk, conf->psk_len );
8055
8056 /* Check and set PSK Identity */
8057 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8058 if( ret != 0 )
8059 ssl_conf_remove_psk( conf );
8060
8061 return( ret );
8062}
8063
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008064static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8065{
8066#if defined(MBEDTLS_USE_PSA_CRYPTO)
8067 if( ssl->handshake->psk_opaque != 0 )
8068 {
8069 ssl->handshake->psk_opaque = 0;
8070 }
8071 else
8072#endif /* MBEDTLS_USE_PSA_CRYPTO */
8073 if( ssl->handshake->psk != NULL )
8074 {
8075 mbedtls_platform_zeroize( ssl->handshake->psk,
8076 ssl->handshake->psk_len );
8077 mbedtls_free( ssl->handshake->psk );
8078 ssl->handshake->psk_len = 0;
8079 }
8080}
8081
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008082int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8083 const unsigned char *psk, size_t psk_len )
8084{
8085 if( psk == NULL || ssl->handshake == NULL )
8086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8087
8088 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8090
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008091 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008092
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008093 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008094 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008095
8096 ssl->handshake->psk_len = psk_len;
8097 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8098
8099 return( 0 );
8100}
8101
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008102#if defined(MBEDTLS_USE_PSA_CRYPTO)
8103int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008104 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008105 const unsigned char *psk_identity,
8106 size_t psk_identity_len )
8107{
Hanno Becker7390c712018-11-15 13:33:04 +00008108 int ret;
8109 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008110 ssl_conf_remove_psk( conf );
8111
Hanno Becker7390c712018-11-15 13:33:04 +00008112 /* Check and set opaque PSK */
8113 if( psk_slot == 0 )
8114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008115 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008116
8117 /* Check and set PSK Identity */
8118 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8119 psk_identity_len );
8120 if( ret != 0 )
8121 ssl_conf_remove_psk( conf );
8122
8123 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008124}
8125
8126int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008127 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008128{
8129 if( psk_slot == 0 || ssl->handshake == NULL )
8130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8131
8132 ssl_remove_psk( ssl );
8133 ssl->handshake->psk_opaque = psk_slot;
8134 return( 0 );
8135}
8136#endif /* MBEDTLS_USE_PSA_CRYPTO */
8137
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008138void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008140 size_t),
8141 void *p_psk )
8142{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008143 conf->f_psk = f_psk;
8144 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008147
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008148#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008149
8150#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008151int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008152{
8153 int ret;
8154
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008155 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8156 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8157 {
8158 mbedtls_mpi_free( &conf->dhm_P );
8159 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008160 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008162
8163 return( 0 );
8164}
Hanno Becker470a8c42017-10-04 15:28:46 +01008165#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
Hanno Beckera90658f2017-10-04 15:29:08 +01008167int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8168 const unsigned char *dhm_P, size_t P_len,
8169 const unsigned char *dhm_G, size_t G_len )
8170{
8171 int ret;
8172
8173 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8174 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8175 {
8176 mbedtls_mpi_free( &conf->dhm_P );
8177 mbedtls_mpi_free( &conf->dhm_G );
8178 return( ret );
8179 }
8180
8181 return( 0 );
8182}
Paul Bakker5121ce52009-01-03 21:22:43 +00008183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008184int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008185{
8186 int ret;
8187
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008188 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8189 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8190 {
8191 mbedtls_mpi_free( &conf->dhm_P );
8192 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008193 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008194 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008195
8196 return( 0 );
8197}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008198#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008199
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008200#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8201/*
8202 * Set the minimum length for Diffie-Hellman parameters
8203 */
8204void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8205 unsigned int bitlen )
8206{
8207 conf->dhm_min_bitlen = bitlen;
8208}
8209#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8210
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008211#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008212/*
8213 * Set allowed/preferred hashes for handshake signatures
8214 */
8215void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8216 const int *hashes )
8217{
8218 conf->sig_hashes = hashes;
8219}
Hanno Becker947194e2017-04-07 13:25:49 +01008220#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008221
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008222#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008223/*
8224 * Set the allowed elliptic curves
8225 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008226void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008227 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008228{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008229 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008230}
Hanno Becker947194e2017-04-07 13:25:49 +01008231#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008232
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008233#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008235{
Hanno Becker947194e2017-04-07 13:25:49 +01008236 /* Initialize to suppress unnecessary compiler warning */
8237 size_t hostname_len = 0;
8238
8239 /* Check if new hostname is valid before
8240 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008241 if( hostname != NULL )
8242 {
8243 hostname_len = strlen( hostname );
8244
8245 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8247 }
8248
8249 /* Now it's clear that we will overwrite the old hostname,
8250 * so we can free it safely */
8251
8252 if( ssl->hostname != NULL )
8253 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008254 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008255 mbedtls_free( ssl->hostname );
8256 }
8257
8258 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008259
Paul Bakker5121ce52009-01-03 21:22:43 +00008260 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008261 {
8262 ssl->hostname = NULL;
8263 }
8264 else
8265 {
8266 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008267 if( ssl->hostname == NULL )
8268 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008269
Hanno Becker947194e2017-04-07 13:25:49 +01008270 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008271
Hanno Becker947194e2017-04-07 13:25:49 +01008272 ssl->hostname[hostname_len] = '\0';
8273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008274
8275 return( 0 );
8276}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008278
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008279#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008280void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008282 const unsigned char *, size_t),
8283 void *p_sni )
8284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008285 conf->f_sni = f_sni;
8286 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008291int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008292{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008293 size_t cur_len, tot_len;
8294 const char **p;
8295
8296 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008297 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8298 * MUST NOT be truncated."
8299 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008300 */
8301 tot_len = 0;
8302 for( p = protos; *p != NULL; p++ )
8303 {
8304 cur_len = strlen( *p );
8305 tot_len += cur_len;
8306
8307 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008309 }
8310
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008311 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008312
8313 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008314}
8315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008317{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008318 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008321
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008322void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008323{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008324 conf->max_major_ver = major;
8325 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008326}
8327
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008328void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008329{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008330 conf->min_major_ver = major;
8331 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008332}
8333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008335void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008336{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008337 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008338}
8339#endif
8340
Janos Follath088ce432017-04-10 12:42:31 +01008341#if defined(MBEDTLS_SSL_SRV_C)
8342void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8343 char cert_req_ca_list )
8344{
8345 conf->cert_req_ca_list = cert_req_ca_list;
8346}
8347#endif
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008350void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008352 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008353}
8354#endif
8355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008357void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008358{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008359 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008360}
8361#endif
8362
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008363#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008364void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008366 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008367}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008368#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008371int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008374 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008377 }
8378
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008379 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008380
8381 return( 0 );
8382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008386void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008388 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008394{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008395 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008396}
8397#endif
8398
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008399void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008400{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008401 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008402}
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008405void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008408}
8409
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008410void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008413}
8414
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008415void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008416 const unsigned char period[8] )
8417{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008418 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008423#if defined(MBEDTLS_SSL_CLI_C)
8424void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008425{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008426 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008427}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008428#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008429
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008430#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008431void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8432 mbedtls_ssl_ticket_write_t *f_ticket_write,
8433 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8434 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008435{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008436 conf->f_ticket_write = f_ticket_write;
8437 conf->f_ticket_parse = f_ticket_parse;
8438 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008439}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008442
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008443#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8444void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8445 mbedtls_ssl_export_keys_t *f_export_keys,
8446 void *p_export_keys )
8447{
8448 conf->f_export_keys = f_export_keys;
8449 conf->p_export_keys = p_export_keys;
8450}
8451#endif
8452
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008453#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008454void mbedtls_ssl_conf_async_private_cb(
8455 mbedtls_ssl_config *conf,
8456 mbedtls_ssl_async_sign_t *f_async_sign,
8457 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8458 mbedtls_ssl_async_resume_t *f_async_resume,
8459 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008460 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008461{
8462 conf->f_async_sign_start = f_async_sign;
8463 conf->f_async_decrypt_start = f_async_decrypt;
8464 conf->f_async_resume = f_async_resume;
8465 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008466 conf->p_async_config_data = async_config_data;
8467}
8468
Gilles Peskine8f97af72018-04-26 11:46:10 +02008469void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8470{
8471 return( conf->p_async_config_data );
8472}
8473
Gilles Peskine1febfef2018-04-30 11:54:39 +02008474void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008475{
8476 if( ssl->handshake == NULL )
8477 return( NULL );
8478 else
8479 return( ssl->handshake->user_async_ctx );
8480}
8481
Gilles Peskine1febfef2018-04-30 11:54:39 +02008482void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008483 void *ctx )
8484{
8485 if( ssl->handshake != NULL )
8486 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008487}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008488#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008489
Paul Bakker5121ce52009-01-03 21:22:43 +00008490/*
8491 * SSL get accessors
8492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008494{
8495 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8496}
8497
Hanno Becker8b170a02017-10-10 11:51:19 +01008498int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8499{
8500 /*
8501 * Case A: We're currently holding back
8502 * a message for further processing.
8503 */
8504
8505 if( ssl->keep_current_message == 1 )
8506 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008508 return( 1 );
8509 }
8510
8511 /*
8512 * Case B: Further records are pending in the current datagram.
8513 */
8514
8515#if defined(MBEDTLS_SSL_PROTO_DTLS)
8516 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8517 ssl->in_left > ssl->next_record_offset )
8518 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008520 return( 1 );
8521 }
8522#endif /* MBEDTLS_SSL_PROTO_DTLS */
8523
8524 /*
8525 * Case C: A handshake message is being processed.
8526 */
8527
Hanno Becker8b170a02017-10-10 11:51:19 +01008528 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8529 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008531 return( 1 );
8532 }
8533
8534 /*
8535 * Case D: An application data message is being processed
8536 */
8537 if( ssl->in_offt != NULL )
8538 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008540 return( 1 );
8541 }
8542
8543 /*
8544 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008545 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008546 * we implement support for multiple alerts in single records.
8547 */
8548
8549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8550 return( 0 );
8551}
8552
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008553uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008554{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008555 if( ssl->session != NULL )
8556 return( ssl->session->verify_result );
8557
8558 if( ssl->session_negotiate != NULL )
8559 return( ssl->session_negotiate->verify_result );
8560
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008561 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008562}
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008565{
Paul Bakker926c8e42013-03-06 10:23:34 +01008566 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008567 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008570}
8571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008576 {
8577 switch( ssl->minor_ver )
8578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008580 return( "DTLSv1.0" );
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008583 return( "DTLSv1.2" );
8584
8585 default:
8586 return( "unknown (DTLS)" );
8587 }
8588 }
8589#endif
8590
Paul Bakker43ca69c2011-01-15 17:35:19 +00008591 switch( ssl->minor_ver )
8592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008594 return( "SSLv3.0" );
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008597 return( "TLSv1.0" );
8598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008600 return( "TLSv1.1" );
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008603 return( "TLSv1.2" );
8604
Paul Bakker43ca69c2011-01-15 17:35:19 +00008605 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008606 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008607 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008608}
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008611{
Hanno Becker3136ede2018-08-17 15:28:19 +01008612 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008614 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008615
Hanno Becker78640902018-08-13 16:35:15 +01008616 if( transform == NULL )
8617 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_ZLIB_SUPPORT)
8620 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8621 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008622#endif
8623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 case MBEDTLS_MODE_GCM:
8627 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008628 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008630 transform_expansion = transform->minlen;
8631 break;
8632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008634
8635 block_size = mbedtls_cipher_get_block_size(
8636 &transform->cipher_ctx_enc );
8637
Hanno Becker3136ede2018-08-17 15:28:19 +01008638 /* Expansion due to the addition of the MAC. */
8639 transform_expansion += transform->maclen;
8640
8641 /* Expansion due to the addition of CBC padding;
8642 * Theoretically up to 256 bytes, but we never use
8643 * more than the block size of the underlying cipher. */
8644 transform_expansion += block_size;
8645
8646 /* For TLS 1.1 or higher, an explicit IV is added
8647 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008648#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8649 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008650 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008651#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008652
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008653 break;
8654
8655 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008658 }
8659
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008660 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008661}
8662
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008663#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8664size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8665{
8666 size_t max_len;
8667
8668 /*
8669 * Assume mfl_code is correct since it was checked when set
8670 */
Angus Grattond8213d02016-05-25 20:56:48 +10008671 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008672
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008673 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008674 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008675 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008676 {
Angus Grattond8213d02016-05-25 20:56:48 +10008677 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008678 }
8679
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008680 /* During a handshake, use the value being negotiated */
8681 if( ssl->session_negotiate != NULL &&
8682 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8683 {
8684 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8685 }
8686
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008687 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008688}
8689#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8690
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008691#if defined(MBEDTLS_SSL_PROTO_DTLS)
8692static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8693{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008694 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8696 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8697 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8698 return ( 0 );
8699
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008700 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8701 return( ssl->mtu );
8702
8703 if( ssl->mtu == 0 )
8704 return( ssl->handshake->mtu );
8705
8706 return( ssl->mtu < ssl->handshake->mtu ?
8707 ssl->mtu : ssl->handshake->mtu );
8708}
8709#endif /* MBEDTLS_SSL_PROTO_DTLS */
8710
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008711int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8712{
8713 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8714
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008715#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8716 !defined(MBEDTLS_SSL_PROTO_DTLS)
8717 (void) ssl;
8718#endif
8719
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008720#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8721 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8722
8723 if( max_len > mfl )
8724 max_len = mfl;
8725#endif
8726
8727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008728 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008729 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008730 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008731 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8732 const size_t overhead = (size_t) ret;
8733
8734 if( ret < 0 )
8735 return( ret );
8736
8737 if( mtu <= overhead )
8738 {
8739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8740 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8741 }
8742
8743 if( max_len > mtu - overhead )
8744 max_len = mtu - overhead;
8745 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008746#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008747
Hanno Becker0defedb2018-08-10 12:35:02 +01008748#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8749 !defined(MBEDTLS_SSL_PROTO_DTLS)
8750 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008751#endif
8752
8753 return( (int) max_len );
8754}
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_X509_CRT_PARSE_C)
8757const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008758{
8759 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008760 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008761
Hanno Beckere6824572019-02-07 13:18:46 +00008762#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008763 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008764#else
8765 return( NULL );
8766#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008771int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8772 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008773{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008774 if( ssl == NULL ||
8775 dst == NULL ||
8776 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008777 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008780 }
8781
Hanno Becker52055ae2019-02-06 14:30:46 +00008782 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008785
Paul Bakker5121ce52009-01-03 21:22:43 +00008786/*
Paul Bakker1961b702013-01-25 14:49:24 +01008787 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008792
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008793 if( ssl == NULL || ssl->conf == NULL )
8794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008797 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008801 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008803#endif
8804
Paul Bakker1961b702013-01-25 14:49:24 +01008805 return( ret );
8806}
8807
8808/*
8809 * Perform the SSL handshake
8810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008812{
8813 int ret = 0;
8814
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008815 if( ssl == NULL || ssl->conf == NULL )
8816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008823
8824 if( ret != 0 )
8825 break;
8826 }
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008829
8830 return( ret );
8831}
8832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#if defined(MBEDTLS_SSL_RENEGOTIATION)
8834#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008835/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008836 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008839{
8840 int ret;
8841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008843
8844 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8846 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008847
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008848 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008849 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008851 return( ret );
8852 }
8853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008855
8856 return( 0 );
8857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008859
8860/*
8861 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 * - any side: calling mbedtls_ssl_renegotiate(),
8863 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8864 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008865 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008866 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008870{
8871 int ret;
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008874
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008875 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8876 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008877
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008878 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8879 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008883 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008885 ssl->handshake->out_msg_seq = 1;
8886 else
8887 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008888 }
8889#endif
8890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8892 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008897 return( ret );
8898 }
8899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008901
8902 return( 0 );
8903}
8904
8905/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008906 * Renegotiate current connection on client,
8907 * or request renegotiation on server
8908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008912
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008913 if( ssl == NULL || ssl->conf == NULL )
8914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008917 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008924
8925 /* Did we already try/start sending HelloRequest? */
8926 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008928
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008929 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008934 /*
8935 * On client, either start the renegotiation process or,
8936 * if already in progress, continue the handshake
8937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008942
8943 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008946 return( ret );
8947 }
8948 }
8949 else
8950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008954 return( ret );
8955 }
8956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008958
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008959 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008960}
8961
8962/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008963 * Check record counters and renegotiate if they're above the limit.
8964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008966{
Andres AG2196c7f2016-12-15 17:01:16 +00008967 size_t ep_len = ssl_ep_len( ssl );
8968 int in_ctr_cmp;
8969 int out_ctr_cmp;
8970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8972 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008973 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008974 {
8975 return( 0 );
8976 }
8977
Andres AG2196c7f2016-12-15 17:01:16 +00008978 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8979 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008980 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008981 ssl->conf->renego_period + ep_len, 8 - ep_len );
8982
8983 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008984 {
8985 return( 0 );
8986 }
8987
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008992
8993/*
8994 * Receive application data decrypted from the SSL layer
8995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008997{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008998 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008999 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009000
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009001 if( ssl == NULL || ssl->conf == NULL )
9002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009010 return( ret );
9011
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009012 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009014 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009015 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009016 return( ret );
9017 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009018 }
9019#endif
9020
Hanno Becker4a810fb2017-05-24 16:27:30 +01009021 /*
9022 * Check if renegotiation is necessary and/or handshake is
9023 * in process. If yes, perform/continue, and fall through
9024 * if an unexpected packet is received while the client
9025 * is waiting for the ServerHello.
9026 *
9027 * (There is no equivalent to the last condition on
9028 * the server-side as it is not treated as within
9029 * a handshake while waiting for the ClientHello
9030 * after a renegotiation request.)
9031 */
9032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009034 ret = ssl_check_ctr_renegotiate( ssl );
9035 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9036 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009039 return( ret );
9040 }
9041#endif
9042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009046 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9047 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009050 return( ret );
9051 }
9052 }
9053
Hanno Beckere41158b2017-10-23 13:30:32 +01009054 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009055 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009057 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009058 if( ssl->f_get_timer != NULL &&
9059 ssl->f_get_timer( ssl->p_timer ) == -1 )
9060 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009061 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009062 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009063
Hanno Becker327c93b2018-08-15 13:56:18 +01009064 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009065 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009066 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9067 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009068
Hanno Becker4a810fb2017-05-24 16:27:30 +01009069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9070 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009071 }
9072
9073 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009075 {
9076 /*
9077 * OpenSSL sends empty messages to randomize the IV
9078 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009079 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009082 return( 0 );
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009085 return( ret );
9086 }
9087 }
9088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009092
Hanno Becker4a810fb2017-05-24 16:27:30 +01009093 /*
9094 * - For client-side, expect SERVER_HELLO_REQUEST.
9095 * - For server-side, expect CLIENT_HELLO.
9096 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9097 */
9098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009100 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009102 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009105
9106 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009109 {
9110 continue;
9111 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009114 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009115#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009116
Hanno Becker4a810fb2017-05-24 16:27:30 +01009117#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009122
9123 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009126 {
9127 continue;
9128 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009131 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009132#endif /* MBEDTLS_SSL_SRV_C */
9133
Hanno Becker21df7f92017-10-17 11:03:26 +01009134#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009135 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009136 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9137 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9138 ssl->conf->allow_legacy_renegotiation ==
9139 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9140 {
9141 /*
9142 * Accept renegotiation request
9143 */
Paul Bakker48916f92012-09-16 19:57:18 +00009144
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009145 /* DTLS clients need to know renego is server-initiated */
9146#if defined(MBEDTLS_SSL_PROTO_DTLS)
9147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9148 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9149 {
9150 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9151 }
9152#endif
9153 ret = ssl_start_renegotiation( ssl );
9154 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9155 ret != 0 )
9156 {
9157 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9158 return( ret );
9159 }
9160 }
9161 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009162#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009163 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009164 /*
9165 * Refuse renegotiation
9166 */
9167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#if defined(MBEDTLS_SSL_PROTO_SSL3)
9171 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009172 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009173 /* SSLv3 does not have a "no_renegotiation" warning, so
9174 we send a fatal alert and abort the connection. */
9175 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9176 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9177 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009178 }
9179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9181#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9182 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9183 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9186 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9187 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009188 {
9189 return( ret );
9190 }
Paul Bakker48916f92012-09-16 19:57:18 +00009191 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9194 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009198 }
Paul Bakker48916f92012-09-16 19:57:18 +00009199 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009200
Hanno Becker90333da2017-10-10 11:27:13 +01009201 /* At this point, we don't know whether the renegotiation has been
9202 * completed or not. The cases to consider are the following:
9203 * 1) The renegotiation is complete. In this case, no new record
9204 * has been read yet.
9205 * 2) The renegotiation is incomplete because the client received
9206 * an application data record while awaiting the ServerHello.
9207 * 3) The renegotiation is incomplete because the client received
9208 * a non-handshake, non-application data message while awaiting
9209 * the ServerHello.
9210 * In each of these case, looping will be the proper action:
9211 * - For 1), the next iteration will read a new record and check
9212 * if it's application data.
9213 * - For 2), the loop condition isn't satisfied as application data
9214 * is present, hence continue is the same as break
9215 * - For 3), the loop condition is satisfied and read_record
9216 * will re-deliver the message that was held back by the client
9217 * when expecting the ServerHello.
9218 */
9219 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009220 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009221#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009223 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009224 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009225 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009226 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009229 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009231 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009232 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9237 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009240 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009241 }
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009247 }
9248
9249 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009250
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009251 /* We're going to return something now, cancel timer,
9252 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009254 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009255
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009257 /* If we requested renego but received AppData, resend HelloRequest.
9258 * Do it now, after setting in_offt, to avoid taking this branch
9259 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009263 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009264 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009267 return( ret );
9268 }
9269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009271#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009272 }
9273
9274 n = ( len < ssl->in_msglen )
9275 ? len : ssl->in_msglen;
9276
9277 memcpy( buf, ssl->in_offt, n );
9278 ssl->in_msglen -= n;
9279
9280 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009281 {
9282 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009283 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009284 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009286 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009287 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009288 /* more data available */
9289 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009293
Paul Bakker23986e52011-04-24 08:57:21 +00009294 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009295}
9296
9297/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009298 * Send application data to be encrypted by the SSL layer, taking care of max
9299 * fragment length and buffer size.
9300 *
9301 * According to RFC 5246 Section 6.2.1:
9302 *
9303 * Zero-length fragments of Application data MAY be sent as they are
9304 * potentially useful as a traffic analysis countermeasure.
9305 *
9306 * Therefore, it is possible that the input message length is 0 and the
9307 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009308 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009309static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009310 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009311{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009312 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9313 const size_t max_len = (size_t) ret;
9314
9315 if( ret < 0 )
9316 {
9317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9318 return( ret );
9319 }
9320
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009321 if( len > max_len )
9322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009327 "maximum fragment length: %d > %d",
9328 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009330 }
9331 else
9332#endif
9333 len = max_len;
9334 }
Paul Bakker887bd502011-06-08 13:10:54 +00009335
Paul Bakker5121ce52009-01-03 21:22:43 +00009336 if( ssl->out_left != 0 )
9337 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009338 /*
9339 * The user has previously tried to send the data and
9340 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9341 * written. In this case, we expect the high-level write function
9342 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009347 return( ret );
9348 }
9349 }
Paul Bakker887bd502011-06-08 13:10:54 +00009350 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009351 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009352 /*
9353 * The user is trying to send a message the first time, so we need to
9354 * copy the data into the internal buffers and setup the data structure
9355 * to keep track of partial writes
9356 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009357 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009359 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009360
Hanno Becker67bc7c32018-08-06 11:33:50 +01009361 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009364 return( ret );
9365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009366 }
9367
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009368 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009369}
9370
9371/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009372 * Write application data, doing 1/n-1 splitting if necessary.
9373 *
9374 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009375 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009376 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009379static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009380 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009381{
9382 int ret;
9383
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009384 if( ssl->conf->cbc_record_splitting ==
9385 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009386 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9388 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9389 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009390 {
9391 return( ssl_write_real( ssl, buf, len ) );
9392 }
9393
9394 if( ssl->split_done == 0 )
9395 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009396 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009397 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009398 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009399 }
9400
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009401 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9402 return( ret );
9403 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009404
9405 return( ret + 1 );
9406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009408
9409/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009410 * Write application data (public-facing wrapper)
9411 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009412int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009413{
9414 int ret;
9415
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009417
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009418 if( ssl == NULL || ssl->conf == NULL )
9419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9420
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009422 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9423 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009425 return( ret );
9426 }
9427#endif
9428
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009429 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009430 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009431 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009432 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009434 return( ret );
9435 }
9436 }
9437
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009438#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009439 ret = ssl_write_split( ssl, buf, len );
9440#else
9441 ret = ssl_write_real( ssl, buf, len );
9442#endif
9443
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009445
9446 return( ret );
9447}
9448
9449/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009450 * Notify the peer that the connection is being closed
9451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009453{
9454 int ret;
9455
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009456 if( ssl == NULL || ssl->conf == NULL )
9457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009460
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009461 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9467 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9468 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009471 return( ret );
9472 }
9473 }
9474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009476
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009477 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009478}
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009481{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009482 if( transform == NULL )
9483 return;
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009486 deflateEnd( &transform->ctx_deflate );
9487 inflateEnd( &transform->ctx_inflate );
9488#endif
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9491 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 mbedtls_md_free( &transform->md_ctx_enc );
9494 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009495
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009496 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009497}
9498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#if defined(MBEDTLS_X509_CRT_PARSE_C)
9500static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009503
9504 while( cur != NULL )
9505 {
9506 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009508 cur = next;
9509 }
9510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009512
Hanno Becker0271f962018-08-16 13:23:47 +01009513#if defined(MBEDTLS_SSL_PROTO_DTLS)
9514
9515static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9516{
9517 unsigned offset;
9518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9519
9520 if( hs == NULL )
9521 return;
9522
Hanno Becker283f5ef2018-08-24 09:34:47 +01009523 ssl_free_buffered_record( ssl );
9524
Hanno Becker0271f962018-08-16 13:23:47 +01009525 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009526 ssl_buffering_free_slot( ssl, offset );
9527}
9528
9529static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9530 uint8_t slot )
9531{
9532 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9533 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009534
9535 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9536 return;
9537
Hanno Beckere605b192018-08-21 15:59:07 +01009538 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009539 {
Hanno Beckere605b192018-08-21 15:59:07 +01009540 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009541 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009542 mbedtls_free( hs_buf->data );
9543 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009544 }
9545}
9546
9547#endif /* MBEDTLS_SSL_PROTO_DTLS */
9548
Gilles Peskine9b562d52018-04-25 20:32:43 +02009549void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009550{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009551 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9552
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009553 if( handshake == NULL )
9554 return;
9555
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009556#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9557 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9558 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009559 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009560 handshake->async_in_progress = 0;
9561 }
9562#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9563
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009564#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9565 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9566 mbedtls_md5_free( &handshake->fin_md5 );
9567 mbedtls_sha1_free( &handshake->fin_sha1 );
9568#endif
9569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9570#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009571#if defined(MBEDTLS_USE_PSA_CRYPTO)
9572 psa_hash_abort( &handshake->fin_sha256_psa );
9573#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009574 mbedtls_sha256_free( &handshake->fin_sha256 );
9575#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009576#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009577#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009578#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009579 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009580#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009581 mbedtls_sha512_free( &handshake->fin_sha512 );
9582#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009583#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_DHM_C)
9587 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#if defined(MBEDTLS_ECDH_C)
9590 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009591#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009592#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009593 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009594#if defined(MBEDTLS_SSL_CLI_C)
9595 mbedtls_free( handshake->ecjpake_cache );
9596 handshake->ecjpake_cache = NULL;
9597 handshake->ecjpake_cache_len = 0;
9598#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009599#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009600
Janos Follath4ae5c292016-02-10 11:27:43 +00009601#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9602 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009603 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009605#endif
9606
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009607#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9608 if( handshake->psk != NULL )
9609 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009610 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009611 mbedtls_free( handshake->psk );
9612 }
9613#endif
9614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9616 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009617 /*
9618 * Free only the linked list wrapper, not the keys themselves
9619 * since the belong to the SNI callback
9620 */
9621 if( handshake->sni_key_cert != NULL )
9622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009624
9625 while( cur != NULL )
9626 {
9627 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009629 cur = next;
9630 }
9631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009633
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009634#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009635 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009636 if( handshake->ecrs_peer_cert != NULL )
9637 {
9638 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9639 mbedtls_free( handshake->ecrs_peer_cert );
9640 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009641#endif
9642
Hanno Becker75173122019-02-06 16:18:31 +00009643#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9644 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9645 mbedtls_pk_free( &handshake->peer_pubkey );
9646#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648#if defined(MBEDTLS_SSL_PROTO_DTLS)
9649 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009650 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009651 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009652#endif
9653
Hanno Becker4a63ed42019-01-08 11:39:35 +00009654#if defined(MBEDTLS_ECDH_C) && \
9655 defined(MBEDTLS_USE_PSA_CRYPTO)
9656 psa_destroy_key( handshake->ecdh_psa_privkey );
9657#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9658
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009659 mbedtls_platform_zeroize( handshake,
9660 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009661}
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009664{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009665 if( session == NULL )
9666 return;
9667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009669 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009670#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009671
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009672#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009674#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009675
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009676 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009677}
9678
Paul Bakker5121ce52009-01-03 21:22:43 +00009679/*
9680 * Free an SSL context
9681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009683{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009684 if( ssl == NULL )
9685 return;
9686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009688
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009689 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009690 {
Angus Grattond8213d02016-05-25 20:56:48 +10009691 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009693 }
9694
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009695 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009696 {
Angus Grattond8213d02016-05-25 20:56:48 +10009697 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009699 }
9700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009702 if( ssl->compress_buf != NULL )
9703 {
Angus Grattond8213d02016-05-25 20:56:48 +10009704 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009706 }
9707#endif
9708
Paul Bakker48916f92012-09-16 19:57:18 +00009709 if( ssl->transform )
9710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 mbedtls_ssl_transform_free( ssl->transform );
9712 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009713 }
9714
9715 if( ssl->handshake )
9716 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009717 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9719 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 mbedtls_free( ssl->handshake );
9722 mbedtls_free( ssl->transform_negotiate );
9723 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009724 }
9725
Paul Bakkerc0463502013-02-14 11:19:38 +01009726 if( ssl->session )
9727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 mbedtls_ssl_session_free( ssl->session );
9729 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009730 }
9731
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009733 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009734 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009735 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009737 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009738#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9741 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9744 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009745 }
9746#endif
9747
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009748#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009750#endif
9751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009753
Paul Bakker86f04f42013-02-14 11:20:09 +01009754 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009755 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009756}
9757
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009758/*
9759 * Initialze mbedtls_ssl_config
9760 */
9761void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9762{
9763 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9764}
9765
Simon Butcherc97b6972015-12-27 23:48:17 +00009766#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009767static int ssl_preset_default_hashes[] = {
9768#if defined(MBEDTLS_SHA512_C)
9769 MBEDTLS_MD_SHA512,
9770 MBEDTLS_MD_SHA384,
9771#endif
9772#if defined(MBEDTLS_SHA256_C)
9773 MBEDTLS_MD_SHA256,
9774 MBEDTLS_MD_SHA224,
9775#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009776#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009777 MBEDTLS_MD_SHA1,
9778#endif
9779 MBEDTLS_MD_NONE
9780};
Simon Butcherc97b6972015-12-27 23:48:17 +00009781#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009782
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009783static int ssl_preset_suiteb_ciphersuites[] = {
9784 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9785 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9786 0
9787};
9788
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009789#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009790static int ssl_preset_suiteb_hashes[] = {
9791 MBEDTLS_MD_SHA256,
9792 MBEDTLS_MD_SHA384,
9793 MBEDTLS_MD_NONE
9794};
9795#endif
9796
9797#if defined(MBEDTLS_ECP_C)
9798static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9799 MBEDTLS_ECP_DP_SECP256R1,
9800 MBEDTLS_ECP_DP_SECP384R1,
9801 MBEDTLS_ECP_DP_NONE
9802};
9803#endif
9804
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009805/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009806 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009807 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009808int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009809 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009810{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009811#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009812 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009813#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009814
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009815 /* Use the functions here so that they are covered in tests,
9816 * but otherwise access member directly for efficiency */
9817 mbedtls_ssl_conf_endpoint( conf, endpoint );
9818 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009819
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009820 /*
9821 * Things that are common to all presets
9822 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009823#if defined(MBEDTLS_SSL_CLI_C)
9824 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9825 {
9826 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9828 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9829#endif
9830 }
9831#endif
9832
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009833#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009834 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009835#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009836
9837#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9838 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9839#endif
9840
9841#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9842 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9843#endif
9844
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009845#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9846 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9847#endif
9848
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009849#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009850 conf->f_cookie_write = ssl_cookie_write_dummy;
9851 conf->f_cookie_check = ssl_cookie_check_dummy;
9852#endif
9853
9854#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9855 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9856#endif
9857
Janos Follath088ce432017-04-10 12:42:31 +01009858#if defined(MBEDTLS_SSL_SRV_C)
9859 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9860#endif
9861
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009862#if defined(MBEDTLS_SSL_PROTO_DTLS)
9863 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9864 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9865#endif
9866
9867#if defined(MBEDTLS_SSL_RENEGOTIATION)
9868 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009869 memset( conf->renego_period, 0x00, 2 );
9870 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009871#endif
9872
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009873#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9874 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9875 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009876 const unsigned char dhm_p[] =
9877 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9878 const unsigned char dhm_g[] =
9879 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9880
Hanno Beckera90658f2017-10-04 15:29:08 +01009881 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9882 dhm_p, sizeof( dhm_p ),
9883 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009884 {
9885 return( ret );
9886 }
9887 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009888#endif
9889
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009890 /*
9891 * Preset-specific defaults
9892 */
9893 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009894 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009895 /*
9896 * NSA Suite B
9897 */
9898 case MBEDTLS_SSL_PRESET_SUITEB:
9899 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9900 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9901 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9902 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9903
9904 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9905 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9906 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9907 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9908 ssl_preset_suiteb_ciphersuites;
9909
9910#if defined(MBEDTLS_X509_CRT_PARSE_C)
9911 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009912#endif
9913
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009914#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009915 conf->sig_hashes = ssl_preset_suiteb_hashes;
9916#endif
9917
9918#if defined(MBEDTLS_ECP_C)
9919 conf->curve_list = ssl_preset_suiteb_curves;
9920#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009921 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009922
9923 /*
9924 * Default
9925 */
9926 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009927 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9928 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9929 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9930 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9931 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9932 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9933 MBEDTLS_SSL_MIN_MINOR_VERSION :
9934 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009935 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9936 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9937
9938#if defined(MBEDTLS_SSL_PROTO_DTLS)
9939 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9940 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9941#endif
9942
9943 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9944 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9945 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9946 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9947 mbedtls_ssl_list_ciphersuites();
9948
9949#if defined(MBEDTLS_X509_CRT_PARSE_C)
9950 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9951#endif
9952
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009953#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009954 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009955#endif
9956
9957#if defined(MBEDTLS_ECP_C)
9958 conf->curve_list = mbedtls_ecp_grp_id_list();
9959#endif
9960
9961#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9962 conf->dhm_min_bitlen = 1024;
9963#endif
9964 }
9965
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009966 return( 0 );
9967}
9968
9969/*
9970 * Free mbedtls_ssl_config
9971 */
9972void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9973{
9974#if defined(MBEDTLS_DHM_C)
9975 mbedtls_mpi_free( &conf->dhm_P );
9976 mbedtls_mpi_free( &conf->dhm_G );
9977#endif
9978
9979#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9980 if( conf->psk != NULL )
9981 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009982 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009983 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009984 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009985 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009986 }
9987
9988 if( conf->psk_identity != NULL )
9989 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009990 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009991 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009992 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009993 conf->psk_identity_len = 0;
9994 }
9995#endif
9996
9997#if defined(MBEDTLS_X509_CRT_PARSE_C)
9998 ssl_key_cert_free( conf->key_cert );
9999#endif
10000
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010001 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010002}
10003
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010004#if defined(MBEDTLS_PK_C) && \
10005 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010006/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011#if defined(MBEDTLS_RSA_C)
10012 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10013 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015#if defined(MBEDTLS_ECDSA_C)
10016 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10017 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010020}
10021
Hanno Becker7e5437a2017-04-28 17:15:26 +010010022unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10023{
10024 switch( type ) {
10025 case MBEDTLS_PK_RSA:
10026 return( MBEDTLS_SSL_SIG_RSA );
10027 case MBEDTLS_PK_ECDSA:
10028 case MBEDTLS_PK_ECKEY:
10029 return( MBEDTLS_SSL_SIG_ECDSA );
10030 default:
10031 return( MBEDTLS_SSL_SIG_ANON );
10032 }
10033}
10034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010036{
10037 switch( sig )
10038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039#if defined(MBEDTLS_RSA_C)
10040 case MBEDTLS_SSL_SIG_RSA:
10041 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#if defined(MBEDTLS_ECDSA_C)
10044 case MBEDTLS_SSL_SIG_ECDSA:
10045 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010046#endif
10047 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010049 }
10050}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010051#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010052
Hanno Becker7e5437a2017-04-28 17:15:26 +010010053#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10054 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10055
10056/* Find an entry in a signature-hash set matching a given hash algorithm. */
10057mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10058 mbedtls_pk_type_t sig_alg )
10059{
10060 switch( sig_alg )
10061 {
10062 case MBEDTLS_PK_RSA:
10063 return( set->rsa );
10064 case MBEDTLS_PK_ECDSA:
10065 return( set->ecdsa );
10066 default:
10067 return( MBEDTLS_MD_NONE );
10068 }
10069}
10070
10071/* Add a signature-hash-pair to a signature-hash set */
10072void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10073 mbedtls_pk_type_t sig_alg,
10074 mbedtls_md_type_t md_alg )
10075{
10076 switch( sig_alg )
10077 {
10078 case MBEDTLS_PK_RSA:
10079 if( set->rsa == MBEDTLS_MD_NONE )
10080 set->rsa = md_alg;
10081 break;
10082
10083 case MBEDTLS_PK_ECDSA:
10084 if( set->ecdsa == MBEDTLS_MD_NONE )
10085 set->ecdsa = md_alg;
10086 break;
10087
10088 default:
10089 break;
10090 }
10091}
10092
10093/* Allow exactly one hash algorithm for each signature. */
10094void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10095 mbedtls_md_type_t md_alg )
10096{
10097 set->rsa = md_alg;
10098 set->ecdsa = md_alg;
10099}
10100
10101#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10102 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10103
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010104/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010105 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010108{
10109 switch( hash )
10110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#if defined(MBEDTLS_MD5_C)
10112 case MBEDTLS_SSL_HASH_MD5:
10113 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115#if defined(MBEDTLS_SHA1_C)
10116 case MBEDTLS_SSL_HASH_SHA1:
10117 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119#if defined(MBEDTLS_SHA256_C)
10120 case MBEDTLS_SSL_HASH_SHA224:
10121 return( MBEDTLS_MD_SHA224 );
10122 case MBEDTLS_SSL_HASH_SHA256:
10123 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#if defined(MBEDTLS_SHA512_C)
10126 case MBEDTLS_SSL_HASH_SHA384:
10127 return( MBEDTLS_MD_SHA384 );
10128 case MBEDTLS_SSL_HASH_SHA512:
10129 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010130#endif
10131 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010133 }
10134}
10135
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010136/*
10137 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10138 */
10139unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10140{
10141 switch( md )
10142 {
10143#if defined(MBEDTLS_MD5_C)
10144 case MBEDTLS_MD_MD5:
10145 return( MBEDTLS_SSL_HASH_MD5 );
10146#endif
10147#if defined(MBEDTLS_SHA1_C)
10148 case MBEDTLS_MD_SHA1:
10149 return( MBEDTLS_SSL_HASH_SHA1 );
10150#endif
10151#if defined(MBEDTLS_SHA256_C)
10152 case MBEDTLS_MD_SHA224:
10153 return( MBEDTLS_SSL_HASH_SHA224 );
10154 case MBEDTLS_MD_SHA256:
10155 return( MBEDTLS_SSL_HASH_SHA256 );
10156#endif
10157#if defined(MBEDTLS_SHA512_C)
10158 case MBEDTLS_MD_SHA384:
10159 return( MBEDTLS_SSL_HASH_SHA384 );
10160 case MBEDTLS_MD_SHA512:
10161 return( MBEDTLS_SSL_HASH_SHA512 );
10162#endif
10163 default:
10164 return( MBEDTLS_SSL_HASH_NONE );
10165 }
10166}
10167
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010168#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010169/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010170 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010171 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010172 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010173int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010176
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010177 if( ssl->conf->curve_list == NULL )
10178 return( -1 );
10179
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010180 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010181 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010182 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010183
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010184 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010185}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010186#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010187
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010188#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010189/*
10190 * Check if a hash proposed by the peer is in our list.
10191 * Return 0 if we're willing to use it, -1 otherwise.
10192 */
10193int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10194 mbedtls_md_type_t md )
10195{
10196 const int *cur;
10197
10198 if( ssl->conf->sig_hashes == NULL )
10199 return( -1 );
10200
10201 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10202 if( *cur == (int) md )
10203 return( 0 );
10204
10205 return( -1 );
10206}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010207#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209#if defined(MBEDTLS_X509_CRT_PARSE_C)
10210int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10211 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010212 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010213 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010214{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010215 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010217 int usage = 0;
10218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010220 const char *ext_oid;
10221 size_t ext_len;
10222#endif
10223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10225 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010226 ((void) cert);
10227 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010228 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010229#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10232 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010233 {
10234 /* Server part of the key exchange */
10235 switch( ciphersuite->key_exchange )
10236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 case MBEDTLS_KEY_EXCHANGE_RSA:
10238 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010239 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010240 break;
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10243 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10244 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10245 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010246 break;
10247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10249 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010250 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010251 break;
10252
10253 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 case MBEDTLS_KEY_EXCHANGE_NONE:
10255 case MBEDTLS_KEY_EXCHANGE_PSK:
10256 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10257 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010258 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010259 usage = 0;
10260 }
10261 }
10262 else
10263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10265 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010266 }
10267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010269 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010270 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010271 ret = -1;
10272 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010273#else
10274 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10278 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10281 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010282 }
10283 else
10284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10286 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010287 }
10288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 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_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010292 ret = -1;
10293 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010295
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010296 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010299
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010300/*
10301 * Convert version numbers to/from wire format
10302 * and, for DTLS, to/from TLS equivalent.
10303 *
10304 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010305 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010306 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10307 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010310 unsigned char ver[2] )
10311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312#if defined(MBEDTLS_SSL_PROTO_DTLS)
10313 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010316 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10317
10318 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10319 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10320 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010321 else
10322#else
10323 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010324#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010325 {
10326 ver[0] = (unsigned char) major;
10327 ver[1] = (unsigned char) minor;
10328 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010329}
10330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010332 const unsigned char ver[2] )
10333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334#if defined(MBEDTLS_SSL_PROTO_DTLS)
10335 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010336 {
10337 *major = 255 - ver[0] + 2;
10338 *minor = 255 - ver[1] + 1;
10339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010341 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10342 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010343 else
10344#else
10345 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010346#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010347 {
10348 *major = ver[0];
10349 *minor = ver[1];
10350 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010351}
10352
Simon Butcher99000142016-10-13 17:21:01 +010010353int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10354{
10355#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10356 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10357 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10358
10359 switch( md )
10360 {
10361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10362#if defined(MBEDTLS_MD5_C)
10363 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010364 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010365#endif
10366#if defined(MBEDTLS_SHA1_C)
10367 case MBEDTLS_SSL_HASH_SHA1:
10368 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10369 break;
10370#endif
10371#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10372#if defined(MBEDTLS_SHA512_C)
10373 case MBEDTLS_SSL_HASH_SHA384:
10374 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10375 break;
10376#endif
10377#if defined(MBEDTLS_SHA256_C)
10378 case MBEDTLS_SSL_HASH_SHA256:
10379 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10380 break;
10381#endif
10382 default:
10383 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10384 }
10385
10386 return 0;
10387#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10388 (void) ssl;
10389 (void) md;
10390
10391 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10392#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10393}
10394
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010395#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10396 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10397int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10398 unsigned char *output,
10399 unsigned char *data, size_t data_len )
10400{
10401 int ret = 0;
10402 mbedtls_md5_context mbedtls_md5;
10403 mbedtls_sha1_context mbedtls_sha1;
10404
10405 mbedtls_md5_init( &mbedtls_md5 );
10406 mbedtls_sha1_init( &mbedtls_sha1 );
10407
10408 /*
10409 * digitally-signed struct {
10410 * opaque md5_hash[16];
10411 * opaque sha_hash[20];
10412 * };
10413 *
10414 * md5_hash
10415 * MD5(ClientHello.random + ServerHello.random
10416 * + ServerParams);
10417 * sha_hash
10418 * SHA(ClientHello.random + ServerHello.random
10419 * + ServerParams);
10420 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010421 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010422 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010424 goto exit;
10425 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010426 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010427 ssl->handshake->randbytes, 64 ) ) != 0 )
10428 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010430 goto exit;
10431 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010432 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010433 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010435 goto exit;
10436 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010437 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010438 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010440 goto exit;
10441 }
10442
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010443 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010444 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010446 goto exit;
10447 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010448 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010449 ssl->handshake->randbytes, 64 ) ) != 0 )
10450 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010452 goto exit;
10453 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010454 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010455 data_len ) ) != 0 )
10456 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010458 goto exit;
10459 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010460 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010461 output + 16 ) ) != 0 )
10462 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010464 goto exit;
10465 }
10466
10467exit:
10468 mbedtls_md5_free( &mbedtls_md5 );
10469 mbedtls_sha1_free( &mbedtls_sha1 );
10470
10471 if( ret != 0 )
10472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10473 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10474
10475 return( ret );
10476
10477}
10478#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10479 MBEDTLS_SSL_PROTO_TLS1_1 */
10480
10481#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10482 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010483
10484#if defined(MBEDTLS_USE_PSA_CRYPTO)
10485int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10486 unsigned char *hash, size_t *hashlen,
10487 unsigned char *data, size_t data_len,
10488 mbedtls_md_type_t md_alg )
10489{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010490 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010491 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010492 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10493
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010495
10496 if( ( status = psa_hash_setup( &hash_operation,
10497 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010498 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010499 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010500 goto exit;
10501 }
10502
Andrzej Kurek814feff2019-01-14 04:35:19 -050010503 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10504 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010505 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010506 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010507 goto exit;
10508 }
10509
Andrzej Kurek814feff2019-01-14 04:35:19 -050010510 if( ( status = psa_hash_update( &hash_operation,
10511 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010512 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010513 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010514 goto exit;
10515 }
10516
Andrzej Kurek814feff2019-01-14 04:35:19 -050010517 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10518 hashlen ) ) != 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_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010521 goto exit;
10522 }
10523
10524exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010525 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010526 {
10527 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10528 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010529 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010530 {
10531 case PSA_ERROR_NOT_SUPPORTED:
10532 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010533 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010534 case PSA_ERROR_BUFFER_TOO_SMALL:
10535 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10536 case PSA_ERROR_INSUFFICIENT_MEMORY:
10537 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10538 default:
10539 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10540 }
10541 }
10542 return( 0 );
10543}
10544
10545#else
10546
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010547int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010548 unsigned char *hash, size_t *hashlen,
10549 unsigned char *data, size_t data_len,
10550 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010551{
10552 int ret = 0;
10553 mbedtls_md_context_t ctx;
10554 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010555 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010556
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010558
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010559 mbedtls_md_init( &ctx );
10560
10561 /*
10562 * digitally-signed struct {
10563 * opaque client_random[32];
10564 * opaque server_random[32];
10565 * ServerDHParams params;
10566 * };
10567 */
10568 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10569 {
10570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10571 goto exit;
10572 }
10573 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10574 {
10575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10576 goto exit;
10577 }
10578 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10579 {
10580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10581 goto exit;
10582 }
10583 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10584 {
10585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10586 goto exit;
10587 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010588 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010589 {
10590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10591 goto exit;
10592 }
10593
10594exit:
10595 mbedtls_md_free( &ctx );
10596
10597 if( ret != 0 )
10598 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10599 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10600
10601 return( ret );
10602}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010603#endif /* MBEDTLS_USE_PSA_CRYPTO */
10604
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010605#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10606 MBEDTLS_SSL_PROTO_TLS1_2 */
10607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#endif /* MBEDTLS_SSL_TLS_C */