blob: f0f38c0b2b99a7b7701d1b0bf8713e24320e922e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000289
290#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291 if( src->peer_cert != NULL )
292 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200293 int ret;
294
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200295 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200302 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 dst->peer_cert = NULL;
306 return( ret );
307 }
308 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000310 if( src->peer_cert_digest != NULL )
311 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000312 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000313 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000314 if( dst->peer_cert_digest == NULL )
315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
316
317 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
318 src->peer_cert_digest_len );
319 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000320 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000321 }
322#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200325
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200326#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200327 if( src->ticket != NULL )
328 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200329 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200330 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200332
333 memcpy( dst->ticket, src->ticket, src->ticket_len );
334 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200335#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200336
337 return( 0 );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519static int tls_prf_generic( mbedtls_md_type_t md_type,
520 const unsigned char *secret, size_t slen,
521 const char *label,
522 const unsigned char *random, size_t rlen,
523 unsigned char *dstbuf, size_t dlen )
524{
525 psa_status_t status;
526 psa_algorithm_t alg;
527 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500528 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500529 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
530
Andrzej Kurek2f760752019-01-28 08:08:15 -0500531 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500533
Andrzej Kurekc929a822019-01-14 03:51:11 -0500534 if( md_type == MBEDTLS_MD_SHA384 )
535 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
536 else
537 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
538
Andrzej Kurek2f760752019-01-28 08:08:15 -0500539 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500540 psa_key_policy_set_usage( &policy,
541 PSA_KEY_USAGE_DERIVE,
542 alg );
543 status = psa_set_key_policy( master_slot, &policy );
544 if( status != PSA_SUCCESS )
545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
546
547 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
548 if( status != PSA_SUCCESS )
549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
550
551 status = psa_key_derivation( &generator,
552 master_slot, alg,
553 random, rlen,
554 (unsigned char const *) label,
555 (size_t) strlen( label ),
556 dlen );
557 if( status != PSA_SUCCESS )
558 {
559 psa_generator_abort( &generator );
560 psa_destroy_key( master_slot );
561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
562 }
563
564 status = psa_generator_read( &generator, dstbuf, dlen );
565 if( status != PSA_SUCCESS )
566 {
567 psa_generator_abort( &generator );
568 psa_destroy_key( master_slot );
569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
570 }
571
572 status = psa_generator_abort( &generator );
573 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500574 {
575 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500576 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500577 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500578
579 status = psa_destroy_key( master_slot );
580 if( status != PSA_SUCCESS )
581 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
582
Andrzej Kurek33171262019-01-15 03:25:18 -0500583 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500584}
585
586#else /* MBEDTLS_USE_PSA_CRYPTO */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 const unsigned char *secret, size_t slen,
590 const char *label,
591 const unsigned char *random, size_t rlen,
592 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593{
594 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
598 const mbedtls_md_info_t *md_info;
599 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 int ret;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
609 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611
612 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 memcpy( tmp + md_len, label, nb );
614 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 nb += rlen;
616
617 /*
618 * Compute P_<hash>(secret, label + random)[0..dlen]
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
624 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
625 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100626
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
635 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
646 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647
648 return( 0 );
649}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652static int tls_prf_sha256( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663static int tls_prf_sha384( const unsigned char *secret, size_t slen,
664 const char *label,
665 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 unsigned char *dstbuf, size_t dlen )
667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_SHA512_C */
672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
677 defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker380da532012-04-18 16:10:25 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_SSL3)
682static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
683static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
687static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
688static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200689#endif
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
692#if defined(MBEDTLS_SHA256_C)
693static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
694static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
695static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
699static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
700static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
701static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Hanno Becker7d0a5692018-10-23 15:26:22 +0100705#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
706 defined(MBEDTLS_USE_PSA_CRYPTO)
707static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
708{
709 if( ssl->conf->f_psk != NULL )
710 {
711 /* If we've used a callback to select the PSK,
712 * the static configuration is irrelevant. */
713 if( ssl->handshake->psk_opaque != 0 )
714 return( 1 );
715
716 return( 0 );
717 }
718
719 if( ssl->conf->psk_opaque != 0 )
720 return( 1 );
721
722 return( 0 );
723}
724#endif /* MBEDTLS_USE_PSA_CRYPTO &&
725 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000728{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200729 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000730#if defined(MBEDTLS_USE_PSA_CRYPTO)
731 int psa_fallthrough;
732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 unsigned char keyblk[256];
735 unsigned char *key1;
736 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100737 unsigned char *mac_enc;
738 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000739 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000741 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 const mbedtls_cipher_info_t *cipher_info;
744 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100745
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000746 /* cf. RFC 5246, Section 8.1:
747 * "The master secret is always exactly 48 bytes in length." */
748 size_t const master_secret_len = 48;
749
Hanno Becker35b23c72018-10-23 12:10:41 +0100750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
751 unsigned char session_hash[48];
752#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_ssl_session *session = ssl->session_negotiate;
755 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Hanno Beckere694c3e2017-12-27 21:34:08 +0000760
761 ciphersuite_info = handshake->ciphersuite_info;
762 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100763 if( cipher_info == NULL )
764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000766 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100768 }
769
Hanno Beckere694c3e2017-12-27 21:34:08 +0000770 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100771 if( md_info == NULL )
772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000774 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100776 }
777
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_SSL3)
782 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 {
Paul Bakker48916f92012-09-16 19:57:18 +0000784 handshake->tls_prf = ssl3_prf;
785 handshake->calc_verify = ssl_calc_verify_ssl;
786 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788 else
789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
791 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 {
Paul Bakker48916f92012-09-16 19:57:18 +0000793 handshake->tls_prf = tls1_prf;
794 handshake->calc_verify = ssl_calc_verify_tls;
795 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797 else
798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
800#if defined(MBEDTLS_SHA512_C)
801 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000802 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803 {
Paul Bakker48916f92012-09-16 19:57:18 +0000804 handshake->tls_prf = tls_prf_sha384;
805 handshake->calc_verify = ssl_calc_verify_tls_sha384;
806 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000808 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SHA256_C)
811 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000812 {
Paul Bakker48916f92012-09-16 19:57:18 +0000813 handshake->tls_prf = tls_prf_sha256;
814 handshake->calc_verify = ssl_calc_verify_tls_sha256;
815 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200817 else
818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200823 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000824
825 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 * SSLv3:
827 * master =
828 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
829 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
830 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200831 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200832 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 * master = PRF( premaster, "master secret", randbytes )[0..47]
834 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100835 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
838 }
839 else
840 {
841 /* The label for the KDF used for key expansion.
842 * This is either "master secret" or "extended master secret"
843 * depending on whether the Extended Master Secret extension
844 * is used. */
845 char const *lbl = "master secret";
846
847 /* The salt for the KDF used for key expansion.
848 * - If the Extended Master Secret extension is not used,
849 * this is ClientHello.Random + ServerHello.Random
850 * (see Sect. 8.1 in RFC 5246).
851 * - If the Extended Master Secret extension is used,
852 * this is the transcript of the handshake so far.
853 * (see Sect. 4 in RFC 7627). */
854 unsigned char const *salt = handshake->randbytes;
855 size_t salt_len = 64;
856
857#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861
Hanno Becker35b23c72018-10-23 12:10:41 +0100862 lbl = "extended master secret";
863 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200864 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000869 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
870 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100871 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000872 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200873 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100874#endif /* MBEDTLS_SHA512_C */
875 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100879 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880
Hanno Becker35b23c72018-10-23 12:10:41 +0100881 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200882 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100883#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
884
Hanno Becker7d0a5692018-10-23 15:26:22 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
886 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
887 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
888 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
889 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100890 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 /* Perform PSK-to-MS expansion in a single step. */
892 psa_status_t status;
893 psa_algorithm_t alg;
894 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500895 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100896
897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
898
899 psk = ssl->conf->psk_opaque;
900 if( ssl->handshake->psk_opaque != 0 )
901 psk = ssl->handshake->psk_opaque;
902
903 if( md_type == MBEDTLS_MD_SHA384 )
904 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
905 else
906 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
907
908 status = psa_key_derivation( &generator, psk, alg,
909 salt, salt_len,
910 (unsigned char const *) lbl,
911 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000912 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100913 if( status != PSA_SUCCESS )
914 {
915 psa_generator_abort( &generator );
916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
917 }
918
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000919 status = psa_generator_read( &generator, session->master,
920 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921 if( status != PSA_SUCCESS )
922 {
923 psa_generator_abort( &generator );
924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
925 }
926
927 status = psa_generator_abort( &generator );
928 if( status != PSA_SUCCESS )
929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100930 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100931 else
932#endif
933 {
934 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
935 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000936 session->master,
937 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100938 if( ret != 0 )
939 {
940 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
941 return( ret );
942 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200943
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
945 handshake->premaster,
946 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 mbedtls_platform_zeroize( handshake->premaster,
949 sizeof(handshake->premaster) );
950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 /*
954 * Swap the client and server random values.
955 */
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( tmp, handshake->randbytes, 64 );
957 memcpy( handshake->randbytes, tmp + 32, 32 );
958 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500959 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 /*
962 * SSLv3:
963 * key block =
964 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
965 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
966 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
967 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
968 * ...
969 *
970 * TLSv1:
971 * key block = PRF( master, "key expansion", randbytes )
972 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100973 ret = handshake->tls_prf( session->master, 48, "key expansion",
974 handshake->randbytes, 64, keyblk, 256 );
975 if( ret != 0 )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100978 return( ret );
979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
982 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
984 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
985 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500987 mbedtls_platform_zeroize( handshake->randbytes,
988 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Determine the appropriate key, IV and MAC length.
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993
Hanno Becker88aaf652017-12-27 08:17:40 +0000994 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200997 cipher_info->mode == MBEDTLS_MODE_CCM ||
998 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001000 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001002 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001004 transform->taglen =
1005 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001006
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001007 /* All modes haves 96-bit IVs;
1008 * GCM and CCM has 4 implicit and 8 explicit bytes
1009 * ChachaPoly has all 12 bytes implicit
1010 */
Paul Bakker68884e32013-01-07 18:20:04 +01001011 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001012 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1013 transform->fixed_ivlen = 12;
1014 else
1015 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001017 /* Minimum length of encrypted record */
1018 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001020 }
1021 else
1022 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1025 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001028 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001032 mac_key_len = mbedtls_md_get_size( md_info );
1033 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001036 /*
1037 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1038 * (rfc 6066 page 13 or rfc 2104 section 4),
1039 * so we only need to adjust the length here.
1040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001044
1045#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1046 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001047 * HMAC implementation which also truncates the key
1048 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001049 mac_key_len = transform->maclen;
1050#endif
1051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001053
1054 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001055 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001057 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001059 transform->minlen = transform->maclen;
1060 else
Paul Bakker68884e32013-01-07 18:20:04 +01001061 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001062 /*
1063 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001064 * 1. if EtM is in use: one block plus MAC
1065 * otherwise: * first multiple of blocklen greater than maclen
1066 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1069 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001070 {
1071 transform->minlen = transform->maclen
1072 + cipher_info->block_size;
1073 }
1074 else
1075#endif
1076 {
1077 transform->minlen = transform->maclen
1078 + cipher_info->block_size
1079 - transform->maclen % cipher_info->block_size;
1080 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1083 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1084 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001085 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001086 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1089 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1090 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001091 {
1092 transform->minlen += transform->ivlen;
1093 }
1094 else
1095#endif
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001099 }
Paul Bakker68884e32013-01-07 18:20:04 +01001100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 }
1102
Hanno Becker88aaf652017-12-27 08:17:40 +00001103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1104 (unsigned) keylen,
1105 (unsigned) transform->minlen,
1106 (unsigned) transform->ivlen,
1107 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
1109 /*
1110 * Finally setup the cipher contexts, IVs and MAC secrets.
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001115 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001116 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker68884e32013-01-07 18:20:04 +01001118 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001119 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001121 /*
1122 * This is not used in TLS v1.1.
1123 */
Paul Bakker48916f92012-09-16 19:57:18 +00001124 iv_copy_len = ( transform->fixed_ivlen ) ?
1125 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001126 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1127 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
1130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_SSL_CLI_C */
1132#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001133 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001135 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Hanno Becker81c7b182017-11-09 18:39:33 +00001138 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001139 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001141 /*
1142 * This is not used in TLS v1.1.
1143 */
Paul Bakker48916f92012-09-16 19:57:18 +00001144 iv_copy_len = ( transform->fixed_ivlen ) ?
1145 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001146 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1147 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001148 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001150 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001159 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001160 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001164 }
1165
Hanno Becker81c7b182017-11-09 18:39:33 +00001166 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1167 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001168 }
1169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1172 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1173 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001174 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001175 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1176 For AEAD-based ciphersuites, there is nothing to do here. */
1177 if( mac_key_len != 0 )
1178 {
1179 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1180 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1181 }
Paul Bakker68884e32013-01-07 18:20:04 +01001182 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001183 else
1184#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1191 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001192 {
1193 int ret = 0;
1194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001196
Hanno Becker88aaf652017-12-27 08:17:40 +00001197 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001198 transform->iv_enc, transform->iv_dec,
1199 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001200 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001201 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1204 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001205 }
1206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001208
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001209#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1210 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001211 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001212 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1213 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001214 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001215 iv_copy_len );
1216 }
1217#endif
1218
Hanno Beckerf704bef2018-11-16 15:21:18 +00001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001220
1221 /* Only use PSA-based ciphers for TLS-1.2.
1222 * That's relevant at least for TLS-1.0, where
1223 * we assume that mbedtls_cipher_crypt() updates
1224 * the structure field for the IV, which the PSA-based
1225 * implementation currently doesn't. */
1226#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1227 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001228 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001229 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1230 cipher_info, taglen );
1231 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1232 {
1233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1234 return( ret );
1235 }
1236
1237 if( ret == 0 )
1238 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001240 psa_fallthrough = 0;
1241 }
1242 else
1243 {
1244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1245 psa_fallthrough = 1;
1246 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001247 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001249 psa_fallthrough = 1;
1250#else
1251 psa_fallthrough = 1;
1252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001253
Hanno Beckercb1cc802018-11-17 22:27:38 +00001254 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001256 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001257 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001260 return( ret );
1261 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001262
Hanno Beckerf704bef2018-11-16 15:21:18 +00001263#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001264 /* Only use PSA-based ciphers for TLS-1.2.
1265 * That's relevant at least for TLS-1.0, where
1266 * we assume that mbedtls_cipher_crypt() updates
1267 * the structure field for the IV, which the PSA-based
1268 * implementation currently doesn't. */
1269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1270 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001271 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001272 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1273 cipher_info, taglen );
1274 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1275 {
1276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1277 return( ret );
1278 }
1279
1280 if( ret == 0 )
1281 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001283 psa_fallthrough = 0;
1284 }
1285 else
1286 {
1287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1288 psa_fallthrough = 1;
1289 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001290 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001291 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001292 psa_fallthrough = 1;
1293#else
1294 psa_fallthrough = 1;
1295#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001296
Hanno Beckercb1cc802018-11-17 22:27:38 +00001297 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001299 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001300 cipher_info ) ) != 0 )
1301 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001303 return( ret );
1304 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001307 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311 return( ret );
1312 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001315 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001319 return( ret );
1320 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_CIPHER_MODE_CBC)
1323 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1326 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001329 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001330 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1333 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 return( ret );
1337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001341 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001344 // Initialize compression
1345 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001347 {
Paul Bakker16770332013-10-11 09:59:44 +02001348 if( ssl->compress_buf == NULL )
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001351 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001352 if( ssl->compress_buf == NULL )
1353 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001355 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001356 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001357 }
1358 }
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001361
Paul Bakker48916f92012-09-16 19:57:18 +00001362 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1363 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001364
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001365 if( deflateInit( &transform->ctx_deflate,
1366 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001367 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1370 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001371 }
1372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
1381void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001382{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001383 mbedtls_md5_context md5;
1384 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 unsigned char pad_1[48];
1386 unsigned char pad_2[48];
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001390 mbedtls_md5_init( &md5 );
1391 mbedtls_sha1_init( &sha1 );
1392
1393 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1394 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Paul Bakker380da532012-04-18 16:10:25 +00001396 memset( pad_1, 0x36, 48 );
1397 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001399 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1400 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1401 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001403 mbedtls_md5_starts_ret( &md5 );
1404 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1405 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1406 mbedtls_md5_update_ret( &md5, hash, 16 );
1407 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001409 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1410 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1411 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001412
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001413 mbedtls_sha1_starts_ret( &sha1 );
1414 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1415 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1416 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1417 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001421
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001422 mbedtls_md5_free( &md5 );
1423 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001424
Paul Bakker380da532012-04-18 16:10:25 +00001425 return;
1426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1430void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001431{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001432 mbedtls_md5_context md5;
1433 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001437 mbedtls_md5_init( &md5 );
1438 mbedtls_sha1_init( &sha1 );
1439
1440 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1441 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Andrzej Kurekeb342242019-01-29 09:14:33 -05001443 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001444 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001449 mbedtls_md5_free( &md5 );
1450 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001451
Paul Bakker380da532012-04-18 16:10:25 +00001452 return;
1453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1457#if defined(MBEDTLS_SHA256_C)
1458void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001459{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001460#if defined(MBEDTLS_USE_PSA_CRYPTO)
1461 size_t hash_size;
1462 psa_status_t status;
1463 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1464
1465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1466 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1467 if( status != PSA_SUCCESS )
1468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1470 return;
1471 }
1472
1473 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1474 if( status != PSA_SUCCESS )
1475 {
1476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1477 return;
1478 }
1479 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1481#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001482 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001484 mbedtls_sha256_init( &sha256 );
1485
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001489 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001494 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001495#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001496 return;
1497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SHA512_C)
1501void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001502{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001503#if defined(MBEDTLS_USE_PSA_CRYPTO)
1504 size_t hash_size;
1505 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001506 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001507
1508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001509 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001510 if( status != PSA_SUCCESS )
1511 {
1512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1513 return;
1514 }
1515
Andrzej Kurek972fba52019-01-30 03:29:12 -05001516 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001517 if( status != PSA_SUCCESS )
1518 {
1519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1520 return;
1521 }
1522 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1524#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001526
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001527 mbedtls_sha512_init( &sha512 );
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001530
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001531 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001532 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001537 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001538#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 return;
1540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_SHA512_C */
1542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1545int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 unsigned char *p = ssl->handshake->premaster;
1548 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001549 const unsigned char *psk = ssl->conf->psk;
1550 size_t psk_len = ssl->conf->psk_len;
1551
1552 /* If the psk callback was called, use its result */
1553 if( ssl->handshake->psk != NULL )
1554 {
1555 psk = ssl->handshake->psk;
1556 psk_len = ssl->handshake->psk_len;
1557 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
1559 /*
1560 * PMS = struct {
1561 * opaque other_secret<0..2^16-1>;
1562 * opaque psk<0..2^16-1>;
1563 * };
1564 * with "other_secret" depending on the particular key exchange
1565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1567 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001569 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001572 *(p++) = (unsigned char)( psk_len >> 8 );
1573 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001574
1575 if( end < p || (size_t)( end - p ) < psk_len )
1576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1577
1578 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001579 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 }
1581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1583#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1584 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001585 {
1586 /*
1587 * other_secret already set by the ClientKeyExchange message,
1588 * and is 48 bytes long
1589 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001590 if( end - p < 2 )
1591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1592
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001593 *p++ = 0;
1594 *p++ = 48;
1595 p += 48;
1596 }
1597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1599#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1600 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001602 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001603 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001605 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001607 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001608 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 return( ret );
1612 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001613 *(p++) = (unsigned char)( len >> 8 );
1614 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001615 p += len;
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 }
1619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1621#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1622 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001624 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 size_t zlen;
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001628 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001629 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 return( ret );
1633 }
1634
1635 *(p++) = (unsigned char)( zlen >> 8 );
1636 *(p++) = (unsigned char)( zlen );
1637 p += zlen;
1638
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001639 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1640 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 }
1642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001647 }
1648
1649 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001650 if( end - p < 2 )
1651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001652
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001653 *(p++) = (unsigned char)( psk_len >> 8 );
1654 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001655
1656 if( end < p || (size_t)( end - p ) < psk_len )
1657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1658
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001659 memcpy( p, psk, psk_len );
1660 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001661
1662 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1663
1664 return( 0 );
1665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001669/*
1670 * SSLv3.0 MAC functions
1671 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001672#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001673static void ssl_mac( mbedtls_md_context_t *md_ctx,
1674 const unsigned char *secret,
1675 const unsigned char *buf, size_t len,
1676 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001677 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001678{
1679 unsigned char header[11];
1680 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1683 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001684
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001685 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001687 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001688 else
Paul Bakker68884e32013-01-07 18:20:04 +01001689 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 memcpy( header, ctr, 8 );
1692 header[ 8] = (unsigned char) type;
1693 header[ 9] = (unsigned char)( len >> 8 );
1694 header[10] = (unsigned char)( len );
1695
Paul Bakker68884e32013-01-07 18:20:04 +01001696 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 mbedtls_md_starts( md_ctx );
1698 mbedtls_md_update( md_ctx, secret, md_size );
1699 mbedtls_md_update( md_ctx, padding, padlen );
1700 mbedtls_md_update( md_ctx, header, 11 );
1701 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001702 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Paul Bakker68884e32013-01-07 18:20:04 +01001704 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_md_starts( md_ctx );
1706 mbedtls_md_update( md_ctx, secret, md_size );
1707 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001708 mbedtls_md_update( md_ctx, out, md_size );
1709 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1714 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001715 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001716#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001717#endif
1718
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001719/* The function below is only used in the Lucky 13 counter-measure in
1720 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1721#if defined(SSL_SOME_MODES_USE_MAC) && \
1722 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1723 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1724 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1725/* This function makes sure every byte in the memory region is accessed
1726 * (in ascending addresses order) */
1727static void ssl_read_memory( unsigned char *p, size_t len )
1728{
1729 unsigned char acc = 0;
1730 volatile unsigned char force;
1731
1732 for( ; len != 0; p++, len-- )
1733 acc ^= *p;
1734
1735 force = acc;
1736 (void) force;
1737}
1738#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1739
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001740/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001746 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754 }
1755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001759 ssl->out_msg, ssl->out_msglen );
1760
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001762 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001764#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 if( mode == MBEDTLS_MODE_STREAM ||
1766 ( mode == MBEDTLS_MODE_CBC
1767#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1768 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001769#endif
1770 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_PROTO_SSL3)
1773 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001774 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001775 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001776
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001777 ssl_mac( &ssl->transform_out->md_ctx_enc,
1778 ssl->transform_out->mac_enc,
1779 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001780 ssl->out_ctr, ssl->out_msgtype,
1781 mac );
1782
1783 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 }
1785 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1788 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 {
Hanno Becker992b6872017-11-09 18:57:39 +00001791 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1794 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1795 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1796 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001797 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001798 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001800
1801 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001802 }
1803 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001804#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 }
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001811 ssl->out_msg + ssl->out_msglen,
1812 ssl->transform_out->maclen );
1813
1814 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001815 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001816 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001817#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001819 /*
1820 * Encrypt
1821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1823 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001825 int ret;
1826 size_t olen = 0;
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 "including %d bytes of padding",
1830 ssl->out_msglen, 0 ) );
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001833 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001834 ssl->transform_out->ivlen,
1835 ssl->out_msg, ssl->out_msglen,
1836 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001839 return( ret );
1840 }
1841
1842 if( ssl->out_msglen != olen )
1843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001846 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 }
Paul Bakker68884e32013-01-07 18:20:04 +01001848 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850#if defined(MBEDTLS_GCM_C) || \
1851 defined(MBEDTLS_CCM_C) || \
1852 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 mode == MBEDTLS_MODE_CCM ||
1855 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001856 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001857 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001858 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001859 unsigned char *enc_msg;
1860 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001861 unsigned char iv[12];
1862 mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001864
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001865 /*
1866 * Prepare additional authenticated data
1867 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001868 memcpy( add_data, ssl->out_ctr, 8 );
1869 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001871 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001872 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1873 add_data[12] = ssl->out_msglen & 0xFF;
1874
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001876
Paul Bakker68884e32013-01-07 18:20:04 +01001877 /*
1878 * Generate IV
1879 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001880 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1881 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001882 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1884 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1885 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1886
1887 }
1888 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1889 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001890 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001891 unsigned char i;
1892
1893 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1894
1895 for( i = 0; i < 8; i++ )
1896 iv[i+4] ^= ssl->out_ctr[i];
1897 }
1898 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001899 {
1900 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001903 }
1904
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1906 iv, transform->ivlen );
1907 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1908 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001909
Paul Bakker68884e32013-01-07 18:20:04 +01001910 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001911 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001912 */
1913 enc_msg = ssl->out_msg;
1914 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001915 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 "including 0 bytes of padding",
1919 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001920
Paul Bakker68884e32013-01-07 18:20:04 +01001921 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001922 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001923 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001924 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1925 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001926 add_data, 13,
1927 enc_msg, enc_msglen,
1928 enc_msg, &olen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001929 enc_msg + enc_msglen,
1930 ssl->transform_out->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001933 return( ret );
1934 }
1935
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001936 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 }
1941
Hanno Beckere694c3e2017-12-27 21:34:08 +00001942 ssl->out_msglen += ssl->transform_out->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001943 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001944
Hanno Beckere694c3e2017-12-27 21:34:08 +00001945 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen,
1946 ssl->transform_out->taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001947 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1950#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001951 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001954 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001955 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001956 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001957
Paul Bakker48916f92012-09-16 19:57:18 +00001958 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1959 ssl->transform_out->ivlen;
1960 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 padlen = 0;
1962
1963 for( i = 0; i <= padlen; i++ )
1964 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1965
1966 ssl->out_msglen += padlen + 1;
1967
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001968 enc_msglen = ssl->out_msglen;
1969 enc_msg = ssl->out_msg;
1970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001972 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001973 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1974 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001977 {
1978 /*
1979 * Generate IV
1980 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001981 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001982 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001983 if( ret != 0 )
1984 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001985
Paul Bakker92be97b2013-01-02 17:30:03 +01001986 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001987 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001988
1989 /*
1990 * Fix pointer positions and message length with added IV
1991 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001992 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001993 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001994 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001999 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002000 ssl->out_msglen, ssl->transform_out->ivlen,
2001 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02002004 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002005 ssl->transform_out->ivlen,
2006 enc_msg, enc_msglen,
2007 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002010 return( ret );
2011 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002012
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 if( enc_msglen != olen )
2014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002017 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2020 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002021 {
2022 /*
2023 * Save IV in SSL3 and TLS1
2024 */
2025 memcpy( ssl->transform_out->iv_enc,
2026 ssl->transform_out->cipher_ctx_enc.iv,
2027 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002029#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002032 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002034 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2035
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002036 /*
2037 * MAC(MAC_write_key, seq_num +
2038 * TLSCipherText.type +
2039 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002040 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002041 * IV + // except for TLS 1.0
2042 * ENC(content + padding + padding_length));
2043 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002044 unsigned char pseudo_hdr[13];
2045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002047
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002048 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2049 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002050 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2051 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2056 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002057 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002058 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002060
Hanno Becker3d8c9072018-01-05 16:24:22 +00002061 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2062 ssl->transform_out->maclen );
2063
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002064 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002065 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002066 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002069 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002071 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002077 /* Make extra sure authentication was performed, exactly once */
2078 if( auth_done != 1 )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002082 }
2083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
2086 return( 0 );
2087}
2088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002092 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002093#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002094 size_t padlen = 0, correct = 1;
2095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002103 }
2104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002106
Paul Bakker48916f92012-09-16 19:57:18 +00002107 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002110 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 }
2113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2115 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002116 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002117 int ret;
2118 size_t olen = 0;
2119
Paul Bakker68884e32013-01-07 18:20:04 +01002120 padlen = 0;
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002123 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002124 ssl->transform_in->ivlen,
2125 ssl->in_msg, ssl->in_msglen,
2126 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002129 return( ret );
2130 }
2131
2132 if( ssl->in_msglen != olen )
2133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
Paul Bakker68884e32013-01-07 18:20:04 +01002138 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002140#if defined(MBEDTLS_GCM_C) || \
2141 defined(MBEDTLS_CCM_C) || \
2142 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002144 mode == MBEDTLS_MODE_CCM ||
2145 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002146 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002147 int ret;
2148 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002149 unsigned char *dec_msg;
2150 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002151 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002152 unsigned char iv[12];
2153 mbedtls_ssl_transform *transform = ssl->transform_in;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002154 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002155
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002156 /*
2157 * Compute and update sizes
2158 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002159 if( ssl->in_msglen < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002162 "+ taglen (%d)", ssl->in_msglen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002163 explicit_iv_len, ssl->transform_in->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002165 }
Hanno Beckere694c3e2017-12-27 21:34:08 +00002166 dec_msglen = ssl->in_msglen - explicit_iv_len - transform->taglen;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002167
Paul Bakker68884e32013-01-07 18:20:04 +01002168 dec_msg = ssl->in_msg;
2169 dec_msg_result = ssl->in_msg;
2170 ssl->in_msglen = dec_msglen;
2171
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002172 /*
2173 * Prepare additional authenticated data
2174 */
Paul Bakker68884e32013-01-07 18:20:04 +01002175 memcpy( add_data, ssl->in_ctr, 8 );
2176 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002178 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002179 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2180 add_data[12] = ssl->in_msglen & 0xFF;
2181
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002182 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002183
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 /*
2185 * Prepare IV
2186 */
2187 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2188 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002189 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002190 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2191 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002192
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002193 }
2194 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2195 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002196 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002197 unsigned char i;
2198
2199 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2200
2201 for( i = 0; i < 8; i++ )
2202 iv[i+4] ^= ssl->in_ctr[i];
2203 }
2204 else
2205 {
2206 /* Reminder if we ever add an AEAD mode with a different size */
2207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2209 }
2210
2211 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Beckere694c3e2017-12-27 21:34:08 +00002212 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen,
2213 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002214
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002215 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002216 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002219 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002220 add_data, 13,
2221 dec_msg, dec_msglen,
2222 dec_msg_result, &olen,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002223 dec_msg + dec_msglen,
2224 ssl->transform_in->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2229 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002230
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002231 return( ret );
2232 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002233 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002234
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002235 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002239 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2243#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002244 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 {
Paul Bakker45829992013-01-03 14:52:21 +01002247 /*
2248 * Decrypt and check the padding
2249 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002250 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002251 unsigned char *dec_msg;
2252 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002253 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002254 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002255 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002256
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 /*
Paul Bakker45829992013-01-03 14:52:21 +01002258 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2261 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002262 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002263#endif
Paul Bakker45829992013-01-03 14:52:21 +01002264
2265 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2266 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002269 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2270 ssl->transform_in->ivlen,
2271 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002273 }
2274
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002275 dec_msglen = ssl->in_msglen;
2276 dec_msg = ssl->in_msg;
2277 dec_msg_result = ssl->in_msg;
2278
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002279 /*
2280 * Authenticate before decrypt if enabled
2281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2283 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002284 {
Hanno Becker992b6872017-11-09 18:57:39 +00002285 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002286 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002289
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002290 dec_msglen -= ssl->transform_in->maclen;
2291 ssl->in_msglen -= ssl->transform_in->maclen;
2292
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002293 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2294 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2295 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2296 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2301 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002302 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002303 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002308 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309 ssl->transform_in->maclen );
2310
Hanno Becker992b6872017-11-09 18:57:39 +00002311 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2312 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002317 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002318 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002321
2322 /*
2323 * Check length sanity
2324 */
2325 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002328 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002330 }
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002333 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002334 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002337 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002339 dec_msglen -= ssl->transform_in->ivlen;
2340 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002341
Paul Bakker48916f92012-09-16 19:57:18 +00002342 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002343 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002348 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002349 ssl->transform_in->ivlen,
2350 dec_msg, dec_msglen,
2351 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002354 return( ret );
2355 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002356
Paul Bakkercca5b812013-08-31 17:40:26 +02002357 if( dec_msglen != olen )
2358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002361 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2364 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002365 {
2366 /*
2367 * Save IV in SSL3 and TLS1
2368 */
2369 memcpy( ssl->transform_in->iv_dec,
2370 ssl->transform_in->cipher_ctx_dec.iv,
2371 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
2375 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002376
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002377 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002378 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380#if defined(MBEDTLS_SSL_DEBUG_ALL)
2381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002382 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002383#endif
Paul Bakker45829992013-01-03 14:52:21 +01002384 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002385 correct = 0;
2386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_PROTO_SSL3)
2389 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 {
Paul Bakker48916f92012-09-16 19:57:18 +00002391 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_SSL_DEBUG_ALL)
2394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002396 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002397#endif
Paul Bakker45829992013-01-03 14:52:21 +01002398 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 }
2400 }
2401 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2403#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2404 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2405 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 {
2407 /*
Paul Bakker45829992013-01-03 14:52:21 +01002408 * TLSv1+: always check the padding up to the first failure
2409 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002411 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002412 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002413 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002414
Paul Bakker956c9e02013-12-19 14:42:28 +01002415 /*
2416 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002417 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002418 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002419 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002420 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002421 *
2422 * In both cases we reset padding_idx to a safe value (0) to
2423 * prevent out-of-buffer reads.
2424 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002425 correct &= ( padlen <= ssl->in_msglen );
2426 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002427 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002428
2429 padding_idx *= correct;
2430
Angus Grattonb512bc12018-06-19 15:57:50 +10002431 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002432 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002433 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002434 pad_count += real_count *
2435 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2436 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002437
2438 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002441 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002443#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002444 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2448 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002452 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002453
2454 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002458 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002462 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002464#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002467#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002470 * Authenticate if not done yet.
2471 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002473#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002474 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002475 {
Hanno Becker992b6872017-11-09 18:57:39 +00002476 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002477
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002480 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2481 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SSL_PROTO_SSL3)
2484 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002485 {
2486 ssl_mac( &ssl->transform_in->md_ctx_dec,
2487 ssl->transform_in->mac_dec,
2488 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002489 ssl->in_ctr, ssl->in_msgtype,
2490 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002491 }
2492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2495 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2496 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002497 {
2498 /*
2499 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002500 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002501 *
2502 * Known timing attacks:
2503 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2504 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002505 * To compensate for different timings for the MAC calculation
2506 * depending on how much padding was removed (which is determined
2507 * by padlen), process extra_run more blocks through the hash
2508 * function.
2509 *
2510 * The formula in the paper is
2511 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2512 * where L1 is the size of the header plus the decrypted message
2513 * plus CBC padding and L2 is the size of the header plus the
2514 * decrypted message. This is for an underlying hash function
2515 * with 64-byte blocks.
2516 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2517 * correctly. We round down instead of up, so -56 is the correct
2518 * value for our calculations instead of -55.
2519 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002520 * Repeat the formula rather than defining a block_size variable.
2521 * This avoids requiring division by a variable at runtime
2522 * (which would be marginally less efficient and would require
2523 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002524 */
2525 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002526
2527 /*
2528 * The next two sizes are the minimum and maximum values of
2529 * in_msglen over all padlen values.
2530 *
2531 * They're independent of padlen, since we previously did
2532 * in_msglen -= padlen.
2533 *
2534 * Note that max_len + maclen is never more than the buffer
2535 * length, as we previously did in_msglen -= maclen too.
2536 */
2537 const size_t max_len = ssl->in_msglen + padlen;
2538 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2539
Hanno Beckere694c3e2017-12-27 21:34:08 +00002540 switch( ssl->handshake->ciphersuite_info->mac )
Gilles Peskine20b44082018-05-29 14:06:49 +02002541 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002542#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2543 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002544 case MBEDTLS_MD_MD5:
2545 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002546 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 /* 8 bytes of message size, 64-byte compression blocks */
2548 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2549 ( 13 + ssl->in_msglen + 8 ) / 64;
2550 break;
2551#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002552#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002553 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002554 /* 16 bytes of message size, 128-byte compression blocks */
2555 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2556 ( 13 + ssl->in_msglen + 16 ) / 128;
2557 break;
2558#endif
2559 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2562 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002563
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002564 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2567 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2568 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2569 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002570 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002571 /* Make sure we access everything even when padlen > 0. This
2572 * makes the synchronisation requirements for just-in-time
2573 * Prime+Probe attacks much tighter and hopefully impractical. */
2574 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002575 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002576
2577 /* Call mbedtls_md_process at least once due to cache attacks
2578 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002579 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002583
2584 /* Make sure we access all the memory that could contain the MAC,
2585 * before we check it in the next code block. This makes the
2586 * synchronisation requirements for just-in-time Prime+Probe
2587 * attacks much tighter and hopefully impractical. */
2588 ssl_read_memory( ssl->in_msg + min_len,
2589 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002590 }
2591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2593 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002599#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002600 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2601 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2602 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Hanno Becker992b6872017-11-09 18:57:39 +00002605 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2606 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_DEBUG_ALL)
2609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002610#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002611 correct = 0;
2612 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002613 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002614 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002615
2616 /*
2617 * Finally check the correct flag
2618 */
2619 if( correct == 0 )
2620 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002621#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002622
2623 /* Make extra sure authentication was performed, exactly once */
2624 if( auth_done != 1 )
2625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
2630 if( ssl->in_msglen == 0 )
2631 {
Angus Gratton34817922018-06-19 15:58:22 +10002632#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2633 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2634 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2635 {
2636 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2638 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2639 }
2640#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 ssl->nb_zero++;
2643
2644 /*
2645 * Three or more empty messages may be a DoS attack
2646 * (excessive CPU consumption).
2647 */
2648 if( ssl->nb_zero > 3 )
2649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
2654 }
2655 else
2656 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002660 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002661 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002662 }
2663 else
2664#endif
2665 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002666 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002667 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2668 if( ++ssl->in_ctr[i - 1] != 0 )
2669 break;
2670
2671 /* The loop goes to its end iff the counter is wrapping */
2672 if( i == ssl_ep_len( ssl ) )
2673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2675 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002676 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002677 }
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 return( 0 );
2682}
2683
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002684#undef MAC_NONE
2685#undef MAC_PLAINTEXT
2686#undef MAC_CIPHERTEXT
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002689/*
2690 * Compression/decompression functions
2691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693{
2694 int ret;
2695 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002696 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002697 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002698 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002701
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002702 if( len_pre == 0 )
2703 return( 0 );
2704
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705 memcpy( msg_pre, ssl->out_msg, len_pre );
2706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002708 ssl->out_msglen ) );
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002711 ssl->out_msg, ssl->out_msglen );
2712
Paul Bakker48916f92012-09-16 19:57:18 +00002713 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2714 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2715 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002716 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002717
Paul Bakker48916f92012-09-16 19:57:18 +00002718 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002719 if( ret != Z_OK )
2720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2722 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002723 }
2724
Angus Grattond8213d02016-05-25 20:56:48 +10002725 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002726 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002729 ssl->out_msglen ) );
2730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732 ssl->out_msg, ssl->out_msglen );
2733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002735
2736 return( 0 );
2737}
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740{
2741 int ret;
2742 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002743 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002744 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002745 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002749 if( len_pre == 0 )
2750 return( 0 );
2751
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752 memcpy( msg_pre, ssl->in_msg, len_pre );
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002755 ssl->in_msglen ) );
2756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002758 ssl->in_msg, ssl->in_msglen );
2759
Paul Bakker48916f92012-09-16 19:57:18 +00002760 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2761 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2762 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002763 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002764 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002765
Paul Bakker48916f92012-09-16 19:57:18 +00002766 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767 if( ret != Z_OK )
2768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2770 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771 }
2772
Angus Grattond8213d02016-05-25 20:56:48 +10002773 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002774 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777 ssl->in_msglen ) );
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002780 ssl->in_msg, ssl->in_msglen );
2781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783
2784 return( 0 );
2785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2789static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_PROTO_DTLS)
2792static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002793{
2794 /* If renegotiation is not enforced, retransmit until we would reach max
2795 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002796 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002797 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002798 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002799 unsigned char doublings = 1;
2800
2801 while( ratio != 0 )
2802 {
2803 ++doublings;
2804 ratio >>= 1;
2805 }
2806
2807 if( ++ssl->renego_records_seen > doublings )
2808 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002810 return( 0 );
2811 }
2812 }
2813
2814 return( ssl_write_hello_request( ssl ) );
2815}
2816#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002820 * Fill the input message buffer by appending data to it.
2821 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002822 *
2823 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2824 * available (from this read and/or a previous one). Otherwise, an error code
2825 * is returned (possibly EOF or WANT_READ).
2826 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002827 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2828 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2829 * since we always read a whole datagram at once.
2830 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002831 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002832 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002835{
Paul Bakker23986e52011-04-24 08:57:21 +00002836 int ret;
2837 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002841 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002844 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002846 }
2847
Angus Grattond8213d02016-05-25 20:56:48 +10002848 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002852 }
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002857 uint32_t timeout;
2858
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002859 /* Just to be sure */
2860 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2863 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2864 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2865 }
2866
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002867 /*
2868 * The point is, we need to always read a full datagram at once, so we
2869 * sometimes read more then requested, and handle the additional data.
2870 * It could be the rest of the current record (while fetching the
2871 * header) and/or some other records in the same datagram.
2872 */
2873
2874 /*
2875 * Move to the next record in the already read datagram if applicable
2876 */
2877 if( ssl->next_record_offset != 0 )
2878 {
2879 if( ssl->in_left < ssl->next_record_offset )
2880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002883 }
2884
2885 ssl->in_left -= ssl->next_record_offset;
2886
2887 if( ssl->in_left != 0 )
2888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002890 ssl->next_record_offset ) );
2891 memmove( ssl->in_hdr,
2892 ssl->in_hdr + ssl->next_record_offset,
2893 ssl->in_left );
2894 }
2895
2896 ssl->next_record_offset = 0;
2897 }
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002901
2902 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002903 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002904 */
2905 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002908 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002909 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002910
2911 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002912 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002913 * are not at the beginning of a new record, the caller did something
2914 * wrong.
2915 */
2916 if( ssl->in_left != 0 )
2917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002920 }
2921
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002922 /*
2923 * Don't even try to read if time's out already.
2924 * This avoids by-passing the timer when repeatedly receiving messages
2925 * that will end up being dropped.
2926 */
2927 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002928 {
2929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002930 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002931 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002932 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002933 {
Angus Grattond8213d02016-05-25 20:56:48 +10002934 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002937 timeout = ssl->handshake->retransmit_timeout;
2938 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002939 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002942
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002943 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002944 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2945 timeout );
2946 else
2947 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002950
2951 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002953 }
2954
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002955 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002958 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002961 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002962 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002965 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002966 }
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002971 return( ret );
2972 }
2973
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002974 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002977 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002979 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002980 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002983 return( ret );
2984 }
2985
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002986 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002989 }
2990
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 if( ret < 0 )
2992 return( ret );
2993
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002994 ssl->in_left = ret;
2995 }
2996 else
2997#endif
2998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003000 ssl->in_left, nb_want ) );
3001
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003002 while( ssl->in_left < nb_want )
3003 {
3004 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003005
3006 if( ssl_check_timer( ssl ) != 0 )
3007 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3008 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003009 {
3010 if( ssl->f_recv_timeout != NULL )
3011 {
3012 ret = ssl->f_recv_timeout( ssl->p_bio,
3013 ssl->in_hdr + ssl->in_left, len,
3014 ssl->conf->read_timeout );
3015 }
3016 else
3017 {
3018 ret = ssl->f_recv( ssl->p_bio,
3019 ssl->in_hdr + ssl->in_left, len );
3020 }
3021 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003024 ssl->in_left, nb_want ) );
3025 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003026
3027 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003029
3030 if( ret < 0 )
3031 return( ret );
3032
mohammad160352aecb92018-03-28 23:41:40 -07003033 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003034 {
Darryl Green11999bb2018-03-13 15:22:58 +00003035 MBEDTLS_SSL_DEBUG_MSG( 1,
3036 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003037 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3039 }
3040
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003041 ssl->in_left += ret;
3042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
3047 return( 0 );
3048}
3049
3050/*
3051 * Flush any data not yet written
3052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003054{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003055 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003056 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003060 if( ssl->f_send == NULL )
3061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003063 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003065 }
3066
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003067 /* Avoid incrementing counter if data is flushed */
3068 if( ssl->out_left == 0 )
3069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003071 return( 0 );
3072 }
3073
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 while( ssl->out_left > 0 )
3075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3077 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Hanno Becker2b1e3542018-08-06 11:19:13 +01003079 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003080 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
3084 if( ret <= 0 )
3085 return( ret );
3086
mohammad160352aecb92018-03-28 23:41:40 -07003087 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003088 {
Darryl Green11999bb2018-03-13 15:22:58 +00003089 MBEDTLS_SSL_DEBUG_MSG( 1,
3090 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003091 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3093 }
3094
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 ssl->out_left -= ret;
3096 }
3097
Hanno Becker2b1e3542018-08-06 11:19:13 +01003098#if defined(MBEDTLS_SSL_PROTO_DTLS)
3099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003100 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003101 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003102 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003103 else
3104#endif
3105 {
3106 ssl->out_hdr = ssl->out_buf + 8;
3107 }
3108 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
3112 return( 0 );
3113}
3114
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003115/*
3116 * Functions to handle the DTLS retransmission state machine
3117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003119/*
3120 * Append current handshake message to current outgoing flight
3121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3126 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3127 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003128
3129 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003130 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003131 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003134 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003135 }
3136
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003137 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003138 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003141 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003142 }
3143
3144 /* Copy current handshake message with headers */
3145 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3146 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003147 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003148 msg->next = NULL;
3149
3150 /* Append to the current flight */
3151 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003152 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003153 else
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003156 while( cur->next != NULL )
3157 cur = cur->next;
3158 cur->next = msg;
3159 }
3160
Hanno Becker3b235902018-08-06 09:54:53 +01003161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162 return( 0 );
3163}
3164
3165/*
3166 * Free the current flight of handshake messages
3167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 mbedtls_ssl_flight_item *cur = flight;
3171 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003172
3173 while( cur != NULL )
3174 {
3175 next = cur->next;
3176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 mbedtls_free( cur->p );
3178 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179
3180 cur = next;
3181 }
3182}
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3185static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003186#endif
3187
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003189 * Swap transform_out and out_ctr with the alternative ones
3190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003194 unsigned char tmp_out_ctr[8];
3195
3196 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003199 return;
3200 }
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003203
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003204 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003205 tmp_transform = ssl->transform_out;
3206 ssl->transform_out = ssl->handshake->alt_transform_out;
3207 ssl->handshake->alt_transform_out = tmp_transform;
3208
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003209 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003210 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3211 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003212 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003213
3214 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003215 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3218 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3223 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003224 }
3225 }
3226#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003227}
3228
3229/*
3230 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003231 */
3232int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3233{
3234 int ret = 0;
3235
3236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3237
3238 ret = mbedtls_ssl_flight_transmit( ssl );
3239
3240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3241
3242 return( ret );
3243}
3244
3245/*
3246 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003247 *
3248 * Need to remember the current message in case flush_output returns
3249 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003250 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003251 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003252int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003253{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003254 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003258 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003260
3261 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003262 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003263 ssl_swap_epochs( ssl );
3264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003266 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003267
3268 while( ssl->handshake->cur_msg != NULL )
3269 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003270 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003271 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003272
Hanno Beckere1dcb032018-08-17 16:47:58 +01003273 int const is_finished =
3274 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3275 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3276
Hanno Becker04da1892018-08-14 13:22:10 +01003277 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3278 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3279
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003280 /* Swap epochs before sending Finished: we can't do it after
3281 * sending ChangeCipherSpec, in case write returns WANT_READ.
3282 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003283 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003284 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003286 ssl_swap_epochs( ssl );
3287 }
3288
Hanno Becker67bc7c32018-08-06 11:33:50 +01003289 ret = ssl_get_remaining_payload_in_datagram( ssl );
3290 if( ret < 0 )
3291 return( ret );
3292 max_frag_len = (size_t) ret;
3293
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003294 /* CCS is copied as is, while HS messages may need fragmentation */
3295 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3296 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003297 if( max_frag_len == 0 )
3298 {
3299 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3300 return( ret );
3301
3302 continue;
3303 }
3304
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003305 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003306 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003307 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003309 /* Update position inside current message */
3310 ssl->handshake->cur_msg_p += cur->len;
3311 }
3312 else
3313 {
3314 const unsigned char * const p = ssl->handshake->cur_msg_p;
3315 const size_t hs_len = cur->len - 12;
3316 const size_t frag_off = p - ( cur->p + 12 );
3317 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003318 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319
Hanno Beckere1dcb032018-08-17 16:47:58 +01003320 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003321 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003322 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003323 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324
Hanno Becker67bc7c32018-08-06 11:33:50 +01003325 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3326 return( ret );
3327
3328 continue;
3329 }
3330 max_hs_frag_len = max_frag_len - 12;
3331
3332 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3333 max_hs_frag_len : rem_len;
3334
3335 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003338 (unsigned) cur_hs_frag_len,
3339 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003340 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003341
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003342 /* Messages are stored with handshake headers as if not fragmented,
3343 * copy beginning of headers then fill fragmentation fields.
3344 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3345 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003347 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3348 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3349 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3350
Hanno Becker67bc7c32018-08-06 11:33:50 +01003351 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3352 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3353 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003354
3355 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3356
Hanno Becker3f7b9732018-08-28 09:53:25 +01003357 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003358 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3359 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003360 ssl->out_msgtype = cur->type;
3361
3362 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003363 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003364 }
3365
3366 /* If done with the current message move to the next one if any */
3367 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3368 {
3369 if( cur->next != NULL )
3370 {
3371 ssl->handshake->cur_msg = cur->next;
3372 ssl->handshake->cur_msg_p = cur->next->p + 12;
3373 }
3374 else
3375 {
3376 ssl->handshake->cur_msg = NULL;
3377 ssl->handshake->cur_msg_p = NULL;
3378 }
3379 }
3380
3381 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003382 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003385 return( ret );
3386 }
3387 }
3388
Hanno Becker67bc7c32018-08-06 11:33:50 +01003389 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3390 return( ret );
3391
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003392 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3394 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003395 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3399 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003400
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003402
3403 return( 0 );
3404}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003405
3406/*
3407 * To be called when the last message of an incoming flight is received.
3408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003410{
3411 /* We won't need to resend that one any more */
3412 ssl_flight_free( ssl->handshake->flight );
3413 ssl->handshake->flight = NULL;
3414 ssl->handshake->cur_msg = NULL;
3415
3416 /* The next incoming flight will start with this msg_seq */
3417 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3418
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003419 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003420 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003421
Hanno Becker0271f962018-08-16 13:23:47 +01003422 /* Clear future message buffering structure. */
3423 ssl_buffering_free( ssl );
3424
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003425 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003426 ssl_set_timer( ssl, 0 );
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3429 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003432 }
3433 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003435}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003436
3437/*
3438 * To be called when the last message of an outgoing flight is send.
3439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003441{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003442 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003443 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3446 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003449 }
3450 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003454
Paul Bakker5121ce52009-01-03 21:22:43 +00003455/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003456 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003458
3459/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003460 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003461 *
3462 * - fill in handshake headers
3463 * - update handshake checksum
3464 * - DTLS: save message for resending
3465 * - then pass to the record layer
3466 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003467 * DTLS: except for HelloRequest, messages are only queued, and will only be
3468 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003469 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003470 * Inputs:
3471 * - ssl->out_msglen: 4 + actual handshake message len
3472 * (4 is the size of handshake headers for TLS)
3473 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3474 * - ssl->out_msg + 4: the handshake message body
3475 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003476 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003477 * - ssl->out_msglen: the length of the record contents
3478 * (including handshake headers but excluding record headers)
3479 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003480 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003481int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003482{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003483 int ret;
3484 const size_t hs_len = ssl->out_msglen - 4;
3485 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3488
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003489 /*
3490 * Sanity checks
3491 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003492 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003493 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3494 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003495 /* In SSLv3, the client might send a NoCertificate alert. */
3496#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3497 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3498 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3499 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3500#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3501 {
3502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3504 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003506
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003507 /* Whenever we send anything different from a
3508 * HelloRequest we should be in a handshake - double check. */
3509 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3510 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003511 ssl->handshake == NULL )
3512 {
3513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003521 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003524 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003526
Hanno Beckerb50a2532018-08-06 11:52:54 +01003527 /* Double-check that we did not exceed the bounds
3528 * of the outgoing record buffer.
3529 * This should never fail as the various message
3530 * writing functions must obey the bounds of the
3531 * outgoing record buffer, but better be safe.
3532 *
3533 * Note: We deliberately do not check for the MTU or MFL here.
3534 */
3535 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3536 {
3537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3538 "size %u, maximum %u",
3539 (unsigned) ssl->out_msglen,
3540 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3542 }
3543
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003544 /*
3545 * Fill handshake headers
3546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003549 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3550 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3551 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003553 /*
3554 * DTLS has additional fields in the Handshake layer,
3555 * between the length field and the actual payload:
3556 * uint16 message_seq;
3557 * uint24 fragment_offset;
3558 * uint24 fragment_length;
3559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003561 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003562 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003563 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003564 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003565 {
3566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3567 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003568 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003569 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3571 }
3572
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003573 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003574 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003575
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003576 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003577 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003578 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003579 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3580 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3581 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003582 }
3583 else
3584 {
3585 ssl->out_msg[4] = 0;
3586 ssl->out_msg[5] = 0;
3587 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003588
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003589 /* Handshake hashes are computed without fragmentation,
3590 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003591 memset( ssl->out_msg + 6, 0x00, 3 );
3592 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003595
Hanno Becker0207e532018-08-28 10:28:28 +01003596 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003597 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3598 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 }
3600
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003601 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003603 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003604 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3605 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 {
3607 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003610 return( ret );
3611 }
3612 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003613 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003615 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003616 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003617 {
3618 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3619 return( ret );
3620 }
3621 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003622
3623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3624
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003625 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003626}
3627
3628/*
3629 * Record layer functions
3630 */
3631
3632/*
3633 * Write current record.
3634 *
3635 * Uses:
3636 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3637 * - ssl->out_msglen: length of the record content (excl headers)
3638 * - ssl->out_msg: record content
3639 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003640int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641{
3642 int ret, done = 0;
3643 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003644 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003645
3646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003649 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003651 {
3652 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003655 return( ret );
3656 }
3657
3658 len = ssl->out_msglen;
3659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3663 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 ret = mbedtls_ssl_hw_record_write( ssl );
3668 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003672 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003673
3674 if( ret == 0 )
3675 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003678 if( !done )
3679 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003680 unsigned i;
3681 size_t protected_record_size;
3682
Paul Bakker05ef8352012-05-08 09:17:57 +00003683 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003685 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003686
Hanno Becker19859472018-08-06 09:40:20 +01003687 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003688 ssl->out_len[0] = (unsigned char)( len >> 8 );
3689 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003690
Paul Bakker48916f92012-09-16 19:57:18 +00003691 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003692 {
3693 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003696 return( ret );
3697 }
3698
3699 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003700 ssl->out_len[0] = (unsigned char)( len >> 8 );
3701 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003702 }
3703
Hanno Becker2b1e3542018-08-06 11:19:13 +01003704 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3705
3706#if defined(MBEDTLS_SSL_PROTO_DTLS)
3707 /* In case of DTLS, double-check that we don't exceed
3708 * the remaining space in the datagram. */
3709 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3710 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003711 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003712 if( ret < 0 )
3713 return( ret );
3714
3715 if( protected_record_size > (size_t) ret )
3716 {
3717 /* Should never happen */
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3719 }
3720 }
3721#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003724 "version = [%d:%d], msglen = %d",
3725 ssl->out_hdr[0], ssl->out_hdr[1],
3726 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003729 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003730
3731 ssl->out_left += protected_record_size;
3732 ssl->out_hdr += protected_record_size;
3733 ssl_update_out_pointers( ssl, ssl->transform_out );
3734
Hanno Becker04484622018-08-06 09:49:38 +01003735 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3736 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3737 break;
3738
3739 /* The loop goes to its end iff the counter is wrapping */
3740 if( i == ssl_ep_len( ssl ) )
3741 {
3742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3743 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 }
3746
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3749 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003750 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003751 size_t remaining;
3752 ret = ssl_get_remaining_payload_in_datagram( ssl );
3753 if( ret < 0 )
3754 {
3755 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3756 ret );
3757 return( ret );
3758 }
3759
3760 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003761 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003762 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003764 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003765 else
3766 {
Hanno Becker513815a2018-08-20 11:56:09 +01003767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 }
3769 }
3770#endif /* MBEDTLS_SSL_PROTO_DTLS */
3771
3772 if( ( flush == SSL_FORCE_FLUSH ) &&
3773 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 return( ret );
3777 }
3778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
3781 return( 0 );
3782}
3783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003785
3786static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3787{
3788 if( ssl->in_msglen < ssl->in_hslen ||
3789 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3790 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3791 {
3792 return( 1 );
3793 }
3794 return( 0 );
3795}
Hanno Becker44650b72018-08-16 12:51:11 +01003796
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003797static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003798{
3799 return( ( ssl->in_msg[9] << 16 ) |
3800 ( ssl->in_msg[10] << 8 ) |
3801 ssl->in_msg[11] );
3802}
3803
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003804static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003805{
3806 return( ( ssl->in_msg[6] << 16 ) |
3807 ( ssl->in_msg[7] << 8 ) |
3808 ssl->in_msg[8] );
3809}
3810
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003811static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003812{
3813 uint32_t msg_len, frag_off, frag_len;
3814
3815 msg_len = ssl_get_hs_total_len( ssl );
3816 frag_off = ssl_get_hs_frag_off( ssl );
3817 frag_len = ssl_get_hs_frag_len( ssl );
3818
3819 if( frag_off > msg_len )
3820 return( -1 );
3821
3822 if( frag_len > msg_len - frag_off )
3823 return( -1 );
3824
3825 if( frag_len + 12 > ssl->in_msglen )
3826 return( -1 );
3827
3828 return( 0 );
3829}
3830
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003831/*
3832 * Mark bits in bitmask (used for DTLS HS reassembly)
3833 */
3834static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3835{
3836 unsigned int start_bits, end_bits;
3837
3838 start_bits = 8 - ( offset % 8 );
3839 if( start_bits != 8 )
3840 {
3841 size_t first_byte_idx = offset / 8;
3842
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003843 /* Special case */
3844 if( len <= start_bits )
3845 {
3846 for( ; len != 0; len-- )
3847 mask[first_byte_idx] |= 1 << ( start_bits - len );
3848
3849 /* Avoid potential issues with offset or len becoming invalid */
3850 return;
3851 }
3852
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003853 offset += start_bits; /* Now offset % 8 == 0 */
3854 len -= start_bits;
3855
3856 for( ; start_bits != 0; start_bits-- )
3857 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3858 }
3859
3860 end_bits = len % 8;
3861 if( end_bits != 0 )
3862 {
3863 size_t last_byte_idx = ( offset + len ) / 8;
3864
3865 len -= end_bits; /* Now len % 8 == 0 */
3866
3867 for( ; end_bits != 0; end_bits-- )
3868 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3869 }
3870
3871 memset( mask + offset / 8, 0xFF, len / 8 );
3872}
3873
3874/*
3875 * Check that bitmask is full
3876 */
3877static int ssl_bitmask_check( unsigned char *mask, size_t len )
3878{
3879 size_t i;
3880
3881 for( i = 0; i < len / 8; i++ )
3882 if( mask[i] != 0xFF )
3883 return( -1 );
3884
3885 for( i = 0; i < len % 8; i++ )
3886 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3887 return( -1 );
3888
3889 return( 0 );
3890}
3891
Hanno Becker56e205e2018-08-16 09:06:12 +01003892/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003893static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003894 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003895{
Hanno Becker56e205e2018-08-16 09:06:12 +01003896 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003897
Hanno Becker56e205e2018-08-16 09:06:12 +01003898 alloc_len = 12; /* Handshake header */
3899 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003900
Hanno Beckerd07df862018-08-16 09:14:58 +01003901 if( add_bitmap )
3902 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003903
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003904 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003905}
Hanno Becker56e205e2018-08-16 09:06:12 +01003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003908
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003909static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003910{
3911 return( ( ssl->in_msg[1] << 16 ) |
3912 ( ssl->in_msg[2] << 8 ) |
3913 ssl->in_msg[3] );
3914}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003915
Simon Butcher99000142016-10-13 17:21:01 +01003916int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003921 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003923 }
3924
Hanno Becker12555c62018-08-16 12:47:53 +01003925 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003928 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003929 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003932 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003933 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003934 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003935 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003936
Hanno Becker44650b72018-08-16 12:51:11 +01003937 if( ssl_check_hs_header( ssl ) != 0 )
3938 {
3939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3940 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3941 }
3942
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003943 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003944 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3945 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3946 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3947 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003948 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003949 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3950 {
3951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3952 recv_msg_seq,
3953 ssl->handshake->in_msg_seq ) );
3954 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3955 }
3956
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003957 /* Retransmit only on last message from previous flight, to avoid
3958 * too many retransmissions.
3959 * Besides, No sane server ever retransmits HelloVerifyRequest */
3960 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003964 "message_seq = %d, start_of_flight = %d",
3965 recv_msg_seq,
3966 ssl->handshake->in_flight_start_seq ) );
3967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003971 return( ret );
3972 }
3973 }
3974 else
3975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003977 "message_seq = %d, expected = %d",
3978 recv_msg_seq,
3979 ssl->handshake->in_msg_seq ) );
3980 }
3981
Hanno Becker90333da2017-10-10 11:27:13 +01003982 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003983 }
3984 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003985
Hanno Becker6d97ef52018-08-16 13:09:04 +01003986 /* Message reassembly is handled alongside buffering of future
3987 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003988 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003989 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003990 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003993 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003994 }
3995 }
3996 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003998 /* With TLS we don't handle fragmentation (for now) */
3999 if( ssl->in_msglen < ssl->in_hslen )
4000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4002 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004003 }
4004
Simon Butcher99000142016-10-13 17:21:01 +01004005 return( 0 );
4006}
4007
4008void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4009{
Hanno Becker0271f962018-08-16 13:23:47 +01004010 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004011
Hanno Becker0271f962018-08-16 13:23:47 +01004012 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004013 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004014 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004015 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004016
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004017 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004020 ssl->handshake != NULL )
4021 {
Hanno Becker0271f962018-08-16 13:23:47 +01004022 unsigned offset;
4023 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004024
Hanno Becker0271f962018-08-16 13:23:47 +01004025 /* Increment handshake sequence number */
4026 hs->in_msg_seq++;
4027
4028 /*
4029 * Clear up handshake buffering and reassembly structure.
4030 */
4031
4032 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004033 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004034
4035 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004036 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4037 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004038 offset++, hs_buf++ )
4039 {
4040 *hs_buf = *(hs_buf + 1);
4041 }
4042
4043 /* Create a fresh last entry */
4044 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004045 }
4046#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004047}
4048
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004049/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004050 * DTLS anti-replay: RFC 6347 4.1.2.6
4051 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004052 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4053 * Bit n is set iff record number in_window_top - n has been seen.
4054 *
4055 * Usually, in_window_top is the last record number seen and the lsb of
4056 * in_window is set. The only exception is the initial state (record number 0
4057 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4060static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004061{
4062 ssl->in_window_top = 0;
4063 ssl->in_window = 0;
4064}
4065
4066static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4067{
4068 return( ( (uint64_t) buf[0] << 40 ) |
4069 ( (uint64_t) buf[1] << 32 ) |
4070 ( (uint64_t) buf[2] << 24 ) |
4071 ( (uint64_t) buf[3] << 16 ) |
4072 ( (uint64_t) buf[4] << 8 ) |
4073 ( (uint64_t) buf[5] ) );
4074}
4075
4076/*
4077 * Return 0 if sequence number is acceptable, -1 otherwise
4078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004080{
4081 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4082 uint64_t bit;
4083
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004084 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004085 return( 0 );
4086
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004087 if( rec_seqnum > ssl->in_window_top )
4088 return( 0 );
4089
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004090 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004091
4092 if( bit >= 64 )
4093 return( -1 );
4094
4095 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4096 return( -1 );
4097
4098 return( 0 );
4099}
4100
4101/*
4102 * Update replay window on new validated record
4103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004105{
4106 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4107
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004108 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004109 return;
4110
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004111 if( rec_seqnum > ssl->in_window_top )
4112 {
4113 /* Update window_top and the contents of the window */
4114 uint64_t shift = rec_seqnum - ssl->in_window_top;
4115
4116 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004117 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004118 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004119 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004120 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004121 ssl->in_window |= 1;
4122 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004123
4124 ssl->in_window_top = rec_seqnum;
4125 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004126 else
4127 {
4128 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004129 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004130
4131 if( bit < 64 ) /* Always true, but be extra sure */
4132 ssl->in_window |= (uint64_t) 1 << bit;
4133 }
4134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004136
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004137#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004138/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004139static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4140
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004141/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004142 * Without any SSL context, check if a datagram looks like a ClientHello with
4143 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004144 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004145 *
4146 * - if cookie is valid, return 0
4147 * - if ClientHello looks superficially valid but cookie is not,
4148 * fill obuf and set olen, then
4149 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4150 * - otherwise return a specific error code
4151 */
4152static int ssl_check_dtls_clihlo_cookie(
4153 mbedtls_ssl_cookie_write_t *f_cookie_write,
4154 mbedtls_ssl_cookie_check_t *f_cookie_check,
4155 void *p_cookie,
4156 const unsigned char *cli_id, size_t cli_id_len,
4157 const unsigned char *in, size_t in_len,
4158 unsigned char *obuf, size_t buf_len, size_t *olen )
4159{
4160 size_t sid_len, cookie_len;
4161 unsigned char *p;
4162
4163 if( f_cookie_write == NULL || f_cookie_check == NULL )
4164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4165
4166 /*
4167 * Structure of ClientHello with record and handshake headers,
4168 * and expected values. We don't need to check a lot, more checks will be
4169 * done when actually parsing the ClientHello - skipping those checks
4170 * avoids code duplication and does not make cookie forging any easier.
4171 *
4172 * 0-0 ContentType type; copied, must be handshake
4173 * 1-2 ProtocolVersion version; copied
4174 * 3-4 uint16 epoch; copied, must be 0
4175 * 5-10 uint48 sequence_number; copied
4176 * 11-12 uint16 length; (ignored)
4177 *
4178 * 13-13 HandshakeType msg_type; (ignored)
4179 * 14-16 uint24 length; (ignored)
4180 * 17-18 uint16 message_seq; copied
4181 * 19-21 uint24 fragment_offset; copied, must be 0
4182 * 22-24 uint24 fragment_length; (ignored)
4183 *
4184 * 25-26 ProtocolVersion client_version; (ignored)
4185 * 27-58 Random random; (ignored)
4186 * 59-xx SessionID session_id; 1 byte len + sid_len content
4187 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4188 * ...
4189 *
4190 * Minimum length is 61 bytes.
4191 */
4192 if( in_len < 61 ||
4193 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4194 in[3] != 0 || in[4] != 0 ||
4195 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4196 {
4197 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4198 }
4199
4200 sid_len = in[59];
4201 if( sid_len > in_len - 61 )
4202 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4203
4204 cookie_len = in[60 + sid_len];
4205 if( cookie_len > in_len - 60 )
4206 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4207
4208 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4209 cli_id, cli_id_len ) == 0 )
4210 {
4211 /* Valid cookie */
4212 return( 0 );
4213 }
4214
4215 /*
4216 * If we get here, we've got an invalid cookie, let's prepare HVR.
4217 *
4218 * 0-0 ContentType type; copied
4219 * 1-2 ProtocolVersion version; copied
4220 * 3-4 uint16 epoch; copied
4221 * 5-10 uint48 sequence_number; copied
4222 * 11-12 uint16 length; olen - 13
4223 *
4224 * 13-13 HandshakeType msg_type; hello_verify_request
4225 * 14-16 uint24 length; olen - 25
4226 * 17-18 uint16 message_seq; copied
4227 * 19-21 uint24 fragment_offset; copied
4228 * 22-24 uint24 fragment_length; olen - 25
4229 *
4230 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4231 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4232 *
4233 * Minimum length is 28.
4234 */
4235 if( buf_len < 28 )
4236 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4237
4238 /* Copy most fields and adapt others */
4239 memcpy( obuf, in, 25 );
4240 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4241 obuf[25] = 0xfe;
4242 obuf[26] = 0xff;
4243
4244 /* Generate and write actual cookie */
4245 p = obuf + 28;
4246 if( f_cookie_write( p_cookie,
4247 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4248 {
4249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4250 }
4251
4252 *olen = p - obuf;
4253
4254 /* Go back and fill length fields */
4255 obuf[27] = (unsigned char)( *olen - 28 );
4256
4257 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4258 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4259 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4260
4261 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4262 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4263
4264 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4265}
4266
4267/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004268 * Handle possible client reconnect with the same UDP quadruplet
4269 * (RFC 6347 Section 4.2.8).
4270 *
4271 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4272 * that looks like a ClientHello.
4273 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004274 * - if the input looks like a ClientHello without cookies,
4275 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004276 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004277 * - if the input looks like a ClientHello with a valid cookie,
4278 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004279 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004280 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004281 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004282 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004283 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4284 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004285 */
4286static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4287{
4288 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004289 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004290
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004291 ret = ssl_check_dtls_clihlo_cookie(
4292 ssl->conf->f_cookie_write,
4293 ssl->conf->f_cookie_check,
4294 ssl->conf->p_cookie,
4295 ssl->cli_id, ssl->cli_id_len,
4296 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004297 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004298
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004299 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4300
4301 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004302 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004303 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004304 * If the error is permanent we'll catch it later,
4305 * if it's not, then hopefully it'll work next time. */
4306 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4307
4308 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004309 }
4310
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004311 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004312 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004313 /* Got a valid cookie, partially reset context */
4314 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4315 {
4316 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4317 return( ret );
4318 }
4319
4320 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004321 }
4322
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004323 return( ret );
4324}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004325#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004326
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004327/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004328 * ContentType type;
4329 * ProtocolVersion version;
4330 * uint16 epoch; // DTLS only
4331 * uint48 sequence_number; // DTLS only
4332 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004333 *
4334 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004335 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004336 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4337 *
4338 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004339 * 1. proceed with the record if this function returns 0
4340 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4341 * 3. return CLIENT_RECONNECT if this function return that value
4342 * 4. drop the whole datagram if this function returns anything else.
4343 * Point 2 is needed when the peer is resending, and we have already received
4344 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004347{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004348 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 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 +02004351
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004353 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004354 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004358 ssl->in_msgtype,
4359 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004361 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4363 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4364 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4365 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004368
4369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004370 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4371 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004372 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4373#endif /* MBEDTLS_SSL_PROTO_DTLS */
4374 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4375 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004378 }
4379
4380 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004381 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4384 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 }
4386
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004387 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4390 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 }
4392
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004393 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004394 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004395 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4398 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004399 }
4400
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004401 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004402 * DTLS-related tests.
4403 * Check epoch before checking length constraint because
4404 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4405 * message gets duplicated before the corresponding Finished message,
4406 * the second ChangeCipherSpec should be discarded because it belongs
4407 * to an old epoch, but not because its length is shorter than
4408 * the minimum record length for packets using the new record transform.
4409 * Note that these two kinds of failures are handled differently,
4410 * as an unexpected record is silently skipped but an invalid
4411 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004412 */
4413#if defined(MBEDTLS_SSL_PROTO_DTLS)
4414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4415 {
4416 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4417
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004418 /* Check epoch (and sequence number) with DTLS */
4419 if( rec_epoch != ssl->in_epoch )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4422 "expected %d, received %d",
4423 ssl->in_epoch, rec_epoch ) );
4424
4425#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4426 /*
4427 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4428 * access the first byte of record content (handshake type), as we
4429 * have an active transform (possibly iv_len != 0), so use the
4430 * fact that the record header len is 13 instead.
4431 */
4432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4433 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4434 rec_epoch == 0 &&
4435 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4436 ssl->in_left > 13 &&
4437 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4438 {
4439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4440 "from the same port" ) );
4441 return( ssl_handle_possible_reconnect( ssl ) );
4442 }
4443 else
4444#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004445 {
4446 /* Consider buffering the record. */
4447 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4450 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4451 }
4452
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004453 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004454 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004455 }
4456
4457#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4458 /* Replay detection only works for the current epoch */
4459 if( rec_epoch == ssl->in_epoch &&
4460 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4461 {
4462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4463 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4464 }
4465#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004466
Hanno Becker52c6dc62017-05-26 16:07:36 +01004467 /* Drop unexpected ApplicationData records,
4468 * except at the beginning of renegotiations */
4469 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4470 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4471#if defined(MBEDTLS_SSL_RENEGOTIATION)
4472 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4473 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4474#endif
4475 )
4476 {
4477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4478 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4479 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004480 }
4481#endif /* MBEDTLS_SSL_PROTO_DTLS */
4482
Hanno Becker52c6dc62017-05-26 16:07:36 +01004483
4484 /* Check length against bounds of the current transform and version */
4485 if( ssl->transform_in == NULL )
4486 {
4487 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004488 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004489 {
4490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4491 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4492 }
4493 }
4494 else
4495 {
4496 if( ssl->in_msglen < ssl->transform_in->minlen )
4497 {
4498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4499 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4500 }
4501
4502#if defined(MBEDTLS_SSL_PROTO_SSL3)
4503 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004504 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004505 {
4506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4508 }
4509#endif
4510#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4511 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4512 /*
4513 * TLS encrypted messages can have up to 256 bytes of padding
4514 */
4515 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4516 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004517 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004518 {
4519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4520 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4521 }
4522#endif
4523 }
4524
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004525 return( 0 );
4526}
Paul Bakker5121ce52009-01-03 21:22:43 +00004527
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004528/*
4529 * If applicable, decrypt (and decompress) record content
4530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004532{
4533 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4536 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4539 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 ret = mbedtls_ssl_hw_record_read( ssl );
4544 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4547 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004548 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004549
4550 if( ret == 0 )
4551 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004552 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004554 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 {
4556 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 return( ret );
4560 }
4561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 ssl->in_msg, ssl->in_msglen );
4564
Angus Grattond8213d02016-05-25 20:56:48 +10004565 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 }
4570 }
4571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004573 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004575 {
4576 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004579 return( ret );
4580 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004581 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004585 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004588 }
4589#endif
4590
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004591 return( 0 );
4592}
4593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004595
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004596/*
4597 * Read a record.
4598 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004599 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4600 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4601 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004602 */
Hanno Becker1097b342018-08-15 14:09:41 +01004603
4604/* Helper functions for mbedtls_ssl_read_record(). */
4605static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004606static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4607static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004608
Hanno Becker327c93b2018-08-15 13:56:18 +01004609int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004610 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004611{
4612 int ret;
4613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004615
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004616 if( ssl->keep_current_message == 0 )
4617 {
4618 do {
Simon Butcher99000142016-10-13 17:21:01 +01004619
Hanno Becker26994592018-08-15 14:14:59 +01004620 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004621 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004622 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004623
Hanno Beckere74d5562018-08-15 14:26:08 +01004624 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004625 {
Hanno Becker40f50842018-08-15 14:48:01 +01004626#if defined(MBEDTLS_SSL_PROTO_DTLS)
4627 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004628
Hanno Becker40f50842018-08-15 14:48:01 +01004629 /* We only check for buffered messages if the
4630 * current datagram is fully consumed. */
4631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004632 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004633 {
Hanno Becker40f50842018-08-15 14:48:01 +01004634 if( ssl_load_buffered_message( ssl ) == 0 )
4635 have_buffered = 1;
4636 }
4637
4638 if( have_buffered == 0 )
4639#endif /* MBEDTLS_SSL_PROTO_DTLS */
4640 {
4641 ret = ssl_get_next_record( ssl );
4642 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4643 continue;
4644
4645 if( ret != 0 )
4646 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004647 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004648 return( ret );
4649 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004650 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004651 }
4652
4653 ret = mbedtls_ssl_handle_message_type( ssl );
4654
Hanno Becker40f50842018-08-15 14:48:01 +01004655#if defined(MBEDTLS_SSL_PROTO_DTLS)
4656 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4657 {
4658 /* Buffer future message */
4659 ret = ssl_buffer_message( ssl );
4660 if( ret != 0 )
4661 return( ret );
4662
4663 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4664 }
4665#endif /* MBEDTLS_SSL_PROTO_DTLS */
4666
Hanno Becker90333da2017-10-10 11:27:13 +01004667 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4668 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004669
4670 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004671 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004672 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004673 return( ret );
4674 }
4675
Hanno Becker327c93b2018-08-15 13:56:18 +01004676 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004677 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004678 {
4679 mbedtls_ssl_update_handshake_status( ssl );
4680 }
Simon Butcher99000142016-10-13 17:21:01 +01004681 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004682 else
Simon Butcher99000142016-10-13 17:21:01 +01004683 {
Hanno Becker02f59072018-08-15 14:00:24 +01004684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004685 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004686 }
4687
4688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4689
4690 return( 0 );
4691}
4692
Hanno Becker40f50842018-08-15 14:48:01 +01004693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004694static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004695{
Hanno Becker40f50842018-08-15 14:48:01 +01004696 if( ssl->in_left > ssl->next_record_offset )
4697 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004698
Hanno Becker40f50842018-08-15 14:48:01 +01004699 return( 0 );
4700}
4701
4702static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4703{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004704 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004705 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004706 int ret = 0;
4707
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708 if( hs == NULL )
4709 return( -1 );
4710
Hanno Beckere00ae372018-08-20 09:39:42 +01004711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4712
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004713 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4714 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4715 {
4716 /* Check if we have seen a ChangeCipherSpec before.
4717 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004718 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4721 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004722 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004723 }
4724
Hanno Becker39b8bc92018-08-28 17:17:13 +01004725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004726 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4727 ssl->in_msglen = 1;
4728 ssl->in_msg[0] = 1;
4729
4730 /* As long as they are equal, the exact value doesn't matter. */
4731 ssl->in_left = 0;
4732 ssl->next_record_offset = 0;
4733
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004734 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004735 goto exit;
4736 }
Hanno Becker37f95322018-08-16 13:55:32 +01004737
Hanno Beckerb8f50142018-08-28 10:01:34 +01004738#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004739 /* Debug only */
4740 {
4741 unsigned offset;
4742 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4743 {
4744 hs_buf = &hs->buffering.hs[offset];
4745 if( hs_buf->is_valid == 1 )
4746 {
4747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4748 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004749 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004750 }
4751 }
4752 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004753#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004754
4755 /* Check if we have buffered and/or fully reassembled the
4756 * next handshake message. */
4757 hs_buf = &hs->buffering.hs[0];
4758 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4759 {
4760 /* Synthesize a record containing the buffered HS message. */
4761 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4762 ( hs_buf->data[2] << 8 ) |
4763 hs_buf->data[3];
4764
4765 /* Double-check that we haven't accidentally buffered
4766 * a message that doesn't fit into the input buffer. */
4767 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4768 {
4769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4771 }
4772
4773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4774 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4775 hs_buf->data, msg_len + 12 );
4776
4777 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4778 ssl->in_hslen = msg_len + 12;
4779 ssl->in_msglen = msg_len + 12;
4780 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4781
4782 ret = 0;
4783 goto exit;
4784 }
4785 else
4786 {
4787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4788 hs->in_msg_seq ) );
4789 }
4790
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004791 ret = -1;
4792
4793exit:
4794
4795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4796 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004797}
4798
Hanno Beckera02b0b42018-08-21 17:20:27 +01004799static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4800 size_t desired )
4801{
4802 int offset;
4803 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4805 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004806
Hanno Becker01315ea2018-08-21 17:22:17 +01004807 /* Get rid of future records epoch first, if such exist. */
4808 ssl_free_buffered_record( ssl );
4809
4810 /* Check if we have enough space available now. */
4811 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4812 hs->buffering.total_bytes_buffered ) )
4813 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004815 return( 0 );
4816 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004817
Hanno Becker4f432ad2018-08-28 10:02:32 +01004818 /* We don't have enough space to buffer the next expected handshake
4819 * message. Remove buffers used for future messages to gain space,
4820 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004821 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4822 offset >= 0; offset-- )
4823 {
4824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4825 offset ) );
4826
Hanno Beckerb309b922018-08-23 13:18:05 +01004827 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004828
4829 /* Check if we have enough space available now. */
4830 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4831 hs->buffering.total_bytes_buffered ) )
4832 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004834 return( 0 );
4835 }
4836 }
4837
4838 return( -1 );
4839}
4840
Hanno Becker40f50842018-08-15 14:48:01 +01004841static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4842{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004843 int ret = 0;
4844 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4845
4846 if( hs == NULL )
4847 return( 0 );
4848
4849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4850
4851 switch( ssl->in_msgtype )
4852 {
4853 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004855
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004856 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004857 break;
4858
4859 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004860 {
4861 unsigned recv_msg_seq_offset;
4862 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4863 mbedtls_ssl_hs_buffer *hs_buf;
4864 size_t msg_len = ssl->in_hslen - 12;
4865
4866 /* We should never receive an old handshake
4867 * message - double-check nonetheless. */
4868 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4869 {
4870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4872 }
4873
4874 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4875 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4876 {
4877 /* Silently ignore -- message too far in the future */
4878 MBEDTLS_SSL_DEBUG_MSG( 2,
4879 ( "Ignore future HS message with sequence number %u, "
4880 "buffering window %u - %u",
4881 recv_msg_seq, ssl->handshake->in_msg_seq,
4882 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4883
4884 goto exit;
4885 }
4886
4887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4888 recv_msg_seq, recv_msg_seq_offset ) );
4889
4890 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4891
4892 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004893 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004894 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004895 size_t reassembly_buf_sz;
4896
Hanno Becker37f95322018-08-16 13:55:32 +01004897 hs_buf->is_fragmented =
4898 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4899
4900 /* We copy the message back into the input buffer
4901 * after reassembly, so check that it's not too large.
4902 * This is an implementation-specific limitation
4903 * and not one from the standard, hence it is not
4904 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004905 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004906 {
4907 /* Ignore message */
4908 goto exit;
4909 }
4910
Hanno Beckere0b150f2018-08-21 15:51:03 +01004911 /* Check if we have enough space to buffer the message. */
4912 if( hs->buffering.total_bytes_buffered >
4913 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4917 }
4918
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004919 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4920 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004921
4922 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4923 hs->buffering.total_bytes_buffered ) )
4924 {
4925 if( recv_msg_seq_offset > 0 )
4926 {
4927 /* If we can't buffer a future message because
4928 * of space limitations -- ignore. */
4929 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",
4930 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4931 (unsigned) hs->buffering.total_bytes_buffered ) );
4932 goto exit;
4933 }
Hanno Beckere1801392018-08-21 16:51:05 +01004934 else
4935 {
4936 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",
4937 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4938 (unsigned) hs->buffering.total_bytes_buffered ) );
4939 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004940
Hanno Beckera02b0b42018-08-21 17:20:27 +01004941 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004942 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004943 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",
4944 (unsigned) msg_len,
4945 (unsigned) reassembly_buf_sz,
4946 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004947 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004948 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4949 goto exit;
4950 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004951 }
4952
4953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4954 msg_len ) );
4955
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004956 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4957 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004958 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004959 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004960 goto exit;
4961 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004962 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004963
4964 /* Prepare final header: copy msg_type, length and message_seq,
4965 * then add standardised fragment_offset and fragment_length */
4966 memcpy( hs_buf->data, ssl->in_msg, 6 );
4967 memset( hs_buf->data + 6, 0, 3 );
4968 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4969
4970 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004971
4972 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004973 }
4974 else
4975 {
4976 /* Make sure msg_type and length are consistent */
4977 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4978 {
4979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4980 /* Ignore */
4981 goto exit;
4982 }
4983 }
4984
Hanno Becker4422bbb2018-08-20 09:40:19 +01004985 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004986 {
4987 size_t frag_len, frag_off;
4988 unsigned char * const msg = hs_buf->data + 12;
4989
4990 /*
4991 * Check and copy current fragment
4992 */
4993
4994 /* Validation of header fields already done in
4995 * mbedtls_ssl_prepare_handshake_record(). */
4996 frag_off = ssl_get_hs_frag_off( ssl );
4997 frag_len = ssl_get_hs_frag_len( ssl );
4998
4999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5000 frag_off, frag_len ) );
5001 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5002
5003 if( hs_buf->is_fragmented )
5004 {
5005 unsigned char * const bitmask = msg + msg_len;
5006 ssl_bitmask_set( bitmask, frag_off, frag_len );
5007 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5008 msg_len ) == 0 );
5009 }
5010 else
5011 {
5012 hs_buf->is_complete = 1;
5013 }
5014
5015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5016 hs_buf->is_complete ? "" : "not yet " ) );
5017 }
5018
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005019 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005020 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005021
5022 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005023 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005024 break;
5025 }
5026
5027exit:
5028
5029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5030 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005031}
5032#endif /* MBEDTLS_SSL_PROTO_DTLS */
5033
Hanno Becker1097b342018-08-15 14:09:41 +01005034static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005035{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005036 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005037 * Consume last content-layer message and potentially
5038 * update in_msglen which keeps track of the contents'
5039 * consumption state.
5040 *
5041 * (1) Handshake messages:
5042 * Remove last handshake message, move content
5043 * and adapt in_msglen.
5044 *
5045 * (2) Alert messages:
5046 * Consume whole record content, in_msglen = 0.
5047 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005048 * (3) Change cipher spec:
5049 * Consume whole record content, in_msglen = 0.
5050 *
5051 * (4) Application data:
5052 * Don't do anything - the record layer provides
5053 * the application data as a stream transport
5054 * and consumes through mbedtls_ssl_read only.
5055 *
5056 */
5057
5058 /* Case (1): Handshake messages */
5059 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005060 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005061 /* Hard assertion to be sure that no application data
5062 * is in flight, as corrupting ssl->in_msglen during
5063 * ssl->in_offt != NULL is fatal. */
5064 if( ssl->in_offt != NULL )
5065 {
5066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5068 }
5069
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005070 /*
5071 * Get next Handshake message in the current record
5072 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005073
Hanno Becker4a810fb2017-05-24 16:27:30 +01005074 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005075 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005076 * current handshake content: If DTLS handshake
5077 * fragmentation is used, that's the fragment
5078 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005079 * size here is faulty and should be changed at
5080 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005081 * (2) While it doesn't seem to cause problems, one
5082 * has to be very careful not to assume that in_hslen
5083 * is always <= in_msglen in a sensible communication.
5084 * Again, it's wrong for DTLS handshake fragmentation.
5085 * The following check is therefore mandatory, and
5086 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005087 * Additionally, ssl->in_hslen might be arbitrarily out of
5088 * bounds after handling a DTLS message with an unexpected
5089 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005090 */
5091 if( ssl->in_hslen < ssl->in_msglen )
5092 {
5093 ssl->in_msglen -= ssl->in_hslen;
5094 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5095 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005096
Hanno Becker4a810fb2017-05-24 16:27:30 +01005097 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5098 ssl->in_msg, ssl->in_msglen );
5099 }
5100 else
5101 {
5102 ssl->in_msglen = 0;
5103 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005104
Hanno Becker4a810fb2017-05-24 16:27:30 +01005105 ssl->in_hslen = 0;
5106 }
5107 /* Case (4): Application data */
5108 else if( ssl->in_offt != NULL )
5109 {
5110 return( 0 );
5111 }
5112 /* Everything else (CCS & Alerts) */
5113 else
5114 {
5115 ssl->in_msglen = 0;
5116 }
5117
Hanno Becker1097b342018-08-15 14:09:41 +01005118 return( 0 );
5119}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005120
Hanno Beckere74d5562018-08-15 14:26:08 +01005121static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5122{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005123 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005124 return( 1 );
5125
5126 return( 0 );
5127}
5128
Hanno Becker5f066e72018-08-16 14:56:31 +01005129#if defined(MBEDTLS_SSL_PROTO_DTLS)
5130
5131static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5132{
5133 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5134 if( hs == NULL )
5135 return;
5136
Hanno Becker01315ea2018-08-21 17:22:17 +01005137 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005138 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005139 hs->buffering.total_bytes_buffered -=
5140 hs->buffering.future_record.len;
5141
5142 mbedtls_free( hs->buffering.future_record.data );
5143 hs->buffering.future_record.data = NULL;
5144 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005145}
5146
5147static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5148{
5149 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5150 unsigned char * rec;
5151 size_t rec_len;
5152 unsigned rec_epoch;
5153
5154 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5155 return( 0 );
5156
5157 if( hs == NULL )
5158 return( 0 );
5159
Hanno Becker5f066e72018-08-16 14:56:31 +01005160 rec = hs->buffering.future_record.data;
5161 rec_len = hs->buffering.future_record.len;
5162 rec_epoch = hs->buffering.future_record.epoch;
5163
5164 if( rec == NULL )
5165 return( 0 );
5166
Hanno Becker4cb782d2018-08-20 11:19:05 +01005167 /* Only consider loading future records if the
5168 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005169 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005170 return( 0 );
5171
Hanno Becker5f066e72018-08-16 14:56:31 +01005172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5173
5174 if( rec_epoch != ssl->in_epoch )
5175 {
5176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5177 goto exit;
5178 }
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5181
5182 /* Double-check that the record is not too large */
5183 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5184 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5185 {
5186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5188 }
5189
5190 memcpy( ssl->in_hdr, rec, rec_len );
5191 ssl->in_left = rec_len;
5192 ssl->next_record_offset = 0;
5193
5194 ssl_free_buffered_record( ssl );
5195
5196exit:
5197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5198 return( 0 );
5199}
5200
5201static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5202{
5203 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5204 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005205 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005206
5207 /* Don't buffer future records outside handshakes. */
5208 if( hs == NULL )
5209 return( 0 );
5210
5211 /* Only buffer handshake records (we are only interested
5212 * in Finished messages). */
5213 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5214 return( 0 );
5215
5216 /* Don't buffer more than one future epoch record. */
5217 if( hs->buffering.future_record.data != NULL )
5218 return( 0 );
5219
Hanno Becker01315ea2018-08-21 17:22:17 +01005220 /* Don't buffer record if there's not enough buffering space remaining. */
5221 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5222 hs->buffering.total_bytes_buffered ) )
5223 {
5224 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",
5225 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5226 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005227 return( 0 );
5228 }
5229
Hanno Becker5f066e72018-08-16 14:56:31 +01005230 /* Buffer record */
5231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5232 ssl->in_epoch + 1 ) );
5233 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5234 rec_hdr_len + ssl->in_msglen );
5235
5236 /* ssl_parse_record_header() only considers records
5237 * of the next epoch as candidates for buffering. */
5238 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005239 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005240
5241 hs->buffering.future_record.data =
5242 mbedtls_calloc( 1, hs->buffering.future_record.len );
5243 if( hs->buffering.future_record.data == NULL )
5244 {
5245 /* If we run out of RAM trying to buffer a
5246 * record from the next epoch, just ignore. */
5247 return( 0 );
5248 }
5249
Hanno Becker01315ea2018-08-21 17:22:17 +01005250 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005251
Hanno Becker01315ea2018-08-21 17:22:17 +01005252 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005253 return( 0 );
5254}
5255
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257
Hanno Beckere74d5562018-08-15 14:26:08 +01005258static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005259{
5260 int ret;
5261
Hanno Becker5f066e72018-08-16 14:56:31 +01005262#if defined(MBEDTLS_SSL_PROTO_DTLS)
5263 /* We might have buffered a future record; if so,
5264 * and if the epoch matches now, load it.
5265 * On success, this call will set ssl->in_left to
5266 * the length of the buffered record, so that
5267 * the calls to ssl_fetch_input() below will
5268 * essentially be no-ops. */
5269 ret = ssl_load_buffered_record( ssl );
5270 if( ret != 0 )
5271 return( ret );
5272#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005277 return( ret );
5278 }
5279
5280 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005283 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5284 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005285 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005286 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5287 {
5288 ret = ssl_buffer_future_record( ssl );
5289 if( ret != 0 )
5290 return( ret );
5291
5292 /* Fall through to handling of unexpected records */
5293 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5294 }
5295
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005296 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5297 {
5298 /* Skip unexpected record (but not whole datagram) */
5299 ssl->next_record_offset = ssl->in_msglen
5300 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005301
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5303 "(header)" ) );
5304 }
5305 else
5306 {
5307 /* Skip invalid record and the rest of the datagram */
5308 ssl->next_record_offset = 0;
5309 ssl->in_left = 0;
5310
5311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5312 "(header)" ) );
5313 }
5314
5315 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005316 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005317 }
5318#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005319 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005320 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321
5322 /*
5323 * Read and optionally decrypt the message contents
5324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5326 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005329 return( ret );
5330 }
5331
5332 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005337 if( ssl->next_record_offset < ssl->in_left )
5338 {
5339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5340 }
5341 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005342 else
5343#endif
5344 ssl->in_left = 0;
5345
5346 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005350 {
5351 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5353 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005354 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005355 /* Except when waiting for Finished as a bad mac here
5356 * probably means something went wrong in the handshake
5357 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5358 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5359 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5360 {
5361#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5362 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5363 {
5364 mbedtls_ssl_send_alert_message( ssl,
5365 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5366 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5367 }
5368#endif
5369 return( ret );
5370 }
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005373 if( ssl->conf->badmac_limit != 0 &&
5374 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5377 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005378 }
5379#endif
5380
Hanno Becker4a810fb2017-05-24 16:27:30 +01005381 /* As above, invalid records cause
5382 * dismissal of the whole datagram. */
5383
5384 ssl->next_record_offset = 0;
5385 ssl->in_left = 0;
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005388 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005389 }
5390
5391 return( ret );
5392 }
5393 else
5394#endif
5395 {
5396 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5398 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 mbedtls_ssl_send_alert_message( ssl,
5401 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5402 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005403 }
5404#endif
5405 return( ret );
5406 }
5407 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005408
Simon Butcher99000142016-10-13 17:21:01 +01005409 return( 0 );
5410}
5411
5412int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5413{
5414 int ret;
5415
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005416 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005417 * Handle particular types of records
5418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 {
Simon Butcher99000142016-10-13 17:21:01 +01005421 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5422 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005423 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 }
5426
Hanno Beckere678eaa2018-08-21 14:57:46 +01005427 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005428 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005429 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005430 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5432 ssl->in_msglen ) );
5433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005434 }
5435
Hanno Beckere678eaa2018-08-21 14:57:46 +01005436 if( ssl->in_msg[0] != 1 )
5437 {
5438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5439 ssl->in_msg[0] ) );
5440 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5441 }
5442
5443#if defined(MBEDTLS_SSL_PROTO_DTLS)
5444 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5445 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5446 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5447 {
5448 if( ssl->handshake == NULL )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5451 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5452 }
5453
5454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5455 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5456 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005457#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005458 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005462 if( ssl->in_msglen != 2 )
5463 {
5464 /* Note: Standard allows for more than one 2 byte alert
5465 to be packed in a single message, but Mbed TLS doesn't
5466 currently support this. */
5467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5468 ssl->in_msglen ) );
5469 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5470 }
5471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 ssl->in_msg[0], ssl->in_msg[1] ) );
5474
5475 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005476 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005481 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 }
5484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5486 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5489 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005491
5492#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5493 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5494 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5495 {
Hanno Becker90333da2017-10-10 11:27:13 +01005496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005497 /* Will be handled when trying to parse ServerHello */
5498 return( 0 );
5499 }
5500#endif
5501
5502#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5503 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5504 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5505 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5506 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5509 /* Will be handled in mbedtls_ssl_parse_certificate() */
5510 return( 0 );
5511 }
5512#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5513
5514 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005515 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 }
5517
Hanno Beckerc76c6192017-06-06 10:03:17 +01005518#if defined(MBEDTLS_SSL_PROTO_DTLS)
5519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5520 ssl->handshake != NULL &&
5521 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5522 {
5523 ssl_handshake_wrapup_free_hs_transform( ssl );
5524 }
5525#endif
5526
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 return( 0 );
5528}
5529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005531{
5532 int ret;
5533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5535 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5536 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005537 {
5538 return( ret );
5539 }
5540
5541 return( 0 );
5542}
5543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005545 unsigned char level,
5546 unsigned char message )
5547{
5548 int ret;
5549
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005550 if( ssl == NULL || ssl->conf == NULL )
5551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005557 ssl->out_msglen = 2;
5558 ssl->out_msg[0] = level;
5559 ssl->out_msg[1] = message;
5560
Hanno Becker67bc7c32018-08-06 11:33:50 +01005561 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005564 return( ret );
5565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005567
5568 return( 0 );
5569}
5570
Hanno Beckerb9d44792019-02-08 07:19:04 +00005571#if defined(MBEDTLS_X509_CRT_PARSE_C)
5572static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5573{
5574#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5575 if( session->peer_cert != NULL )
5576 {
5577 mbedtls_x509_crt_free( session->peer_cert );
5578 mbedtls_free( session->peer_cert );
5579 session->peer_cert = NULL;
5580 }
5581#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5582 if( session->peer_cert_digest != NULL )
5583 {
5584 /* Zeroization is not necessary. */
5585 mbedtls_free( session->peer_cert_digest );
5586 session->peer_cert_digest = NULL;
5587 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5588 session->peer_cert_digest_len = 0;
5589 }
5590#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5591}
5592#endif /* MBEDTLS_X509_CRT_PARSE_C */
5593
Paul Bakker5121ce52009-01-03 21:22:43 +00005594/*
5595 * Handshake functions
5596 */
Hanno Becker21489932019-02-05 13:20:55 +00005597#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005598/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005600{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005601 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5602 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005605
Hanno Becker7177a882019-02-05 13:36:46 +00005606 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005609 ssl->state++;
5610 return( 0 );
5611 }
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615}
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005618{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5620 ssl->handshake->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;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005643 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5644 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005647
Hanno Becker7177a882019-02-05 13:36:46 +00005648 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005651 ssl->state++;
5652 return( 0 );
5653 }
5654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 {
5658 if( ssl->client_auth == 0 )
5659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 ssl->state++;
5662 return( 0 );
5663 }
5664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 /*
5667 * If using SSLv3 and got no cert, send an Alert message
5668 * (otherwise an empty Certificate message will be sent).
5669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5671 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 {
5673 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5675 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5676 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 goto write_msg;
5680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683#endif /* MBEDTLS_SSL_CLI_C */
5684#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5690 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005691 }
5692 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005693#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005696
5697 /*
5698 * 0 . 0 handshake type
5699 * 1 . 3 handshake length
5700 * 4 . 6 length of all certs
5701 * 7 . 9 length of cert. 1
5702 * 10 . n-1 peer certificate
5703 * n . n+2 length of cert. 2
5704 * n+3 . ... upper level cert, etc.
5705 */
5706 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005708
Paul Bakker29087132010-03-21 21:03:34 +00005709 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005710 {
5711 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005712 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005715 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 }
5718
5719 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5720 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5721 ssl->out_msg[i + 2] = (unsigned char)( n );
5722
5723 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5724 i += n; crt = crt->next;
5725 }
5726
5727 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5728 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5729 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5730
5731 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5733 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005734
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005735#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005736write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005738
5739 ssl->state++;
5740
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005741 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 return( ret );
5745 }
5746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
Paul Bakkered27a042013-04-18 22:46:23 +02005749 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005750}
5751
Hanno Becker84879e32019-01-31 07:44:03 +00005752#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005753
5754#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005755static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5756 unsigned char *crt_buf,
5757 size_t crt_buf_len )
5758{
5759 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5760
5761 if( peer_crt == NULL )
5762 return( -1 );
5763
5764 if( peer_crt->raw.len != crt_buf_len )
5765 return( -1 );
5766
Hanno Becker46f34d02019-02-08 14:00:04 +00005767 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005768}
Hanno Becker177475a2019-02-05 17:02:46 +00005769#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5770static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5771 unsigned char *crt_buf,
5772 size_t crt_buf_len )
5773{
5774 int ret;
5775 unsigned char const * const peer_cert_digest =
5776 ssl->session->peer_cert_digest;
5777 mbedtls_md_type_t const peer_cert_digest_type =
5778 ssl->session->peer_cert_digest_type;
5779 mbedtls_md_info_t const * const digest_info =
5780 mbedtls_md_info_from_type( peer_cert_digest_type );
5781 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5782 size_t digest_len;
5783
5784 if( peer_cert_digest == NULL || digest_info == NULL )
5785 return( -1 );
5786
5787 digest_len = mbedtls_md_get_size( digest_info );
5788 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5789 return( -1 );
5790
5791 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5792 if( ret != 0 )
5793 return( -1 );
5794
5795 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5796}
5797#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005798#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005799
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005800/*
5801 * Once the certificate message is read, parse it into a cert chain and
5802 * perform basic checks, but leave actual verification to the caller
5803 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005804static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5805 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005806{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005807 int ret;
5808#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5809 int crt_cnt=0;
5810#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005811 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005812 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5818 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 }
5821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5823 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005826 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5827 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 }
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005832
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005836 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005837
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005838 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005842 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5843 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 }
5846
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005847 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5848 i += 3;
5849
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005850 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 while( i < ssl->in_hslen )
5852 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005853 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005854 if ( i + 3 > ssl->in_hslen ) {
5855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005856 mbedtls_ssl_send_alert_message( ssl,
5857 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5858 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005859 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5860 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005861 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5862 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005863 if( ssl->in_msg[i] != 0 )
5864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005866 mbedtls_ssl_send_alert_message( ssl,
5867 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5868 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005870 }
5871
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005872 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5874 | (unsigned int) ssl->in_msg[i + 2];
5875 i += 3;
5876
5877 if( n < 128 || i + n > ssl->in_hslen )
5878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005880 mbedtls_ssl_send_alert_message( ssl,
5881 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5882 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 }
5885
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005886 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005887#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5888 if( crt_cnt++ == 0 &&
5889 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5890 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005891 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005892 /* During client-side renegotiation, check that the server's
5893 * end-CRTs hasn't changed compared to the initial handshake,
5894 * mitigating the triple handshake attack. On success, reuse
5895 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5897 if( ssl_check_peer_crt_unchanged( ssl,
5898 &ssl->in_msg[i],
5899 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005900 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5902 mbedtls_ssl_send_alert_message( ssl,
5903 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5904 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5905 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005906 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005907
5908 /* Now we can safely free the original chain. */
5909 ssl_clear_peer_cert( ssl->session );
5910 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005911#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5912
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005913 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00005914#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005915 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00005916#else
Hanno Becker353a6f02019-02-26 11:51:34 +00005917 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00005918 * it in-place from the input buffer instead of making a copy. */
5919 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5920#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005921 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005923 case 0: /*ok*/
5924 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5925 /* Ignore certificate with an unknown algorithm: maybe a
5926 prior certificate was already trusted. */
5927 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005928
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005929 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5930 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5931 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005932
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005933 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5934 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5935 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005936
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005937 default:
5938 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5939 crt_parse_der_failed:
5940 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5941 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5942 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 }
5944
5945 i += n;
5946 }
5947
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005948 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005949 return( 0 );
5950}
5951
Hanno Becker4a55f632019-02-05 12:49:06 +00005952#if defined(MBEDTLS_SSL_SRV_C)
5953static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5954{
5955 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5956 return( -1 );
5957
5958#if defined(MBEDTLS_SSL_PROTO_SSL3)
5959 /*
5960 * Check if the client sent an empty certificate
5961 */
5962 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5963 {
5964 if( ssl->in_msglen == 2 &&
5965 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5966 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5967 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5968 {
5969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5970 return( 0 );
5971 }
5972
5973 return( -1 );
5974 }
5975#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5976
5977#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5978 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5979 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5980 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5981 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5982 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5983 {
5984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5985 return( 0 );
5986 }
5987
5988 return( -1 );
5989#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5990 MBEDTLS_SSL_PROTO_TLS1_2 */
5991}
5992#endif /* MBEDTLS_SSL_SRV_C */
5993
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005994/* Check if a certificate message is expected.
5995 * Return either
5996 * - SSL_CERTIFICATE_EXPECTED, or
5997 * - SSL_CERTIFICATE_SKIP
5998 * indicating whether a Certificate message is expected or not.
5999 */
6000#define SSL_CERTIFICATE_EXPECTED 0
6001#define SSL_CERTIFICATE_SKIP 1
6002static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6003 int authmode )
6004{
6005 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006006 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006007
6008 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6009 return( SSL_CERTIFICATE_SKIP );
6010
6011#if defined(MBEDTLS_SSL_SRV_C)
6012 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6013 {
6014 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6015 return( SSL_CERTIFICATE_SKIP );
6016
6017 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6018 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006019 ssl->session_negotiate->verify_result =
6020 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6021 return( SSL_CERTIFICATE_SKIP );
6022 }
6023 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006024#else
6025 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006026#endif /* MBEDTLS_SSL_SRV_C */
6027
6028 return( SSL_CERTIFICATE_EXPECTED );
6029}
6030
Hanno Becker68636192019-02-05 14:36:34 +00006031static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6032 int authmode,
6033 mbedtls_x509_crt *chain,
6034 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006035{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006036 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006037 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006038 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006039 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006040
Hanno Becker8927c832019-04-03 12:52:50 +01006041 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6042 void *p_vrfy;
6043
Hanno Becker68636192019-02-05 14:36:34 +00006044 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6045 return( 0 );
6046
Hanno Becker8927c832019-04-03 12:52:50 +01006047 if( ssl->f_vrfy != NULL )
6048 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006050 f_vrfy = ssl->f_vrfy;
6051 p_vrfy = ssl->p_vrfy;
6052 }
6053 else
6054 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006056 f_vrfy = ssl->conf->f_vrfy;
6057 p_vrfy = ssl->conf->p_vrfy;
6058 }
6059
Hanno Becker68636192019-02-05 14:36:34 +00006060 /*
6061 * Main check: verify certificate
6062 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006063#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6064 if( ssl->conf->f_ca_cb != NULL )
6065 {
6066 ((void) rs_ctx);
6067 have_ca_chain = 1;
6068
6069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006070 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006071 chain,
6072 ssl->conf->f_ca_cb,
6073 ssl->conf->p_ca_cb,
6074 ssl->conf->cert_profile,
6075 ssl->hostname,
6076 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006077 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006078 }
6079 else
6080#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6081 {
6082 mbedtls_x509_crt *ca_chain;
6083 mbedtls_x509_crl *ca_crl;
6084
6085#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6086 if( ssl->handshake->sni_ca_chain != NULL )
6087 {
6088 ca_chain = ssl->handshake->sni_ca_chain;
6089 ca_crl = ssl->handshake->sni_ca_crl;
6090 }
6091 else
6092#endif
6093 {
6094 ca_chain = ssl->conf->ca_chain;
6095 ca_crl = ssl->conf->ca_crl;
6096 }
6097
6098 if( ca_chain != NULL )
6099 have_ca_chain = 1;
6100
6101 ret = mbedtls_x509_crt_verify_restartable(
6102 chain,
6103 ca_chain, ca_crl,
6104 ssl->conf->cert_profile,
6105 ssl->hostname,
6106 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006107 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006108 }
Hanno Becker68636192019-02-05 14:36:34 +00006109
6110 if( ret != 0 )
6111 {
6112 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6113 }
6114
6115#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6116 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6117 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6118#endif
6119
6120 /*
6121 * Secondary checks: always done, but change 'ret' only if it was 0
6122 */
6123
6124#if defined(MBEDTLS_ECP_C)
6125 {
6126 const mbedtls_pk_context *pk = &chain->pk;
6127
6128 /* If certificate uses an EC key, make sure the curve is OK */
6129 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6130 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6131 {
6132 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6133
6134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6135 if( ret == 0 )
6136 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6137 }
6138 }
6139#endif /* MBEDTLS_ECP_C */
6140
6141 if( mbedtls_ssl_check_cert_usage( chain,
6142 ciphersuite_info,
6143 ! ssl->conf->endpoint,
6144 &ssl->session_negotiate->verify_result ) != 0 )
6145 {
6146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6147 if( ret == 0 )
6148 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6149 }
6150
6151 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6152 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6153 * with details encoded in the verification flags. All other kinds
6154 * of error codes, including those from the user provided f_vrfy
6155 * functions, are treated as fatal and lead to a failure of
6156 * ssl_parse_certificate even if verification was optional. */
6157 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6158 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6159 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6160 {
6161 ret = 0;
6162 }
6163
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006164 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006165 {
6166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6167 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6168 }
6169
6170 if( ret != 0 )
6171 {
6172 uint8_t alert;
6173
6174 /* The certificate may have been rejected for several reasons.
6175 Pick one and send the corresponding alert. Which alert to send
6176 may be a subject of debate in some cases. */
6177 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6178 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6179 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6180 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6181 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6182 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6183 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6184 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6185 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6186 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6187 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6188 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6189 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6190 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6191 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6192 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6193 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6194 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6195 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6196 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6197 else
6198 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6199 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6200 alert );
6201 }
6202
6203#if defined(MBEDTLS_DEBUG_C)
6204 if( ssl->session_negotiate->verify_result != 0 )
6205 {
6206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6207 ssl->session_negotiate->verify_result ) );
6208 }
6209 else
6210 {
6211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6212 }
6213#endif /* MBEDTLS_DEBUG_C */
6214
6215 return( ret );
6216}
6217
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006218#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6219static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6220 unsigned char *start, size_t len )
6221{
6222 int ret;
6223 /* Remember digest of the peer's end-CRT. */
6224 ssl->session_negotiate->peer_cert_digest =
6225 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6226 if( ssl->session_negotiate->peer_cert_digest == NULL )
6227 {
6228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6229 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6230 mbedtls_ssl_send_alert_message( ssl,
6231 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6232 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6233
6234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6235 }
6236
6237 ret = mbedtls_md( mbedtls_md_info_from_type(
6238 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6239 start, len,
6240 ssl->session_negotiate->peer_cert_digest );
6241
6242 ssl->session_negotiate->peer_cert_digest_type =
6243 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6244 ssl->session_negotiate->peer_cert_digest_len =
6245 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6246
6247 return( ret );
6248}
6249
6250static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6251 unsigned char *start, size_t len )
6252{
6253 unsigned char *end = start + len;
6254 int ret;
6255
6256 /* Make a copy of the peer's raw public key. */
6257 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6258 ret = mbedtls_pk_parse_subpubkey( &start, end,
6259 &ssl->handshake->peer_pubkey );
6260 if( ret != 0 )
6261 {
6262 /* We should have parsed the public key before. */
6263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6264 }
6265
6266 return( 0 );
6267}
6268#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6269
Hanno Becker68636192019-02-05 14:36:34 +00006270int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6271{
6272 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006273 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006274#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6275 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6276 ? ssl->handshake->sni_authmode
6277 : ssl->conf->authmode;
6278#else
6279 const int authmode = ssl->conf->authmode;
6280#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006281 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006282 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006283
6284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6285
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006286 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6287 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006288 {
6289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006290 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006291 }
6292
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006293#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6294 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006295 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006296 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006297 chain = ssl->handshake->ecrs_peer_cert;
6298 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006299 goto crt_verify;
6300 }
6301#endif
6302
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006303 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006304 {
6305 /* mbedtls_ssl_read_record may have sent an alert already. We
6306 let it decide whether to alert. */
6307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006308 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006309 }
6310
Hanno Becker4a55f632019-02-05 12:49:06 +00006311#if defined(MBEDTLS_SSL_SRV_C)
6312 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6313 {
6314 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006315
6316 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006317 ret = 0;
6318 else
6319 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006320
Hanno Becker6bdfab22019-02-05 13:11:17 +00006321 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006322 }
6323#endif /* MBEDTLS_SSL_SRV_C */
6324
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006325 /* Clear existing peer CRT structure in case we tried to
6326 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006327 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006328
6329 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6330 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006331 {
6332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6333 sizeof( mbedtls_x509_crt ) ) );
6334 mbedtls_ssl_send_alert_message( ssl,
6335 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6336 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006337
Hanno Becker3dad3112019-02-05 17:19:52 +00006338 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6339 goto exit;
6340 }
6341 mbedtls_x509_crt_init( chain );
6342
6343 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006344 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006345 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006346
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006347#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6348 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006349 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006350
6351crt_verify:
6352 if( ssl->handshake->ecrs_enabled)
6353 rs_ctx = &ssl->handshake->ecrs_ctx;
6354#endif
6355
Hanno Becker68636192019-02-05 14:36:34 +00006356 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006357 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006358 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006359 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006360
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006361#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006362 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006363 unsigned char *crt_start, *pk_start;
6364 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006365
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006366 /* We parse the CRT chain without copying, so
6367 * these pointers point into the input buffer,
6368 * and are hence still valid after freeing the
6369 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006370
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006371 crt_start = chain->raw.p;
6372 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006373
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006374 pk_start = chain->pk_raw.p;
6375 pk_len = chain->pk_raw.len;
6376
6377 /* Free the CRT structures before computing
6378 * digest and copying the peer's public key. */
6379 mbedtls_x509_crt_free( chain );
6380 mbedtls_free( chain );
6381 chain = NULL;
6382
6383 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006384 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006385 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006386
6387 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6388 if( ret != 0 )
6389 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006390 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006391#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6392 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006393 ssl->session_negotiate->peer_cert = chain;
6394 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006395#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398
Hanno Becker6bdfab22019-02-05 13:11:17 +00006399exit:
6400
Hanno Becker3dad3112019-02-05 17:19:52 +00006401 if( ret == 0 )
6402 ssl->state++;
6403
6404#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6405 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6406 {
6407 ssl->handshake->ecrs_peer_cert = chain;
6408 chain = NULL;
6409 }
6410#endif
6411
6412 if( chain != NULL )
6413 {
6414 mbedtls_x509_crt_free( chain );
6415 mbedtls_free( chain );
6416 }
6417
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 return( ret );
6419}
Hanno Becker21489932019-02-05 13:20:55 +00006420#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006423{
6424 int ret;
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 ssl->out_msglen = 1;
6430 ssl->out_msg[0] = 1;
6431
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 ssl->state++;
6433
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006434 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 return( ret );
6438 }
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006441
6442 return( 0 );
6443}
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006446{
6447 int ret;
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006450
Hanno Becker327c93b2018-08-15 13:56:18 +01006451 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 return( ret );
6455 }
6456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 }
6464
Hanno Beckere678eaa2018-08-21 14:57:46 +01006465 /* CCS records are only accepted if they have length 1 and content '1',
6466 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006467
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006468 /*
6469 * Switch to our negotiated transform and session parameters for inbound
6470 * data.
6471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006473 ssl->transform_in = ssl->transform_negotiate;
6474 ssl->session_in = ssl->session_negotiate;
6475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006477 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006480 ssl_dtls_replay_reset( ssl );
6481#endif
6482
6483 /* Increment epoch */
6484 if( ++ssl->in_epoch == 0 )
6485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006487 /* This is highly unlikely to happen for legitimate reasons, so
6488 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006490 }
6491 }
6492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006494 memset( ssl->in_ctr, 0, 8 );
6495
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006496 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6499 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6505 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006507 }
6508 }
6509#endif
6510
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 ssl->state++;
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006514
6515 return( 0 );
6516}
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006520{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006521 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6524 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6525 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006526 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006527 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6530#if defined(MBEDTLS_SHA512_C)
6531 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6533 else
6534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535#if defined(MBEDTLS_SHA256_C)
6536 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006537 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006538 else
6539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006543 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006544 }
Paul Bakker380da532012-04-18 16:10:25 +00006545}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6550 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006551 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6552 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6555#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006556#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006557 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006558 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6559#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006560 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006561#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006564#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006565 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006566 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006567#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006568 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006569#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006572}
6573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006575 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6578 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006579 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6580 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6583#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006584#if defined(MBEDTLS_USE_PSA_CRYPTO)
6585 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6586#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006587 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006588#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006591#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006592 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006593#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006594 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006595#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006598}
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6601 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6602static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006603 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006604{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006605 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6606 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006607}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006608#endif
Paul Bakker380da532012-04-18 16:10:25 +00006609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6611#if defined(MBEDTLS_SHA256_C)
6612static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006613 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006614{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006615#if defined(MBEDTLS_USE_PSA_CRYPTO)
6616 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6617#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006618 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006619#endif
Paul Bakker380da532012-04-18 16:10:25 +00006620}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006621#endif
Paul Bakker380da532012-04-18 16:10:25 +00006622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623#if defined(MBEDTLS_SHA512_C)
6624static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006625 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006626{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006628 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006629#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006630 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006631#endif
Paul Bakker380da532012-04-18 16:10:25 +00006632}
Paul Bakker769075d2012-11-24 11:26:46 +01006633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006637static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006639{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006640 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006641 mbedtls_md5_context md5;
6642 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006643
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 unsigned char padbuf[48];
6645 unsigned char md5sum[16];
6646 unsigned char sha1sum[20];
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006649 if( !session )
6650 session = ssl->session;
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006653
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006654 mbedtls_md5_init( &md5 );
6655 mbedtls_sha1_init( &sha1 );
6656
6657 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6658 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
6660 /*
6661 * SSLv3:
6662 * hash =
6663 * MD5( master + pad2 +
6664 * MD5( handshake + sender + master + pad1 ) )
6665 * + SHA1( master + pad2 +
6666 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 */
6668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006670 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6671 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006675 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6676 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006677#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006680 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006681
Paul Bakker1ef83d62012-04-11 12:09:53 +00006682 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006684 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6685 mbedtls_md5_update_ret( &md5, session->master, 48 );
6686 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6687 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006689 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6690 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6691 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6692 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Paul Bakker1ef83d62012-04-11 12:09:53 +00006694 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006696 mbedtls_md5_starts_ret( &md5 );
6697 mbedtls_md5_update_ret( &md5, session->master, 48 );
6698 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6699 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6700 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006702 mbedtls_sha1_starts_ret( &sha1 );
6703 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6704 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6705 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6706 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006709
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006710 mbedtls_md5_free( &md5 );
6711 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006712
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006713 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6714 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6715 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006722static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006724{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006725 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006726 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006727 mbedtls_md5_context md5;
6728 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006729 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006732 if( !session )
6733 session = ssl->session;
6734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006736
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006737 mbedtls_md5_init( &md5 );
6738 mbedtls_sha1_init( &sha1 );
6739
6740 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6741 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
Paul Bakker1ef83d62012-04-11 12:09:53 +00006743 /*
6744 * TLSv1:
6745 * hash = PRF( master, finished_label,
6746 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6747 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006750 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6751 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006752#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006755 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6756 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006757#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006760 ? "client finished"
6761 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006762
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006763 mbedtls_md5_finish_ret( &md5, padbuf );
6764 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006765
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006766 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006767 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006770
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006771 mbedtls_md5_free( &md5 );
6772 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006774 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6781#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006782static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784{
6785 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006786 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006787 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006788#if defined(MBEDTLS_USE_PSA_CRYPTO)
6789 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006790 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006791 psa_status_t status;
6792#else
6793 mbedtls_sha256_context sha256;
6794#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006797 if( !session )
6798 session = ssl->session;
6799
Andrzej Kurekeb342242019-01-29 09:14:33 -05006800 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6801 ? "client finished"
6802 : "server finished";
6803
6804#if defined(MBEDTLS_USE_PSA_CRYPTO)
6805 sha256_psa = psa_hash_operation_init();
6806
6807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6808
6809 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6810 if( status != PSA_SUCCESS )
6811 {
6812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6813 return;
6814 }
6815
6816 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6817 if( status != PSA_SUCCESS )
6818 {
6819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6820 return;
6821 }
6822 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6823#else
6824
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006825 mbedtls_sha256_init( &sha256 );
6826
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006828
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006829 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006830
6831 /*
6832 * TLSv1.2:
6833 * hash = PRF( master, finished_label,
6834 * Hash( handshake ) )[0.11]
6835 */
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837#if !defined(MBEDTLS_SHA256_ALT)
6838 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006839 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006840#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006841
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006842 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006843 mbedtls_sha256_free( &sha256 );
6844#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006845
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006846 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006847 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006850
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006851 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006858static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006860{
6861 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006862 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006863 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006864#if defined(MBEDTLS_USE_PSA_CRYPTO)
6865 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006866 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006867 psa_status_t status;
6868#else
6869 mbedtls_sha512_context sha512;
6870#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006873 if( !session )
6874 session = ssl->session;
6875
Andrzej Kurekeb342242019-01-29 09:14:33 -05006876 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6877 ? "client finished"
6878 : "server finished";
6879
6880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006881 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006882
6883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6884
Andrzej Kurek972fba52019-01-30 03:29:12 -05006885 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006886 if( status != PSA_SUCCESS )
6887 {
6888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6889 return;
6890 }
6891
Andrzej Kurek972fba52019-01-30 03:29:12 -05006892 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006893 if( status != PSA_SUCCESS )
6894 {
6895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6896 return;
6897 }
6898 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6899#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006900 mbedtls_sha512_init( &sha512 );
6901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006903
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006904 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006905
6906 /*
6907 * TLSv1.2:
6908 * hash = PRF( master, finished_label,
6909 * Hash( handshake ) )[0.11]
6910 */
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6914 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006915#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006916
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006917 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006918 mbedtls_sha512_free( &sha512 );
6919#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006920
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006921 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006922 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006925
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006926 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006929}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#endif /* MBEDTLS_SHA512_C */
6931#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006936
6937 /*
6938 * Free our handshake params
6939 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006940 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006942 ssl->handshake = NULL;
6943
6944 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006945 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006946 */
6947 if( ssl->transform )
6948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 mbedtls_ssl_transform_free( ssl->transform );
6950 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006951 }
6952 ssl->transform = ssl->transform_negotiate;
6953 ssl->transform_negotiate = NULL;
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006956}
6957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006959{
6960 int resume = ssl->handshake->resume;
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_RENEGOTIATION)
6965 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006968 ssl->renego_records_seen = 0;
6969 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006970#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006971
6972 /*
6973 * Free the previous session and switch in the current one
6974 */
Paul Bakker0a597072012-09-25 21:55:46 +00006975 if( ssl->session )
6976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006978 /* RFC 7366 3.1: keep the EtM state */
6979 ssl->session_negotiate->encrypt_then_mac =
6980 ssl->session->encrypt_then_mac;
6981#endif
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 mbedtls_ssl_session_free( ssl->session );
6984 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006985 }
6986 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006987 ssl->session_negotiate = NULL;
6988
Paul Bakker0a597072012-09-25 21:55:46 +00006989 /*
6990 * Add cache entry
6991 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006992 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006993 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006994 resume == 0 )
6995 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006996 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006998 }
Paul Bakker0a597072012-09-25 21:55:46 +00006999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007002 ssl->handshake->flight != NULL )
7003 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007004 /* Cancel handshake timer */
7005 ssl_set_timer( ssl, 0 );
7006
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007007 /* Keep last flight around in case we need to resend it:
7008 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007010 }
7011 else
7012#endif
7013 ssl_handshake_wrapup_free_hs_transform( ssl );
7014
Paul Bakker48916f92012-09-16 19:57:18 +00007015 ssl->state++;
7016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007018}
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007021{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007022 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007025
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007026 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007027
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007028 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007029
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007030 /*
7031 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7032 * may define some other value. Currently (early 2016), no defined
7033 * ciphersuite does this (and this is unlikely to change as activity has
7034 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007039 ssl->verify_data_len = hash_len;
7040 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007041#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007042
Paul Bakker5121ce52009-01-03 21:22:43 +00007043 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7045 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
7047 /*
7048 * In case of session resuming, invert the client and server
7049 * ChangeCipherSpec messages order.
7050 */
Paul Bakker0a597072012-09-25 21:55:46 +00007051 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007054 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007058 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007061 }
7062 else
7063 ssl->state++;
7064
Paul Bakker48916f92012-09-16 19:57:18 +00007065 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007066 * Switch to our negotiated transform and session parameters for outbound
7067 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007073 {
7074 unsigned char i;
7075
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007076 /* Remember current epoch settings for resending */
7077 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007078 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007079
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007080 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007081 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007082
7083 /* Increment epoch */
7084 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007085 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007086 break;
7087
7088 /* The loop goes to its end iff the counter is wrapping */
7089 if( i == 0 )
7090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7092 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007093 }
7094 }
7095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007097 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007099 ssl->transform_out = ssl->transform_negotiate;
7100 ssl->session_out = ssl->session_negotiate;
7101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7103 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7108 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007109 }
7110 }
7111#endif
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007116#endif
7117
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007118 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 return( ret );
7122 }
7123
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007124#if defined(MBEDTLS_SSL_PROTO_DTLS)
7125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7126 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7127 {
7128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7129 return( ret );
7130 }
7131#endif
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134
7135 return( 0 );
7136}
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007139#define SSL_MAX_HASH_LEN 36
7140#else
7141#define SSL_MAX_HASH_LEN 12
7142#endif
7143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007145{
Paul Bakker23986e52011-04-24 08:57:21 +00007146 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007147 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007148 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007151
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007152 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007153
Hanno Becker327c93b2018-08-15 13:56:18 +01007154 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007157 return( ret );
7158 }
7159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 }
7167
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007168 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#if defined(MBEDTLS_SSL_PROTO_SSL3)
7170 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007171 hash_len = 36;
7172 else
7173#endif
7174 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7177 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7181 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 }
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007186 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007189 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7190 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 }
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007195 ssl->verify_data_len = hash_len;
7196 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007197#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007198
Paul Bakker0a597072012-09-25 21:55:46 +00007199 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007202 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007206 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007209 }
7210 else
7211 ssl->state++;
7212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007216#endif
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007219
7220 return( 0 );
7221}
7222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7228 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7229 mbedtls_md5_init( &handshake->fin_md5 );
7230 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007231 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7232 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7235#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#if defined(MBEDTLS_USE_PSA_CRYPTO)
7237 handshake->fin_sha256_psa = psa_hash_operation_init();
7238 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7239#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007241 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007242#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007246 handshake->fin_sha384_psa = psa_hash_operation_init();
7247 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007248#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007250 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007251#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007254
7255 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007256
7257#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7258 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7259 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7260#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_DHM_C)
7263 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_ECDH_C)
7266 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007267#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007268#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007269 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007270#if defined(MBEDTLS_SSL_CLI_C)
7271 handshake->ecjpake_cache = NULL;
7272 handshake->ecjpake_cache_len = 0;
7273#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007274#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007275
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007276#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007277 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007278#endif
7279
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007280#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7281 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7282#endif
Hanno Becker75173122019-02-06 16:18:31 +00007283
7284#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7285 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7286 mbedtls_pk_init( &handshake->peer_pubkey );
7287#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007288}
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7295 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 mbedtls_md_init( &transform->md_ctx_enc );
7298 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007299}
7300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007304}
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007307{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007308 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007309 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007311 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007313 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007314 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315
7316 /*
7317 * Either the pointers are now NULL or cleared properly and can be freed.
7318 * Now allocate missing structures.
7319 */
7320 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007321 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007322 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007323 }
Paul Bakker48916f92012-09-16 19:57:18 +00007324
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007325 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007326 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007327 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007328 }
Paul Bakker48916f92012-09-16 19:57:18 +00007329
Paul Bakker82788fb2014-10-20 13:59:19 +02007330 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007331 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007332 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007333 }
Paul Bakker48916f92012-09-16 19:57:18 +00007334
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007336 if( ssl->handshake == NULL ||
7337 ssl->transform_negotiate == NULL ||
7338 ssl->session_negotiate == NULL )
7339 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 mbedtls_free( ssl->handshake );
7343 mbedtls_free( ssl->transform_negotiate );
7344 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007345
7346 ssl->handshake = NULL;
7347 ssl->transform_negotiate = NULL;
7348 ssl->session_negotiate = NULL;
7349
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007350 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007351 }
7352
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007353 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007355 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007356 ssl_handshake_params_init( ssl->handshake );
7357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7360 {
7361 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007362
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7364 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7365 else
7366 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007367
7368 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007369 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007370#endif
7371
Paul Bakker48916f92012-09-16 19:57:18 +00007372 return( 0 );
7373}
7374
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007375#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007376/* Dummy cookie callbacks for defaults */
7377static int ssl_cookie_write_dummy( void *ctx,
7378 unsigned char **p, unsigned char *end,
7379 const unsigned char *cli_id, size_t cli_id_len )
7380{
7381 ((void) ctx);
7382 ((void) p);
7383 ((void) end);
7384 ((void) cli_id);
7385 ((void) cli_id_len);
7386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007388}
7389
7390static int ssl_cookie_check_dummy( void *ctx,
7391 const unsigned char *cookie, size_t cookie_len,
7392 const unsigned char *cli_id, size_t cli_id_len )
7393{
7394 ((void) ctx);
7395 ((void) cookie);
7396 ((void) cookie_len);
7397 ((void) cli_id);
7398 ((void) cli_id_len);
7399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007401}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007402#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007403
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007404/* Once ssl->out_hdr as the address of the beginning of the
7405 * next outgoing record is set, deduce the other pointers.
7406 *
7407 * Note: For TLS, we save the implicit record sequence number
7408 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7409 * and the caller has to make sure there's space for this.
7410 */
7411
7412static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7413 mbedtls_ssl_transform *transform )
7414{
7415#if defined(MBEDTLS_SSL_PROTO_DTLS)
7416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7417 {
7418 ssl->out_ctr = ssl->out_hdr + 3;
7419 ssl->out_len = ssl->out_hdr + 11;
7420 ssl->out_iv = ssl->out_hdr + 13;
7421 }
7422 else
7423#endif
7424 {
7425 ssl->out_ctr = ssl->out_hdr - 8;
7426 ssl->out_len = ssl->out_hdr + 3;
7427 ssl->out_iv = ssl->out_hdr + 5;
7428 }
7429
7430 /* Adjust out_msg to make space for explicit IV, if used. */
7431 if( transform != NULL &&
7432 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7433 {
7434 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7435 }
7436 else
7437 ssl->out_msg = ssl->out_iv;
7438}
7439
7440/* Once ssl->in_hdr as the address of the beginning of the
7441 * next incoming record is set, deduce the other pointers.
7442 *
7443 * Note: For TLS, we save the implicit record sequence number
7444 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7445 * and the caller has to make sure there's space for this.
7446 */
7447
7448static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7449 mbedtls_ssl_transform *transform )
7450{
7451#if defined(MBEDTLS_SSL_PROTO_DTLS)
7452 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7453 {
7454 ssl->in_ctr = ssl->in_hdr + 3;
7455 ssl->in_len = ssl->in_hdr + 11;
7456 ssl->in_iv = ssl->in_hdr + 13;
7457 }
7458 else
7459#endif
7460 {
7461 ssl->in_ctr = ssl->in_hdr - 8;
7462 ssl->in_len = ssl->in_hdr + 3;
7463 ssl->in_iv = ssl->in_hdr + 5;
7464 }
7465
7466 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7467 if( transform != NULL &&
7468 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7469 {
7470 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7471 }
7472 else
7473 ssl->in_msg = ssl->in_iv;
7474}
7475
Paul Bakker5121ce52009-01-03 21:22:43 +00007476/*
7477 * Initialize an SSL context
7478 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007479void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7480{
7481 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7482}
7483
7484/*
7485 * Setup an SSL context
7486 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007487
7488static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7489{
7490 /* Set the incoming and outgoing record pointers. */
7491#if defined(MBEDTLS_SSL_PROTO_DTLS)
7492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7493 {
7494 ssl->out_hdr = ssl->out_buf;
7495 ssl->in_hdr = ssl->in_buf;
7496 }
7497 else
7498#endif /* MBEDTLS_SSL_PROTO_DTLS */
7499 {
7500 ssl->out_hdr = ssl->out_buf + 8;
7501 ssl->in_hdr = ssl->in_buf + 8;
7502 }
7503
7504 /* Derive other internal pointers. */
7505 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7506 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7507}
7508
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007509int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007510 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007511{
Paul Bakker48916f92012-09-16 19:57:18 +00007512 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007514 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007515
7516 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007517 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007518 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007519
7520 /* Set to NULL in case of an error condition */
7521 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007522
Angus Grattond8213d02016-05-25 20:56:48 +10007523 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7524 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007525 {
Angus Grattond8213d02016-05-25 20:56:48 +10007526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007527 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007528 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007529 }
7530
7531 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7532 if( ssl->out_buf == NULL )
7533 {
7534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007535 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007536 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 }
7538
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007539 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007540
Paul Bakker48916f92012-09-16 19:57:18 +00007541 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007542 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007543
7544 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007545
7546error:
7547 mbedtls_free( ssl->in_buf );
7548 mbedtls_free( ssl->out_buf );
7549
7550 ssl->conf = NULL;
7551
7552 ssl->in_buf = NULL;
7553 ssl->out_buf = NULL;
7554
7555 ssl->in_hdr = NULL;
7556 ssl->in_ctr = NULL;
7557 ssl->in_len = NULL;
7558 ssl->in_iv = NULL;
7559 ssl->in_msg = NULL;
7560
7561 ssl->out_hdr = NULL;
7562 ssl->out_ctr = NULL;
7563 ssl->out_len = NULL;
7564 ssl->out_iv = NULL;
7565 ssl->out_msg = NULL;
7566
k-stachowiak9f7798e2018-07-31 16:52:32 +02007567 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007568}
7569
7570/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007571 * Reset an initialized and used SSL context for re-use while retaining
7572 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007573 *
7574 * If partial is non-zero, keep data in the input buffer and client ID.
7575 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007576 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007577static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007578{
Paul Bakker48916f92012-09-16 19:57:18 +00007579 int ret;
7580
Hanno Becker7e772132018-08-10 12:38:21 +01007581#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7582 !defined(MBEDTLS_SSL_SRV_C)
7583 ((void) partial);
7584#endif
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007587
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007588 /* Cancel any possibly running timer */
7589 ssl_set_timer( ssl, 0 );
7590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#if defined(MBEDTLS_SSL_RENEGOTIATION)
7592 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007593 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007594
7595 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7597 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007600
Paul Bakker7eb013f2011-10-06 12:37:39 +00007601 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007602 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007603
7604 ssl->in_msgtype = 0;
7605 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007607 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007608 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007611 ssl_dtls_replay_reset( ssl );
7612#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007613
7614 ssl->in_hslen = 0;
7615 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007616
7617 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007618
7619 ssl->out_msgtype = 0;
7620 ssl->out_msglen = 0;
7621 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7623 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007624 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007625#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007626
Hanno Becker19859472018-08-06 09:40:20 +01007627 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7628
Paul Bakker48916f92012-09-16 19:57:18 +00007629 ssl->transform_in = NULL;
7630 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007631
Hanno Becker78640902018-08-13 16:35:15 +01007632 ssl->session_in = NULL;
7633 ssl->session_out = NULL;
7634
Angus Grattond8213d02016-05-25 20:56:48 +10007635 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007636
7637#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007638 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007639#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7640 {
7641 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007642 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007643 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7646 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7649 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7652 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007653 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007654 }
7655#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007656
Paul Bakker48916f92012-09-16 19:57:18 +00007657 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 mbedtls_ssl_transform_free( ssl->transform );
7660 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007661 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007662 }
Paul Bakker48916f92012-09-16 19:57:18 +00007663
Paul Bakkerc0463502013-02-14 11:19:38 +01007664 if( ssl->session )
7665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 mbedtls_ssl_session_free( ssl->session );
7667 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007668 ssl->session = NULL;
7669 }
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007672 ssl->alpn_chosen = NULL;
7673#endif
7674
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007675#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007676#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007677 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007678#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007679 {
7680 mbedtls_free( ssl->cli_id );
7681 ssl->cli_id = NULL;
7682 ssl->cli_id_len = 0;
7683 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007684#endif
7685
Paul Bakker48916f92012-09-16 19:57:18 +00007686 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7687 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007688
7689 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007690}
7691
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007692/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007693 * Reset an initialized and used SSL context for re-use while retaining
7694 * all application-set variables, function pointers and data.
7695 */
7696int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7697{
7698 return( ssl_session_reset_int( ssl, 0 ) );
7699}
7700
7701/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007702 * SSL set accessors
7703 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007704void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007706 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007707}
7708
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007709void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007711 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007712}
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007715void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007716{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007717 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007718}
7719#endif
7720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007722void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007723{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007724 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007725}
7726#endif
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007729
Hanno Becker1841b0a2018-08-24 11:13:57 +01007730void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7731 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007732{
7733 ssl->disable_datagram_packing = !allow_packing;
7734}
7735
7736void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7737 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007739 conf->hs_timeout_min = min;
7740 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007741}
7742#endif
7743
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007744void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007745{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007746 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007747}
7748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007750void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007751 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007752 void *p_vrfy )
7753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->f_vrfy = f_vrfy;
7755 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007758
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007759void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007760 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 void *p_rng )
7762{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007763 conf->f_rng = f_rng;
7764 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007765}
7766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007767void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007768 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007769 void *p_dbg )
7770{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007771 conf->f_dbg = f_dbg;
7772 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007773}
7774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007776 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007777 mbedtls_ssl_send_t *f_send,
7778 mbedtls_ssl_recv_t *f_recv,
7779 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007780{
7781 ssl->p_bio = p_bio;
7782 ssl->f_send = f_send;
7783 ssl->f_recv = f_recv;
7784 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007785}
7786
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007787#if defined(MBEDTLS_SSL_PROTO_DTLS)
7788void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7789{
7790 ssl->mtu = mtu;
7791}
7792#endif
7793
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007794void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007795{
7796 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007797}
7798
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007799void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7800 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007801 mbedtls_ssl_set_timer_t *f_set_timer,
7802 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007803{
7804 ssl->p_timer = p_timer;
7805 ssl->f_set_timer = f_set_timer;
7806 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007807
7808 /* Make sure we start with no timer running */
7809 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007810}
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007813void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007814 void *p_cache,
7815 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7816 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007817{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007818 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007819 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824#if defined(MBEDTLS_SSL_CLI_C)
7825int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007827 int ret;
7828
7829 if( ssl == NULL ||
7830 session == NULL ||
7831 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007832 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007835 }
7836
Hanno Becker52055ae2019-02-06 14:30:46 +00007837 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7838 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007839 return( ret );
7840
Paul Bakker0a597072012-09-25 21:55:46 +00007841 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007842
7843 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007847void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007848 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007850 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7851 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7852 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7853 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007854}
7855
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007856void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007857 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007858 int major, int minor )
7859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007861 return;
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007864 return;
7865
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007866 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007867}
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007870void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007871 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007872{
7873 conf->cert_profile = profile;
7874}
7875
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007876/* Append a new keycert entry to a (possibly empty) list */
7877static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7878 mbedtls_x509_crt *cert,
7879 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007880{
niisato8ee24222018-06-25 19:05:48 +09007881 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007882
niisato8ee24222018-06-25 19:05:48 +09007883 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7884 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007886
niisato8ee24222018-06-25 19:05:48 +09007887 new_cert->cert = cert;
7888 new_cert->key = key;
7889 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007890
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007891 /* Update head is the list was null, else add to the end */
7892 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007893 {
niisato8ee24222018-06-25 19:05:48 +09007894 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007895 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007896 else
7897 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007898 mbedtls_ssl_key_cert *cur = *head;
7899 while( cur->next != NULL )
7900 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007901 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007902 }
7903
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007904 return( 0 );
7905}
7906
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007907int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007908 mbedtls_x509_crt *own_cert,
7909 mbedtls_pk_context *pk_key )
7910{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007911 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007912}
7913
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007914void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007915 mbedtls_x509_crt *ca_chain,
7916 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007917{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007918 conf->ca_chain = ca_chain;
7919 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00007920
7921#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7922 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7923 * cannot be used together. */
7924 conf->f_ca_cb = NULL;
7925 conf->p_ca_cb = NULL;
7926#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00007927}
Hanno Becker5adaad92019-03-27 16:54:37 +00007928
7929#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7930void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007931 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00007932 void *p_ca_cb )
7933{
7934 conf->f_ca_cb = f_ca_cb;
7935 conf->p_ca_cb = p_ca_cb;
7936
7937 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
7938 * cannot be used together. */
7939 conf->ca_chain = NULL;
7940 conf->ca_crl = NULL;
7941}
7942#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007944
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007945#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7946int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7947 mbedtls_x509_crt *own_cert,
7948 mbedtls_pk_context *pk_key )
7949{
7950 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7951 own_cert, pk_key ) );
7952}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007953
7954void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7955 mbedtls_x509_crt *ca_chain,
7956 mbedtls_x509_crl *ca_crl )
7957{
7958 ssl->handshake->sni_ca_chain = ca_chain;
7959 ssl->handshake->sni_ca_crl = ca_crl;
7960}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007961
7962void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7963 int authmode )
7964{
7965 ssl->handshake->sni_authmode = authmode;
7966}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007967#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7968
Hanno Becker8927c832019-04-03 12:52:50 +01007969#if defined(MBEDTLS_X509_CRT_PARSE_C)
7970void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
7971 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
7972 void *p_vrfy )
7973{
7974 ssl->f_vrfy = f_vrfy;
7975 ssl->p_vrfy = p_vrfy;
7976}
7977#endif
7978
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007979#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007980/*
7981 * Set EC J-PAKE password for current handshake
7982 */
7983int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7984 const unsigned char *pw,
7985 size_t pw_len )
7986{
7987 mbedtls_ecjpake_role role;
7988
Janos Follath8eb64132016-06-03 15:40:57 +01007989 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7991
7992 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7993 role = MBEDTLS_ECJPAKE_SERVER;
7994 else
7995 role = MBEDTLS_ECJPAKE_CLIENT;
7996
7997 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7998 role,
7999 MBEDTLS_MD_SHA256,
8000 MBEDTLS_ECP_DP_SECP256R1,
8001 pw, pw_len ) );
8002}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008003#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008006
8007static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8008{
8009 /* Remove reference to existing PSK, if any. */
8010#if defined(MBEDTLS_USE_PSA_CRYPTO)
8011 if( conf->psk_opaque != 0 )
8012 {
8013 /* The maintenance of the PSK key slot is the
8014 * user's responsibility. */
8015 conf->psk_opaque = 0;
8016 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008017 /* This and the following branch should never
8018 * be taken simultaenously as we maintain the
8019 * invariant that raw and opaque PSKs are never
8020 * configured simultaneously. As a safeguard,
8021 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008022#endif /* MBEDTLS_USE_PSA_CRYPTO */
8023 if( conf->psk != NULL )
8024 {
8025 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8026
8027 mbedtls_free( conf->psk );
8028 conf->psk = NULL;
8029 conf->psk_len = 0;
8030 }
8031
8032 /* Remove reference to PSK identity, if any. */
8033 if( conf->psk_identity != NULL )
8034 {
8035 mbedtls_free( conf->psk_identity );
8036 conf->psk_identity = NULL;
8037 conf->psk_identity_len = 0;
8038 }
8039}
8040
Hanno Becker7390c712018-11-15 13:33:04 +00008041/* This function assumes that PSK identity in the SSL config is unset.
8042 * It checks that the provided identity is well-formed and attempts
8043 * to make a copy of it in the SSL config.
8044 * On failure, the PSK identity in the config remains unset. */
8045static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8046 unsigned char const *psk_identity,
8047 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008048{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008049 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008050 if( psk_identity == NULL ||
8051 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008052 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008053 {
8054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8055 }
8056
Hanno Becker7390c712018-11-15 13:33:04 +00008057 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8058 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008060
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008061 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008062 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008063
8064 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008065}
8066
Hanno Becker7390c712018-11-15 13:33:04 +00008067int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8068 const unsigned char *psk, size_t psk_len,
8069 const unsigned char *psk_identity, size_t psk_identity_len )
8070{
8071 int ret;
8072 /* Remove opaque/raw PSK + PSK Identity */
8073 ssl_conf_remove_psk( conf );
8074
8075 /* Check and set raw PSK */
8076 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8078 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8079 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8080 conf->psk_len = psk_len;
8081 memcpy( conf->psk, psk, conf->psk_len );
8082
8083 /* Check and set PSK Identity */
8084 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8085 if( ret != 0 )
8086 ssl_conf_remove_psk( conf );
8087
8088 return( ret );
8089}
8090
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008091static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8092{
8093#if defined(MBEDTLS_USE_PSA_CRYPTO)
8094 if( ssl->handshake->psk_opaque != 0 )
8095 {
8096 ssl->handshake->psk_opaque = 0;
8097 }
8098 else
8099#endif /* MBEDTLS_USE_PSA_CRYPTO */
8100 if( ssl->handshake->psk != NULL )
8101 {
8102 mbedtls_platform_zeroize( ssl->handshake->psk,
8103 ssl->handshake->psk_len );
8104 mbedtls_free( ssl->handshake->psk );
8105 ssl->handshake->psk_len = 0;
8106 }
8107}
8108
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008109int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8110 const unsigned char *psk, size_t psk_len )
8111{
8112 if( psk == NULL || ssl->handshake == NULL )
8113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8114
8115 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8117
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008118 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008119
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008120 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008121 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008122
8123 ssl->handshake->psk_len = psk_len;
8124 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8125
8126 return( 0 );
8127}
8128
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008129#if defined(MBEDTLS_USE_PSA_CRYPTO)
8130int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008131 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008132 const unsigned char *psk_identity,
8133 size_t psk_identity_len )
8134{
Hanno Becker7390c712018-11-15 13:33:04 +00008135 int ret;
8136 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008137 ssl_conf_remove_psk( conf );
8138
Hanno Becker7390c712018-11-15 13:33:04 +00008139 /* Check and set opaque PSK */
8140 if( psk_slot == 0 )
8141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008142 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008143
8144 /* Check and set PSK Identity */
8145 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8146 psk_identity_len );
8147 if( ret != 0 )
8148 ssl_conf_remove_psk( conf );
8149
8150 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008151}
8152
8153int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008154 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008155{
8156 if( psk_slot == 0 || ssl->handshake == NULL )
8157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8158
8159 ssl_remove_psk( ssl );
8160 ssl->handshake->psk_opaque = psk_slot;
8161 return( 0 );
8162}
8163#endif /* MBEDTLS_USE_PSA_CRYPTO */
8164
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008165void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008167 size_t),
8168 void *p_psk )
8169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008170 conf->f_psk = f_psk;
8171 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008174
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008175#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008176
8177#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008178int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008179{
8180 int ret;
8181
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008182 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8183 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8184 {
8185 mbedtls_mpi_free( &conf->dhm_P );
8186 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008187 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008189
8190 return( 0 );
8191}
Hanno Becker470a8c42017-10-04 15:28:46 +01008192#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008193
Hanno Beckera90658f2017-10-04 15:29:08 +01008194int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8195 const unsigned char *dhm_P, size_t P_len,
8196 const unsigned char *dhm_G, size_t G_len )
8197{
8198 int ret;
8199
8200 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8201 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8202 {
8203 mbedtls_mpi_free( &conf->dhm_P );
8204 mbedtls_mpi_free( &conf->dhm_G );
8205 return( ret );
8206 }
8207
8208 return( 0 );
8209}
Paul Bakker5121ce52009-01-03 21:22:43 +00008210
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008211int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008212{
8213 int ret;
8214
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008215 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8216 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8217 {
8218 mbedtls_mpi_free( &conf->dhm_P );
8219 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008220 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008221 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008222
8223 return( 0 );
8224}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008225#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008226
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008227#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8228/*
8229 * Set the minimum length for Diffie-Hellman parameters
8230 */
8231void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8232 unsigned int bitlen )
8233{
8234 conf->dhm_min_bitlen = bitlen;
8235}
8236#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8237
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008238#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008239/*
8240 * Set allowed/preferred hashes for handshake signatures
8241 */
8242void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8243 const int *hashes )
8244{
8245 conf->sig_hashes = hashes;
8246}
Hanno Becker947194e2017-04-07 13:25:49 +01008247#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008248
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008249#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008250/*
8251 * Set the allowed elliptic curves
8252 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008253void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008254 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008256 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008257}
Hanno Becker947194e2017-04-07 13:25:49 +01008258#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008259
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008262{
Hanno Becker947194e2017-04-07 13:25:49 +01008263 /* Initialize to suppress unnecessary compiler warning */
8264 size_t hostname_len = 0;
8265
8266 /* Check if new hostname is valid before
8267 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008268 if( hostname != NULL )
8269 {
8270 hostname_len = strlen( hostname );
8271
8272 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8274 }
8275
8276 /* Now it's clear that we will overwrite the old hostname,
8277 * so we can free it safely */
8278
8279 if( ssl->hostname != NULL )
8280 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008281 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008282 mbedtls_free( ssl->hostname );
8283 }
8284
8285 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008286
Paul Bakker5121ce52009-01-03 21:22:43 +00008287 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008288 {
8289 ssl->hostname = NULL;
8290 }
8291 else
8292 {
8293 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008294 if( ssl->hostname == NULL )
8295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008296
Hanno Becker947194e2017-04-07 13:25:49 +01008297 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008298
Hanno Becker947194e2017-04-07 13:25:49 +01008299 ssl->hostname[hostname_len] = '\0';
8300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008301
8302 return( 0 );
8303}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008305
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008306#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008307void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008309 const unsigned char *, size_t),
8310 void *p_sni )
8311{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008312 conf->f_sni = f_sni;
8313 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008318int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008319{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008320 size_t cur_len, tot_len;
8321 const char **p;
8322
8323 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008324 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8325 * MUST NOT be truncated."
8326 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008327 */
8328 tot_len = 0;
8329 for( p = protos; *p != NULL; p++ )
8330 {
8331 cur_len = strlen( *p );
8332 tot_len += cur_len;
8333
8334 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008336 }
8337
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008338 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008339
8340 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008341}
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008344{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008345 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008348
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008349void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008351 conf->max_major_ver = major;
8352 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008353}
8354
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008355void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008356{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008357 conf->min_major_ver = major;
8358 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008359}
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008362void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008363{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008364 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008365}
8366#endif
8367
Janos Follath088ce432017-04-10 12:42:31 +01008368#if defined(MBEDTLS_SSL_SRV_C)
8369void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8370 char cert_req_ca_list )
8371{
8372 conf->cert_req_ca_list = cert_req_ca_list;
8373}
8374#endif
8375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008377void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008378{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008379 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008380}
8381#endif
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008384void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008386 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008387}
8388#endif
8389
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008390#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008391void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008393 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008394}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008395#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008401 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008404 }
8405
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008406 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008407
8408 return( 0 );
8409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008413void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008415 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008421{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008422 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008423}
8424#endif
8425
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008428 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008429}
8430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008435}
8436
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008437void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008438{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008439 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008440}
8441
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008442void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008443 const unsigned char period[8] )
8444{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008445 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008450#if defined(MBEDTLS_SSL_CLI_C)
8451void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008452{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008453 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008454}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008455#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008456
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008457#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008458void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8459 mbedtls_ssl_ticket_write_t *f_ticket_write,
8460 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8461 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008462{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008463 conf->f_ticket_write = f_ticket_write;
8464 conf->f_ticket_parse = f_ticket_parse;
8465 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008466}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008469
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008470#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8471void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8472 mbedtls_ssl_export_keys_t *f_export_keys,
8473 void *p_export_keys )
8474{
8475 conf->f_export_keys = f_export_keys;
8476 conf->p_export_keys = p_export_keys;
8477}
8478#endif
8479
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008480#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008481void mbedtls_ssl_conf_async_private_cb(
8482 mbedtls_ssl_config *conf,
8483 mbedtls_ssl_async_sign_t *f_async_sign,
8484 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8485 mbedtls_ssl_async_resume_t *f_async_resume,
8486 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008487 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008488{
8489 conf->f_async_sign_start = f_async_sign;
8490 conf->f_async_decrypt_start = f_async_decrypt;
8491 conf->f_async_resume = f_async_resume;
8492 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008493 conf->p_async_config_data = async_config_data;
8494}
8495
Gilles Peskine8f97af72018-04-26 11:46:10 +02008496void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8497{
8498 return( conf->p_async_config_data );
8499}
8500
Gilles Peskine1febfef2018-04-30 11:54:39 +02008501void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008502{
8503 if( ssl->handshake == NULL )
8504 return( NULL );
8505 else
8506 return( ssl->handshake->user_async_ctx );
8507}
8508
Gilles Peskine1febfef2018-04-30 11:54:39 +02008509void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008510 void *ctx )
8511{
8512 if( ssl->handshake != NULL )
8513 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008514}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008515#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008516
Paul Bakker5121ce52009-01-03 21:22:43 +00008517/*
8518 * SSL get accessors
8519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008521{
8522 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8523}
8524
Hanno Becker8b170a02017-10-10 11:51:19 +01008525int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8526{
8527 /*
8528 * Case A: We're currently holding back
8529 * a message for further processing.
8530 */
8531
8532 if( ssl->keep_current_message == 1 )
8533 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008535 return( 1 );
8536 }
8537
8538 /*
8539 * Case B: Further records are pending in the current datagram.
8540 */
8541
8542#if defined(MBEDTLS_SSL_PROTO_DTLS)
8543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8544 ssl->in_left > ssl->next_record_offset )
8545 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008547 return( 1 );
8548 }
8549#endif /* MBEDTLS_SSL_PROTO_DTLS */
8550
8551 /*
8552 * Case C: A handshake message is being processed.
8553 */
8554
Hanno Becker8b170a02017-10-10 11:51:19 +01008555 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8556 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008558 return( 1 );
8559 }
8560
8561 /*
8562 * Case D: An application data message is being processed
8563 */
8564 if( ssl->in_offt != NULL )
8565 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008567 return( 1 );
8568 }
8569
8570 /*
8571 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008572 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008573 * we implement support for multiple alerts in single records.
8574 */
8575
8576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8577 return( 0 );
8578}
8579
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008580uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008581{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008582 if( ssl->session != NULL )
8583 return( ssl->session->verify_result );
8584
8585 if( ssl->session_negotiate != NULL )
8586 return( ssl->session_negotiate->verify_result );
8587
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008588 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008589}
8590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008592{
Paul Bakker926c8e42013-03-06 10:23:34 +01008593 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008594 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008597}
8598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008602 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008603 {
8604 switch( ssl->minor_ver )
8605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008607 return( "DTLSv1.0" );
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008610 return( "DTLSv1.2" );
8611
8612 default:
8613 return( "unknown (DTLS)" );
8614 }
8615 }
8616#endif
8617
Paul Bakker43ca69c2011-01-15 17:35:19 +00008618 switch( ssl->minor_ver )
8619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008621 return( "SSLv3.0" );
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008624 return( "TLSv1.0" );
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008627 return( "TLSv1.1" );
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008630 return( "TLSv1.2" );
8631
Paul Bakker43ca69c2011-01-15 17:35:19 +00008632 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008633 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008634 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008635}
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008638{
Hanno Becker3136ede2018-08-17 15:28:19 +01008639 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008641 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008642
Hanno Becker78640902018-08-13 16:35:15 +01008643 if( transform == NULL )
8644 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#if defined(MBEDTLS_ZLIB_SUPPORT)
8647 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8648 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008649#endif
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653 case MBEDTLS_MODE_GCM:
8654 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008655 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008657 transform_expansion = transform->minlen;
8658 break;
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008661
8662 block_size = mbedtls_cipher_get_block_size(
8663 &transform->cipher_ctx_enc );
8664
Hanno Becker3136ede2018-08-17 15:28:19 +01008665 /* Expansion due to the addition of the MAC. */
8666 transform_expansion += transform->maclen;
8667
8668 /* Expansion due to the addition of CBC padding;
8669 * Theoretically up to 256 bytes, but we never use
8670 * more than the block size of the underlying cipher. */
8671 transform_expansion += block_size;
8672
8673 /* For TLS 1.1 or higher, an explicit IV is added
8674 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008675#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8676 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008677 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008678#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008679
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008680 break;
8681
8682 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008685 }
8686
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008687 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008688}
8689
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008690#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8691size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8692{
8693 size_t max_len;
8694
8695 /*
8696 * Assume mfl_code is correct since it was checked when set
8697 */
Angus Grattond8213d02016-05-25 20:56:48 +10008698 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008699
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008700 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008701 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008702 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008703 {
Angus Grattond8213d02016-05-25 20:56:48 +10008704 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008705 }
8706
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008707 /* During a handshake, use the value being negotiated */
8708 if( ssl->session_negotiate != NULL &&
8709 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8710 {
8711 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8712 }
8713
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008714 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008715}
8716#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8717
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008718#if defined(MBEDTLS_SSL_PROTO_DTLS)
8719static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8720{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008721 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8722 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8723 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8724 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8725 return ( 0 );
8726
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008727 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8728 return( ssl->mtu );
8729
8730 if( ssl->mtu == 0 )
8731 return( ssl->handshake->mtu );
8732
8733 return( ssl->mtu < ssl->handshake->mtu ?
8734 ssl->mtu : ssl->handshake->mtu );
8735}
8736#endif /* MBEDTLS_SSL_PROTO_DTLS */
8737
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008738int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8739{
8740 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8741
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008742#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8743 !defined(MBEDTLS_SSL_PROTO_DTLS)
8744 (void) ssl;
8745#endif
8746
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008747#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8748 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8749
8750 if( max_len > mfl )
8751 max_len = mfl;
8752#endif
8753
8754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008755 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008756 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008757 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008758 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8759 const size_t overhead = (size_t) ret;
8760
8761 if( ret < 0 )
8762 return( ret );
8763
8764 if( mtu <= overhead )
8765 {
8766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8767 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8768 }
8769
8770 if( max_len > mtu - overhead )
8771 max_len = mtu - overhead;
8772 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008773#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008774
Hanno Becker0defedb2018-08-10 12:35:02 +01008775#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8776 !defined(MBEDTLS_SSL_PROTO_DTLS)
8777 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008778#endif
8779
8780 return( (int) max_len );
8781}
8782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#if defined(MBEDTLS_X509_CRT_PARSE_C)
8784const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008785{
8786 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008787 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008788
Hanno Beckere6824572019-02-07 13:18:46 +00008789#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008790 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008791#else
8792 return( NULL );
8793#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008798int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8799 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008800{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008801 if( ssl == NULL ||
8802 dst == NULL ||
8803 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008804 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008807 }
8808
Hanno Becker52055ae2019-02-06 14:30:46 +00008809 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008812
Paul Bakker5121ce52009-01-03 21:22:43 +00008813/*
Paul Bakker1961b702013-01-25 14:49:24 +01008814 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008819
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008820 if( ssl == NULL || ssl->conf == NULL )
8821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008824 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008830#endif
8831
Paul Bakker1961b702013-01-25 14:49:24 +01008832 return( ret );
8833}
8834
8835/*
8836 * Perform the SSL handshake
8837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008839{
8840 int ret = 0;
8841
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008842 if( ssl == NULL || ssl->conf == NULL )
8843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008850
8851 if( ret != 0 )
8852 break;
8853 }
8854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008856
8857 return( ret );
8858}
8859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860#if defined(MBEDTLS_SSL_RENEGOTIATION)
8861#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008862/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008863 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008866{
8867 int ret;
8868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008870
8871 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8873 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008874
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008875 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008876 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008878 return( ret );
8879 }
8880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008882
8883 return( 0 );
8884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008886
8887/*
8888 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889 * - any side: calling mbedtls_ssl_renegotiate(),
8890 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8891 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008892 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008893 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008897{
8898 int ret;
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
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008902 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8903 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008904
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008905 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8906 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008910 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008912 ssl->handshake->out_msg_seq = 1;
8913 else
8914 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008915 }
8916#endif
8917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8919 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008924 return( ret );
8925 }
8926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008928
8929 return( 0 );
8930}
8931
8932/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008933 * Renegotiate current connection on client,
8934 * or request renegotiation on server
8935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008939
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008940 if( ssl == NULL || ssl->conf == NULL )
8941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008944 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008951
8952 /* Did we already try/start sending HelloRequest? */
8953 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008955
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008956 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008961 /*
8962 * On client, either start the renegotiation process or,
8963 * if already in progress, continue the handshake
8964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008969
8970 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008973 return( ret );
8974 }
8975 }
8976 else
8977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008981 return( ret );
8982 }
8983 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008985
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008986 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008987}
8988
8989/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008990 * Check record counters and renegotiate if they're above the limit.
8991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008993{
Andres AG2196c7f2016-12-15 17:01:16 +00008994 size_t ep_len = ssl_ep_len( ssl );
8995 int in_ctr_cmp;
8996 int out_ctr_cmp;
8997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8999 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009000 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009001 {
9002 return( 0 );
9003 }
9004
Andres AG2196c7f2016-12-15 17:01:16 +00009005 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9006 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009007 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009008 ssl->conf->renego_period + ep_len, 8 - ep_len );
9009
9010 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009011 {
9012 return( 0 );
9013 }
9014
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009019
9020/*
9021 * Receive application data decrypted from the SSL layer
9022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009024{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009025 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009026 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009027
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009028 if( ssl == NULL || ssl->conf == NULL )
9029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009034 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009037 return( ret );
9038
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009039 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009041 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009042 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009043 return( ret );
9044 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009045 }
9046#endif
9047
Hanno Becker4a810fb2017-05-24 16:27:30 +01009048 /*
9049 * Check if renegotiation is necessary and/or handshake is
9050 * in process. If yes, perform/continue, and fall through
9051 * if an unexpected packet is received while the client
9052 * is waiting for the ServerHello.
9053 *
9054 * (There is no equivalent to the last condition on
9055 * the server-side as it is not treated as within
9056 * a handshake while waiting for the ClientHello
9057 * after a renegotiation request.)
9058 */
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009061 ret = ssl_check_ctr_renegotiate( ssl );
9062 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9063 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009066 return( ret );
9067 }
9068#endif
9069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009073 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9074 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009077 return( ret );
9078 }
9079 }
9080
Hanno Beckere41158b2017-10-23 13:30:32 +01009081 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009082 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009083 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009084 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009085 if( ssl->f_get_timer != NULL &&
9086 ssl->f_get_timer( ssl->p_timer ) == -1 )
9087 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009088 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009089 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009090
Hanno Becker327c93b2018-08-15 13:56:18 +01009091 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009092 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009093 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9094 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009095
Hanno Becker4a810fb2017-05-24 16:27:30 +01009096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9097 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009098 }
9099
9100 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009102 {
9103 /*
9104 * OpenSSL sends empty messages to randomize the IV
9105 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009106 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009109 return( 0 );
9110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009112 return( ret );
9113 }
9114 }
9115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009119
Hanno Becker4a810fb2017-05-24 16:27:30 +01009120 /*
9121 * - For client-side, expect SERVER_HELLO_REQUEST.
9122 * - For server-side, expect CLIENT_HELLO.
9123 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9124 */
9125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009129 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009132
9133 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009136 {
9137 continue;
9138 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009141 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009142#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009143
Hanno Becker4a810fb2017-05-24 16:27:30 +01009144#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009149
9150 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009153 {
9154 continue;
9155 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009158 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009159#endif /* MBEDTLS_SSL_SRV_C */
9160
Hanno Becker21df7f92017-10-17 11:03:26 +01009161#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009162 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009163 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9164 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9165 ssl->conf->allow_legacy_renegotiation ==
9166 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9167 {
9168 /*
9169 * Accept renegotiation request
9170 */
Paul Bakker48916f92012-09-16 19:57:18 +00009171
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009172 /* DTLS clients need to know renego is server-initiated */
9173#if defined(MBEDTLS_SSL_PROTO_DTLS)
9174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9175 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9176 {
9177 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9178 }
9179#endif
9180 ret = ssl_start_renegotiation( ssl );
9181 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9182 ret != 0 )
9183 {
9184 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9185 return( ret );
9186 }
9187 }
9188 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009189#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009190 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009191 /*
9192 * Refuse renegotiation
9193 */
9194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197#if defined(MBEDTLS_SSL_PROTO_SSL3)
9198 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009199 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009200 /* SSLv3 does not have a "no_renegotiation" warning, so
9201 we send a fatal alert and abort the connection. */
9202 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9203 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9204 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009205 }
9206 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9209 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9210 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9213 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9214 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009215 {
9216 return( ret );
9217 }
Paul Bakker48916f92012-09-16 19:57:18 +00009218 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009219 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9221 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009225 }
Paul Bakker48916f92012-09-16 19:57:18 +00009226 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009227
Hanno Becker90333da2017-10-10 11:27:13 +01009228 /* At this point, we don't know whether the renegotiation has been
9229 * completed or not. The cases to consider are the following:
9230 * 1) The renegotiation is complete. In this case, no new record
9231 * has been read yet.
9232 * 2) The renegotiation is incomplete because the client received
9233 * an application data record while awaiting the ServerHello.
9234 * 3) The renegotiation is incomplete because the client received
9235 * a non-handshake, non-application data message while awaiting
9236 * the ServerHello.
9237 * In each of these case, looping will be the proper action:
9238 * - For 1), the next iteration will read a new record and check
9239 * if it's application data.
9240 * - For 2), the loop condition isn't satisfied as application data
9241 * is present, hence continue is the same as break
9242 * - For 3), the loop condition is satisfied and read_record
9243 * will re-deliver the message that was held back by the client
9244 * when expecting the ServerHello.
9245 */
9246 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009247 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009248#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009250 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009251 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009252 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009253 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009256 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009258 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009259 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9264 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009267 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009268 }
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9273 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009274 }
9275
9276 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009277
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009278 /* We're going to return something now, cancel timer,
9279 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009281 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009282
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009284 /* If we requested renego but received AppData, resend HelloRequest.
9285 * Do it now, after setting in_offt, to avoid taking this branch
9286 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009290 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009291 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009294 return( ret );
9295 }
9296 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009298#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009299 }
9300
9301 n = ( len < ssl->in_msglen )
9302 ? len : ssl->in_msglen;
9303
9304 memcpy( buf, ssl->in_offt, n );
9305 ssl->in_msglen -= n;
9306
9307 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009308 {
9309 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009310 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009311 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009313 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009314 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009315 /* more data available */
9316 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009320
Paul Bakker23986e52011-04-24 08:57:21 +00009321 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009322}
9323
9324/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009325 * Send application data to be encrypted by the SSL layer, taking care of max
9326 * fragment length and buffer size.
9327 *
9328 * According to RFC 5246 Section 6.2.1:
9329 *
9330 * Zero-length fragments of Application data MAY be sent as they are
9331 * potentially useful as a traffic analysis countermeasure.
9332 *
9333 * Therefore, it is possible that the input message length is 0 and the
9334 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009335 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009336static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009337 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009338{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009339 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9340 const size_t max_len = (size_t) ret;
9341
9342 if( ret < 0 )
9343 {
9344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9345 return( ret );
9346 }
9347
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009348 if( len > max_len )
9349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009354 "maximum fragment length: %d > %d",
9355 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009357 }
9358 else
9359#endif
9360 len = max_len;
9361 }
Paul Bakker887bd502011-06-08 13:10:54 +00009362
Paul Bakker5121ce52009-01-03 21:22:43 +00009363 if( ssl->out_left != 0 )
9364 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009365 /*
9366 * The user has previously tried to send the data and
9367 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9368 * written. In this case, we expect the high-level write function
9369 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009374 return( ret );
9375 }
9376 }
Paul Bakker887bd502011-06-08 13:10:54 +00009377 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009378 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009379 /*
9380 * The user is trying to send a message the first time, so we need to
9381 * copy the data into the internal buffers and setup the data structure
9382 * to keep track of partial writes
9383 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009384 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009386 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009387
Hanno Becker67bc7c32018-08-06 11:33:50 +01009388 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009391 return( ret );
9392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009393 }
9394
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009395 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009396}
9397
9398/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009399 * Write application data, doing 1/n-1 splitting if necessary.
9400 *
9401 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009402 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009403 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009406static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009407 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009408{
9409 int ret;
9410
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009411 if( ssl->conf->cbc_record_splitting ==
9412 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009413 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9415 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9416 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009417 {
9418 return( ssl_write_real( ssl, buf, len ) );
9419 }
9420
9421 if( ssl->split_done == 0 )
9422 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009423 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009424 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009425 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009426 }
9427
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009428 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9429 return( ret );
9430 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009431
9432 return( ret + 1 );
9433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009435
9436/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009437 * Write application data (public-facing wrapper)
9438 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009439int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009440{
9441 int ret;
9442
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009444
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009445 if( ssl == NULL || ssl->conf == NULL )
9446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9447
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009448#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009449 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9450 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009451 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009452 return( ret );
9453 }
9454#endif
9455
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009456 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009457 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009458 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009459 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009461 return( ret );
9462 }
9463 }
9464
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009465#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009466 ret = ssl_write_split( ssl, buf, len );
9467#else
9468 ret = ssl_write_real( ssl, buf, len );
9469#endif
9470
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009472
9473 return( ret );
9474}
9475
9476/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009477 * Notify the peer that the connection is being closed
9478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009480{
9481 int ret;
9482
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009483 if( ssl == NULL || ssl->conf == NULL )
9484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009487
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009488 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9494 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9495 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009498 return( ret );
9499 }
9500 }
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009503
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009504 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009505}
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009508{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009509 if( transform == NULL )
9510 return;
9511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009513 deflateEnd( &transform->ctx_deflate );
9514 inflateEnd( &transform->ctx_inflate );
9515#endif
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9518 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 mbedtls_md_free( &transform->md_ctx_enc );
9521 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009522
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009523 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009524}
9525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#if defined(MBEDTLS_X509_CRT_PARSE_C)
9527static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009530
9531 while( cur != NULL )
9532 {
9533 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009535 cur = next;
9536 }
9537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009539
Hanno Becker0271f962018-08-16 13:23:47 +01009540#if defined(MBEDTLS_SSL_PROTO_DTLS)
9541
9542static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9543{
9544 unsigned offset;
9545 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9546
9547 if( hs == NULL )
9548 return;
9549
Hanno Becker283f5ef2018-08-24 09:34:47 +01009550 ssl_free_buffered_record( ssl );
9551
Hanno Becker0271f962018-08-16 13:23:47 +01009552 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009553 ssl_buffering_free_slot( ssl, offset );
9554}
9555
9556static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9557 uint8_t slot )
9558{
9559 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9560 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009561
9562 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9563 return;
9564
Hanno Beckere605b192018-08-21 15:59:07 +01009565 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009566 {
Hanno Beckere605b192018-08-21 15:59:07 +01009567 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009568 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009569 mbedtls_free( hs_buf->data );
9570 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009571 }
9572}
9573
9574#endif /* MBEDTLS_SSL_PROTO_DTLS */
9575
Gilles Peskine9b562d52018-04-25 20:32:43 +02009576void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009577{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009578 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9579
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009580 if( handshake == NULL )
9581 return;
9582
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009583#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9584 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9585 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009586 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009587 handshake->async_in_progress = 0;
9588 }
9589#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9590
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009591#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9592 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9593 mbedtls_md5_free( &handshake->fin_md5 );
9594 mbedtls_sha1_free( &handshake->fin_sha1 );
9595#endif
9596#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9597#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009598#if defined(MBEDTLS_USE_PSA_CRYPTO)
9599 psa_hash_abort( &handshake->fin_sha256_psa );
9600#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009601 mbedtls_sha256_free( &handshake->fin_sha256 );
9602#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009603#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009604#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009605#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009606 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009607#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009608 mbedtls_sha512_free( &handshake->fin_sha512 );
9609#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009610#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613#if defined(MBEDTLS_DHM_C)
9614 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_ECDH_C)
9617 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009618#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009619#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009620 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009621#if defined(MBEDTLS_SSL_CLI_C)
9622 mbedtls_free( handshake->ecjpake_cache );
9623 handshake->ecjpake_cache = NULL;
9624 handshake->ecjpake_cache_len = 0;
9625#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009626#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009627
Janos Follath4ae5c292016-02-10 11:27:43 +00009628#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9629 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009630 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009632#endif
9633
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009634#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9635 if( handshake->psk != NULL )
9636 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009637 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009638 mbedtls_free( handshake->psk );
9639 }
9640#endif
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9643 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009644 /*
9645 * Free only the linked list wrapper, not the keys themselves
9646 * since the belong to the SNI callback
9647 */
9648 if( handshake->sni_key_cert != NULL )
9649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009651
9652 while( cur != NULL )
9653 {
9654 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009656 cur = next;
9657 }
9658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009660
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009661#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009662 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009663 if( handshake->ecrs_peer_cert != NULL )
9664 {
9665 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9666 mbedtls_free( handshake->ecrs_peer_cert );
9667 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009668#endif
9669
Hanno Becker75173122019-02-06 16:18:31 +00009670#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9671 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9672 mbedtls_pk_free( &handshake->peer_pubkey );
9673#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675#if defined(MBEDTLS_SSL_PROTO_DTLS)
9676 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009677 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009678 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009679#endif
9680
Hanno Becker4a63ed42019-01-08 11:39:35 +00009681#if defined(MBEDTLS_ECDH_C) && \
9682 defined(MBEDTLS_USE_PSA_CRYPTO)
9683 psa_destroy_key( handshake->ecdh_psa_privkey );
9684#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9685
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009686 mbedtls_platform_zeroize( handshake,
9687 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009688}
9689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009691{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009692 if( session == NULL )
9693 return;
9694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009696 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009697#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009698
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009699#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009701#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009702
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009703 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009704}
9705
Paul Bakker5121ce52009-01-03 21:22:43 +00009706/*
9707 * Free an SSL context
9708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009710{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009711 if( ssl == NULL )
9712 return;
9713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009715
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009716 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 {
Angus Grattond8213d02016-05-25 20:56:48 +10009718 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009720 }
9721
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009722 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009723 {
Angus Grattond8213d02016-05-25 20:56:48 +10009724 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009726 }
9727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009729 if( ssl->compress_buf != NULL )
9730 {
Angus Grattond8213d02016-05-25 20:56:48 +10009731 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009733 }
9734#endif
9735
Paul Bakker48916f92012-09-16 19:57:18 +00009736 if( ssl->transform )
9737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 mbedtls_ssl_transform_free( ssl->transform );
9739 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009740 }
9741
9742 if( ssl->handshake )
9743 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009744 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9746 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 mbedtls_free( ssl->handshake );
9749 mbedtls_free( ssl->transform_negotiate );
9750 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009751 }
9752
Paul Bakkerc0463502013-02-14 11:19:38 +01009753 if( ssl->session )
9754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755 mbedtls_ssl_session_free( ssl->session );
9756 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009757 }
9758
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009759#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009760 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009761 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009762 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009764 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009765#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9768 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9771 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009772 }
9773#endif
9774
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009775#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009777#endif
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009780
Paul Bakker86f04f42013-02-14 11:20:09 +01009781 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009782 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009783}
9784
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009785/*
9786 * Initialze mbedtls_ssl_config
9787 */
9788void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9789{
9790 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9791}
9792
Simon Butcherc97b6972015-12-27 23:48:17 +00009793#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009794static int ssl_preset_default_hashes[] = {
9795#if defined(MBEDTLS_SHA512_C)
9796 MBEDTLS_MD_SHA512,
9797 MBEDTLS_MD_SHA384,
9798#endif
9799#if defined(MBEDTLS_SHA256_C)
9800 MBEDTLS_MD_SHA256,
9801 MBEDTLS_MD_SHA224,
9802#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009803#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009804 MBEDTLS_MD_SHA1,
9805#endif
9806 MBEDTLS_MD_NONE
9807};
Simon Butcherc97b6972015-12-27 23:48:17 +00009808#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009809
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009810static int ssl_preset_suiteb_ciphersuites[] = {
9811 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9812 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9813 0
9814};
9815
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009816#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009817static int ssl_preset_suiteb_hashes[] = {
9818 MBEDTLS_MD_SHA256,
9819 MBEDTLS_MD_SHA384,
9820 MBEDTLS_MD_NONE
9821};
9822#endif
9823
9824#if defined(MBEDTLS_ECP_C)
9825static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9826 MBEDTLS_ECP_DP_SECP256R1,
9827 MBEDTLS_ECP_DP_SECP384R1,
9828 MBEDTLS_ECP_DP_NONE
9829};
9830#endif
9831
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009832/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009833 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009834 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009835int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009836 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009837{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009838#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009839 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009840#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009841
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009842 /* Use the functions here so that they are covered in tests,
9843 * but otherwise access member directly for efficiency */
9844 mbedtls_ssl_conf_endpoint( conf, endpoint );
9845 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009846
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009847 /*
9848 * Things that are common to all presets
9849 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009850#if defined(MBEDTLS_SSL_CLI_C)
9851 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9852 {
9853 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9854#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9855 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9856#endif
9857 }
9858#endif
9859
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009860#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009861 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009862#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009863
9864#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9865 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9866#endif
9867
9868#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9869 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9870#endif
9871
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009872#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9873 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9874#endif
9875
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009876#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009877 conf->f_cookie_write = ssl_cookie_write_dummy;
9878 conf->f_cookie_check = ssl_cookie_check_dummy;
9879#endif
9880
9881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9882 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9883#endif
9884
Janos Follath088ce432017-04-10 12:42:31 +01009885#if defined(MBEDTLS_SSL_SRV_C)
9886 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9887#endif
9888
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009889#if defined(MBEDTLS_SSL_PROTO_DTLS)
9890 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9891 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9892#endif
9893
9894#if defined(MBEDTLS_SSL_RENEGOTIATION)
9895 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009896 memset( conf->renego_period, 0x00, 2 );
9897 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009898#endif
9899
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009900#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9901 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9902 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009903 const unsigned char dhm_p[] =
9904 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9905 const unsigned char dhm_g[] =
9906 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9907
Hanno Beckera90658f2017-10-04 15:29:08 +01009908 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9909 dhm_p, sizeof( dhm_p ),
9910 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009911 {
9912 return( ret );
9913 }
9914 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009915#endif
9916
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009917 /*
9918 * Preset-specific defaults
9919 */
9920 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009921 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009922 /*
9923 * NSA Suite B
9924 */
9925 case MBEDTLS_SSL_PRESET_SUITEB:
9926 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9927 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9928 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9929 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9930
9931 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9932 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9933 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9934 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9935 ssl_preset_suiteb_ciphersuites;
9936
9937#if defined(MBEDTLS_X509_CRT_PARSE_C)
9938 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009939#endif
9940
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009941#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009942 conf->sig_hashes = ssl_preset_suiteb_hashes;
9943#endif
9944
9945#if defined(MBEDTLS_ECP_C)
9946 conf->curve_list = ssl_preset_suiteb_curves;
9947#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009948 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009949
9950 /*
9951 * Default
9952 */
9953 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009954 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9955 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9956 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9957 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9958 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9959 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9960 MBEDTLS_SSL_MIN_MINOR_VERSION :
9961 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009962 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9963 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9964
9965#if defined(MBEDTLS_SSL_PROTO_DTLS)
9966 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9967 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9968#endif
9969
9970 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9971 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9972 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9973 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9974 mbedtls_ssl_list_ciphersuites();
9975
9976#if defined(MBEDTLS_X509_CRT_PARSE_C)
9977 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9978#endif
9979
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009980#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009981 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009982#endif
9983
9984#if defined(MBEDTLS_ECP_C)
9985 conf->curve_list = mbedtls_ecp_grp_id_list();
9986#endif
9987
9988#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9989 conf->dhm_min_bitlen = 1024;
9990#endif
9991 }
9992
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009993 return( 0 );
9994}
9995
9996/*
9997 * Free mbedtls_ssl_config
9998 */
9999void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10000{
10001#if defined(MBEDTLS_DHM_C)
10002 mbedtls_mpi_free( &conf->dhm_P );
10003 mbedtls_mpi_free( &conf->dhm_G );
10004#endif
10005
10006#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10007 if( conf->psk != NULL )
10008 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010009 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010010 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010011 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010012 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010013 }
10014
10015 if( conf->psk_identity != NULL )
10016 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010017 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010018 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010019 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010020 conf->psk_identity_len = 0;
10021 }
10022#endif
10023
10024#if defined(MBEDTLS_X509_CRT_PARSE_C)
10025 ssl_key_cert_free( conf->key_cert );
10026#endif
10027
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010028 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010029}
10030
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010031#if defined(MBEDTLS_PK_C) && \
10032 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010033/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#if defined(MBEDTLS_RSA_C)
10039 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10040 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042#if defined(MBEDTLS_ECDSA_C)
10043 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10044 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010047}
10048
Hanno Becker7e5437a2017-04-28 17:15:26 +010010049unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10050{
10051 switch( type ) {
10052 case MBEDTLS_PK_RSA:
10053 return( MBEDTLS_SSL_SIG_RSA );
10054 case MBEDTLS_PK_ECDSA:
10055 case MBEDTLS_PK_ECKEY:
10056 return( MBEDTLS_SSL_SIG_ECDSA );
10057 default:
10058 return( MBEDTLS_SSL_SIG_ANON );
10059 }
10060}
10061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010063{
10064 switch( sig )
10065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066#if defined(MBEDTLS_RSA_C)
10067 case MBEDTLS_SSL_SIG_RSA:
10068 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070#if defined(MBEDTLS_ECDSA_C)
10071 case MBEDTLS_SSL_SIG_ECDSA:
10072 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010073#endif
10074 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010076 }
10077}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010078#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010079
Hanno Becker7e5437a2017-04-28 17:15:26 +010010080#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10081 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10082
10083/* Find an entry in a signature-hash set matching a given hash algorithm. */
10084mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10085 mbedtls_pk_type_t sig_alg )
10086{
10087 switch( sig_alg )
10088 {
10089 case MBEDTLS_PK_RSA:
10090 return( set->rsa );
10091 case MBEDTLS_PK_ECDSA:
10092 return( set->ecdsa );
10093 default:
10094 return( MBEDTLS_MD_NONE );
10095 }
10096}
10097
10098/* Add a signature-hash-pair to a signature-hash set */
10099void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10100 mbedtls_pk_type_t sig_alg,
10101 mbedtls_md_type_t md_alg )
10102{
10103 switch( sig_alg )
10104 {
10105 case MBEDTLS_PK_RSA:
10106 if( set->rsa == MBEDTLS_MD_NONE )
10107 set->rsa = md_alg;
10108 break;
10109
10110 case MBEDTLS_PK_ECDSA:
10111 if( set->ecdsa == MBEDTLS_MD_NONE )
10112 set->ecdsa = md_alg;
10113 break;
10114
10115 default:
10116 break;
10117 }
10118}
10119
10120/* Allow exactly one hash algorithm for each signature. */
10121void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10122 mbedtls_md_type_t md_alg )
10123{
10124 set->rsa = md_alg;
10125 set->ecdsa = md_alg;
10126}
10127
10128#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10129 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10130
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010131/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010132 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010135{
10136 switch( hash )
10137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138#if defined(MBEDTLS_MD5_C)
10139 case MBEDTLS_SSL_HASH_MD5:
10140 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142#if defined(MBEDTLS_SHA1_C)
10143 case MBEDTLS_SSL_HASH_SHA1:
10144 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146#if defined(MBEDTLS_SHA256_C)
10147 case MBEDTLS_SSL_HASH_SHA224:
10148 return( MBEDTLS_MD_SHA224 );
10149 case MBEDTLS_SSL_HASH_SHA256:
10150 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152#if defined(MBEDTLS_SHA512_C)
10153 case MBEDTLS_SSL_HASH_SHA384:
10154 return( MBEDTLS_MD_SHA384 );
10155 case MBEDTLS_SSL_HASH_SHA512:
10156 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010157#endif
10158 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010160 }
10161}
10162
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010163/*
10164 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10165 */
10166unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10167{
10168 switch( md )
10169 {
10170#if defined(MBEDTLS_MD5_C)
10171 case MBEDTLS_MD_MD5:
10172 return( MBEDTLS_SSL_HASH_MD5 );
10173#endif
10174#if defined(MBEDTLS_SHA1_C)
10175 case MBEDTLS_MD_SHA1:
10176 return( MBEDTLS_SSL_HASH_SHA1 );
10177#endif
10178#if defined(MBEDTLS_SHA256_C)
10179 case MBEDTLS_MD_SHA224:
10180 return( MBEDTLS_SSL_HASH_SHA224 );
10181 case MBEDTLS_MD_SHA256:
10182 return( MBEDTLS_SSL_HASH_SHA256 );
10183#endif
10184#if defined(MBEDTLS_SHA512_C)
10185 case MBEDTLS_MD_SHA384:
10186 return( MBEDTLS_SSL_HASH_SHA384 );
10187 case MBEDTLS_MD_SHA512:
10188 return( MBEDTLS_SSL_HASH_SHA512 );
10189#endif
10190 default:
10191 return( MBEDTLS_SSL_HASH_NONE );
10192 }
10193}
10194
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010195#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010196/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010197 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010198 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010199 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010200int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010203
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010204 if( ssl->conf->curve_list == NULL )
10205 return( -1 );
10206
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010207 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010208 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010209 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010210
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010211 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010212}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010213#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010214
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010215#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010216/*
10217 * Check if a hash proposed by the peer is in our list.
10218 * Return 0 if we're willing to use it, -1 otherwise.
10219 */
10220int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10221 mbedtls_md_type_t md )
10222{
10223 const int *cur;
10224
10225 if( ssl->conf->sig_hashes == NULL )
10226 return( -1 );
10227
10228 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10229 if( *cur == (int) md )
10230 return( 0 );
10231
10232 return( -1 );
10233}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010234#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_X509_CRT_PARSE_C)
10237int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10238 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010239 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010240 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010241{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010242 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010244 int usage = 0;
10245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010247 const char *ext_oid;
10248 size_t ext_len;
10249#endif
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10252 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010253 ((void) cert);
10254 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010255 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010256#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10259 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010260 {
10261 /* Server part of the key exchange */
10262 switch( ciphersuite->key_exchange )
10263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 case MBEDTLS_KEY_EXCHANGE_RSA:
10265 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010266 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010267 break;
10268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10270 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10271 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10272 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010273 break;
10274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10276 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010277 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010278 break;
10279
10280 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 case MBEDTLS_KEY_EXCHANGE_NONE:
10282 case MBEDTLS_KEY_EXCHANGE_PSK:
10283 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10284 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010285 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010286 usage = 0;
10287 }
10288 }
10289 else
10290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10292 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010293 }
10294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010296 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010297 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010298 ret = -1;
10299 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010300#else
10301 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10305 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10308 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010309 }
10310 else
10311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10313 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010314 }
10315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010317 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010318 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010319 ret = -1;
10320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010322
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010323 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010326
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010327/*
10328 * Convert version numbers to/from wire format
10329 * and, for DTLS, to/from TLS equivalent.
10330 *
10331 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010332 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010333 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10334 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010337 unsigned char ver[2] )
10338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_SSL_PROTO_DTLS)
10340 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010343 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10344
10345 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10346 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10347 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010348 else
10349#else
10350 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010351#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010352 {
10353 ver[0] = (unsigned char) major;
10354 ver[1] = (unsigned char) minor;
10355 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010356}
10357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010359 const unsigned char ver[2] )
10360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361#if defined(MBEDTLS_SSL_PROTO_DTLS)
10362 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010363 {
10364 *major = 255 - ver[0] + 2;
10365 *minor = 255 - ver[1] + 1;
10366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010368 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10369 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010370 else
10371#else
10372 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010373#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010374 {
10375 *major = ver[0];
10376 *minor = ver[1];
10377 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010378}
10379
Simon Butcher99000142016-10-13 17:21:01 +010010380int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10381{
10382#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10383 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10384 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10385
10386 switch( md )
10387 {
10388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10389#if defined(MBEDTLS_MD5_C)
10390 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010391 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010392#endif
10393#if defined(MBEDTLS_SHA1_C)
10394 case MBEDTLS_SSL_HASH_SHA1:
10395 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10396 break;
10397#endif
10398#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10399#if defined(MBEDTLS_SHA512_C)
10400 case MBEDTLS_SSL_HASH_SHA384:
10401 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10402 break;
10403#endif
10404#if defined(MBEDTLS_SHA256_C)
10405 case MBEDTLS_SSL_HASH_SHA256:
10406 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10407 break;
10408#endif
10409 default:
10410 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10411 }
10412
10413 return 0;
10414#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10415 (void) ssl;
10416 (void) md;
10417
10418 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10420}
10421
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010422#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10423 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10424int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10425 unsigned char *output,
10426 unsigned char *data, size_t data_len )
10427{
10428 int ret = 0;
10429 mbedtls_md5_context mbedtls_md5;
10430 mbedtls_sha1_context mbedtls_sha1;
10431
10432 mbedtls_md5_init( &mbedtls_md5 );
10433 mbedtls_sha1_init( &mbedtls_sha1 );
10434
10435 /*
10436 * digitally-signed struct {
10437 * opaque md5_hash[16];
10438 * opaque sha_hash[20];
10439 * };
10440 *
10441 * md5_hash
10442 * MD5(ClientHello.random + ServerHello.random
10443 * + ServerParams);
10444 * sha_hash
10445 * SHA(ClientHello.random + ServerHello.random
10446 * + ServerParams);
10447 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010448 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010449 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010451 goto exit;
10452 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010453 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010454 ssl->handshake->randbytes, 64 ) ) != 0 )
10455 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010457 goto exit;
10458 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010459 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010460 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010462 goto exit;
10463 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010464 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010465 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010467 goto exit;
10468 }
10469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010470 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010471 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010473 goto exit;
10474 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010475 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010476 ssl->handshake->randbytes, 64 ) ) != 0 )
10477 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010479 goto exit;
10480 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010481 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010482 data_len ) ) != 0 )
10483 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010485 goto exit;
10486 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010487 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010488 output + 16 ) ) != 0 )
10489 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010491 goto exit;
10492 }
10493
10494exit:
10495 mbedtls_md5_free( &mbedtls_md5 );
10496 mbedtls_sha1_free( &mbedtls_sha1 );
10497
10498 if( ret != 0 )
10499 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10500 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10501
10502 return( ret );
10503
10504}
10505#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10506 MBEDTLS_SSL_PROTO_TLS1_1 */
10507
10508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010510
10511#if defined(MBEDTLS_USE_PSA_CRYPTO)
10512int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10513 unsigned char *hash, size_t *hashlen,
10514 unsigned char *data, size_t data_len,
10515 mbedtls_md_type_t md_alg )
10516{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010517 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010518 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010519 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10520
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010522
10523 if( ( status = psa_hash_setup( &hash_operation,
10524 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010525 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010526 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010527 goto exit;
10528 }
10529
Andrzej Kurek814feff2019-01-14 04:35:19 -050010530 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10531 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010532 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010533 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010534 goto exit;
10535 }
10536
Andrzej Kurek814feff2019-01-14 04:35:19 -050010537 if( ( status = psa_hash_update( &hash_operation,
10538 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010539 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010540 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010541 goto exit;
10542 }
10543
Andrzej Kurek814feff2019-01-14 04:35:19 -050010544 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10545 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010546 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010547 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010548 goto exit;
10549 }
10550
10551exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010552 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010553 {
10554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10555 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010556 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010557 {
10558 case PSA_ERROR_NOT_SUPPORTED:
10559 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010560 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010561 case PSA_ERROR_BUFFER_TOO_SMALL:
10562 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10563 case PSA_ERROR_INSUFFICIENT_MEMORY:
10564 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10565 default:
10566 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10567 }
10568 }
10569 return( 0 );
10570}
10571
10572#else
10573
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010574int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010575 unsigned char *hash, size_t *hashlen,
10576 unsigned char *data, size_t data_len,
10577 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010578{
10579 int ret = 0;
10580 mbedtls_md_context_t ctx;
10581 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010582 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010583
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010585
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010586 mbedtls_md_init( &ctx );
10587
10588 /*
10589 * digitally-signed struct {
10590 * opaque client_random[32];
10591 * opaque server_random[32];
10592 * ServerDHParams params;
10593 * };
10594 */
10595 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10596 {
10597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10598 goto exit;
10599 }
10600 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10601 {
10602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10603 goto exit;
10604 }
10605 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10606 {
10607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10608 goto exit;
10609 }
10610 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10611 {
10612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10613 goto exit;
10614 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010615 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010616 {
10617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10618 goto exit;
10619 }
10620
10621exit:
10622 mbedtls_md_free( &ctx );
10623
10624 if( ret != 0 )
10625 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10626 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10627
10628 return( ret );
10629}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010630#endif /* MBEDTLS_USE_PSA_CRYPTO */
10631
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010632#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10633 MBEDTLS_SSL_PROTO_TLS1_2 */
10634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#endif /* MBEDTLS_SSL_TLS_C */