blob: 4ca8f326f4ea42a0bee84d9b8be0aaa70eb7dd92 [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)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 if( src->peer_cert != NULL )
290 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200291 int ret;
292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200293 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200294 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200300 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 dst->peer_cert = NULL;
304 return( ret );
305 }
306 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000307
308#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
309 if( src->peer_cert_digest != NULL )
310 {
311 dst->peer_cert_digest_len = src->peer_cert_digest_len;
312 dst->peer_cert_digest =
313 mbedtls_calloc( 1, dst->peer_cert_digest_len );
314 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;
320 }
321#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200325#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326 if( src->ticket != NULL )
327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200329 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200331
332 memcpy( dst->ticket, src->ticket, src->ticket_len );
333 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200334#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 return( 0 );
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
340int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200341 const unsigned char *key_enc, const unsigned char *key_dec,
342 size_t keylen,
343 const unsigned char *iv_enc, const unsigned char *iv_dec,
344 size_t ivlen,
345 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
348int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
349int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
352#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354/*
355 * Key material generation
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200358static int ssl3_prf( const unsigned char *secret, size_t slen,
359 const char *label,
360 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000361 unsigned char *dstbuf, size_t dlen )
362{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100363 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000364 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200365 mbedtls_md5_context md5;
366 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000367 unsigned char padding[16];
368 unsigned char sha1sum[20];
369 ((void)label);
370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200371 mbedtls_md5_init( &md5 );
372 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200373
Paul Bakker5f70b252012-09-13 14:23:06 +0000374 /*
375 * SSLv3:
376 * block =
377 * MD5( secret + SHA1( 'A' + secret + random ) ) +
378 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
379 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
380 * ...
381 */
382 for( i = 0; i < dlen / 16; i++ )
383 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200384 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100386 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100389 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100390 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100391 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100393 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100394 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100395 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000396
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100400 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100401 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100402 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100403 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100404 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000405 }
406
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200408 mbedtls_md5_free( &md5 );
409 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000410
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500411 mbedtls_platform_zeroize( padding, sizeof( padding ) );
412 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100414 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200419static int tls1_prf( const unsigned char *secret, size_t slen,
420 const char *label,
421 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000422 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t nb, hs;
425 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 unsigned char tmp[128];
428 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 const mbedtls_md_info_t *md_info;
430 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100431 int ret;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 hs = ( slen + 1 ) / 2;
439 S1 = secret;
440 S2 = secret + slen - hs;
441
442 nb = strlen( label );
443 memcpy( tmp + 20, label, nb );
444 memcpy( tmp + 20 + nb, random, rlen );
445 nb += rlen;
446
447 /*
448 * First compute P_md5(secret,label+random)[0..dlen]
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100454 return( ret );
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
457 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
458 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460 for( i = 0; i < dlen; i += 16 )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_md_hmac_reset ( &md_ctx );
463 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
464 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_hmac_reset ( &md_ctx );
467 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
468 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
471
472 for( j = 0; j < k; j++ )
473 dstbuf[i + j] = h_i[j];
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 /*
479 * XOR out with P_sha1(secret,label+random)[0..dlen]
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485 return( ret );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
488 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
489 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 for( i = 0; i < dlen; i += 20 )
492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_hmac_reset ( &md_ctx );
494 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
495 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_md_hmac_reset ( &md_ctx );
498 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
499 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
502
503 for( j = 0; j < k; j++ )
504 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500509 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
510 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 return( 0 );
513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500517#if defined(MBEDTLS_USE_PSA_CRYPTO)
518static int tls_prf_generic( mbedtls_md_type_t md_type,
519 const unsigned char *secret, size_t slen,
520 const char *label,
521 const unsigned char *random, size_t rlen,
522 unsigned char *dstbuf, size_t dlen )
523{
524 psa_status_t status;
525 psa_algorithm_t alg;
526 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500527 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500528 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
529
Andrzej Kurek2f760752019-01-28 08:08:15 -0500530 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500532
Andrzej Kurekc929a822019-01-14 03:51:11 -0500533 if( md_type == MBEDTLS_MD_SHA384 )
534 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
535 else
536 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
537
Andrzej Kurek2f760752019-01-28 08:08:15 -0500538 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500539 psa_key_policy_set_usage( &policy,
540 PSA_KEY_USAGE_DERIVE,
541 alg );
542 status = psa_set_key_policy( master_slot, &policy );
543 if( status != PSA_SUCCESS )
544 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
545
546 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
547 if( status != PSA_SUCCESS )
548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
549
550 status = psa_key_derivation( &generator,
551 master_slot, alg,
552 random, rlen,
553 (unsigned char const *) label,
554 (size_t) strlen( label ),
555 dlen );
556 if( status != PSA_SUCCESS )
557 {
558 psa_generator_abort( &generator );
559 psa_destroy_key( master_slot );
560 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
561 }
562
563 status = psa_generator_read( &generator, dstbuf, dlen );
564 if( status != PSA_SUCCESS )
565 {
566 psa_generator_abort( &generator );
567 psa_destroy_key( master_slot );
568 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
569 }
570
571 status = psa_generator_abort( &generator );
572 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500573 {
574 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500575 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500576 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500577
578 status = psa_destroy_key( master_slot );
579 if( status != PSA_SUCCESS )
580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
581
Andrzej Kurek33171262019-01-15 03:25:18 -0500582 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500583}
584
585#else /* MBEDTLS_USE_PSA_CRYPTO */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588 const unsigned char *secret, size_t slen,
589 const char *label,
590 const unsigned char *random, size_t rlen,
591 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592{
593 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100594 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
597 const mbedtls_md_info_t *md_info;
598 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100599 int ret;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607
608 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000610
611 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100612 memcpy( tmp + md_len, label, nb );
613 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 nb += rlen;
615
616 /*
617 * Compute P_<hash>(secret, label + random)[0..dlen]
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100620 return( ret );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
623 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
624 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100625
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
634 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100636 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500644 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
645 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646
647 return( 0 );
648}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500649#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651static int tls_prf_sha256( const unsigned char *secret, size_t slen,
652 const char *label,
653 const unsigned char *random, size_t rlen,
654 unsigned char *dstbuf, size_t dlen )
655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100657 label, random, rlen, dstbuf, dlen ) );
658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200662static int tls_prf_sha384( const unsigned char *secret, size_t slen,
663 const char *label,
664 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 unsigned char *dstbuf, size_t dlen )
666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_SHA512_C */
671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
676 defined(MBEDTLS_SSL_PROTO_TLS1_1)
677static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678#endif
Paul Bakker380da532012-04-18 16:10:25 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SSL_PROTO_SSL3)
681static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
682static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683#endif
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
686static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
687static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#endif
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
691#if defined(MBEDTLS_SHA256_C)
692static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
693static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
694static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SHA512_C)
698static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
699static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
700static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703
Hanno Becker7d0a5692018-10-23 15:26:22 +0100704#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
705 defined(MBEDTLS_USE_PSA_CRYPTO)
706static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
707{
708 if( ssl->conf->f_psk != NULL )
709 {
710 /* If we've used a callback to select the PSK,
711 * the static configuration is irrelevant. */
712 if( ssl->handshake->psk_opaque != 0 )
713 return( 1 );
714
715 return( 0 );
716 }
717
718 if( ssl->conf->psk_opaque != 0 )
719 return( 1 );
720
721 return( 0 );
722}
723#endif /* MBEDTLS_USE_PSA_CRYPTO &&
724 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200728 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000729#if defined(MBEDTLS_USE_PSA_CRYPTO)
730 int psa_fallthrough;
731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char keyblk[256];
734 unsigned char *key1;
735 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100736 unsigned char *mac_enc;
737 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000738 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000740 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 const mbedtls_cipher_info_t *cipher_info;
742 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100743
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000744 /* cf. RFC 5246, Section 8.1:
745 * "The master secret is always exactly 48 bytes in length." */
746 size_t const master_secret_len = 48;
747
Hanno Becker35b23c72018-10-23 12:10:41 +0100748#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
749 unsigned char session_hash[48];
750#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_ssl_session *session = ssl->session_negotiate;
753 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
754 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100759 if( cipher_info == NULL )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100762 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( md_info == NULL )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100770 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100772 }
773
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SSL_PROTO_SSL3)
778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 {
Paul Bakker48916f92012-09-16 19:57:18 +0000780 handshake->tls_prf = ssl3_prf;
781 handshake->calc_verify = ssl_calc_verify_ssl;
782 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784 else
785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
787 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 {
Paul Bakker48916f92012-09-16 19:57:18 +0000789 handshake->tls_prf = tls1_prf;
790 handshake->calc_verify = ssl_calc_verify_tls;
791 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793 else
794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
796#if defined(MBEDTLS_SHA512_C)
797 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
798 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000799 {
Paul Bakker48916f92012-09-16 19:57:18 +0000800 handshake->tls_prf = tls_prf_sha384;
801 handshake->calc_verify = ssl_calc_verify_tls_sha384;
802 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_SHA256_C)
807 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000808 {
Paul Bakker48916f92012-09-16 19:57:18 +0000809 handshake->tls_prf = tls_prf_sha256;
810 handshake->calc_verify = ssl_calc_verify_tls_sha256;
811 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000812 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813 else
814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200819 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
821 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 * SSLv3:
823 * master =
824 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
825 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
826 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200827 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * master = PRF( premaster, "master secret", randbytes )[0..47]
830 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100831 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
834 }
835 else
836 {
837 /* The label for the KDF used for key expansion.
838 * This is either "master secret" or "extended master secret"
839 * depending on whether the Extended Master Secret extension
840 * is used. */
841 char const *lbl = "master secret";
842
843 /* The salt for the KDF used for key expansion.
844 * - If the Extended Master Secret extension is not used,
845 * this is ClientHello.Random + ServerHello.Random
846 * (see Sect. 8.1 in RFC 5246).
847 * - If the Extended Master Secret extension is used,
848 * this is the transcript of the handshake so far.
849 * (see Sect. 4 in RFC 7627). */
850 unsigned char const *salt = handshake->randbytes;
851 size_t salt_len = 64;
852
853#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
854 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
855 ssl->transform_negotiate->ciphersuite_info;
856 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
857#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
860 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863
Hanno Becker35b23c72018-10-23 12:10:41 +0100864 lbl = "extended master secret";
865 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200866 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100871 if( md_type == MBEDTLS_MD_SHA384 )
872 salt_len = 48;
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
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200994 transform->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;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001004
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005 /* All modes haves 96-bit IVs;
1006 * GCM and CCM has 4 implicit and 8 explicit bytes
1007 * ChachaPoly has all 12 bytes implicit
1008 */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001010 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1011 transform->fixed_ivlen = 12;
1012 else
1013 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001014
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001015 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1016 taglen = transform->ciphersuite_info->flags &
1017 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1018
1019
1020 /* Minimum length of encrypted record */
1021 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1022 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 }
1024 else
1025 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001026 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1028 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001035 mac_key_len = mbedtls_md_get_size( md_info );
1036 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 /*
1040 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1041 * (rfc 6066 page 13 or rfc 2104 section 4),
1042 * so we only need to adjust the length here.
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001047
1048#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1049 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001050 * HMAC implementation which also truncates the key
1051 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001052 mac_key_len = transform->maclen;
1053#endif
1054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001056
1057 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001058 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001060 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001062 transform->minlen = transform->maclen;
1063 else
Paul Bakker68884e32013-01-07 18:20:04 +01001064 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065 /*
1066 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001067 * 1. if EtM is in use: one block plus MAC
1068 * otherwise: * first multiple of blocklen greater than maclen
1069 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1072 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001073 {
1074 transform->minlen = transform->maclen
1075 + cipher_info->block_size;
1076 }
1077 else
1078#endif
1079 {
1080 transform->minlen = transform->maclen
1081 + cipher_info->block_size
1082 - transform->maclen % cipher_info->block_size;
1083 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1087 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001088 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1093 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001094 {
1095 transform->minlen += transform->ivlen;
1096 }
1097 else
1098#endif
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 }
Paul Bakker68884e32013-01-07 18:20:04 +01001103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001107 transform->keylen, transform->minlen, transform->ivlen,
1108 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110 /*
1111 * Finally setup the cipher contexts, IVs and MAC secrets.
1112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001116 key1 = keyblk + mac_key_len * 2;
1117 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001122 /*
1123 * This is not used in TLS v1.1.
1124 */
Paul Bakker48916f92012-09-16 19:57:18 +00001125 iv_copy_len = ( transform->fixed_ivlen ) ?
1126 transform->fixed_ivlen : transform->ivlen;
1127 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1128 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_CLI_C */
1133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001134 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1137 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Hanno Becker81c7b182017-11-09 18:39:33 +00001139 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001140 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001142 /*
1143 * This is not used in TLS v1.1.
1144 */
Paul Bakker48916f92012-09-16 19:57:18 +00001145 iv_copy_len = ( transform->fixed_ivlen ) ?
1146 transform->fixed_ivlen : transform->ivlen;
1147 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1148 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001149 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_SSL_PROTO_SSL3)
1159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001165 }
1166
Hanno Becker81c7b182017-11-09 18:39:33 +00001167 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1168 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001169 }
1170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1174 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001175 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001176 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1177 For AEAD-based ciphersuites, there is nothing to do here. */
1178 if( mac_key_len != 0 )
1179 {
1180 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1181 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1182 }
Paul Bakker68884e32013-01-07 18:20:04 +01001183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001184 else
1185#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001189 }
Paul Bakker68884e32013-01-07 18:20:04 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1192 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001193 {
1194 int ret = 0;
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001199 transform->iv_enc, transform->iv_dec,
1200 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001201 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001202 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1205 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001206 }
1207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001209
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001210#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1211 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001212 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001213 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1214 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001215 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001216 iv_copy_len );
1217 }
1218#endif
1219
Hanno Beckerf704bef2018-11-16 15:21:18 +00001220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001221
1222 /* Only use PSA-based ciphers for TLS-1.2.
1223 * That's relevant at least for TLS-1.0, where
1224 * we assume that mbedtls_cipher_crypt() updates
1225 * the structure field for the IV, which the PSA-based
1226 * implementation currently doesn't. */
1227#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1228 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001229 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001230 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1231 cipher_info, taglen );
1232 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1233 {
1234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1235 return( ret );
1236 }
1237
1238 if( ret == 0 )
1239 {
1240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1241 psa_fallthrough = 0;
1242 }
1243 else
1244 {
1245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1246 psa_fallthrough = 1;
1247 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001250 psa_fallthrough = 1;
1251#else
1252 psa_fallthrough = 1;
1253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001254
Hanno Beckercb1cc802018-11-17 22:27:38 +00001255 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001256#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001257 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001258 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001261 return( ret );
1262 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001263
Hanno Beckerf704bef2018-11-16 15:21:18 +00001264#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001265 /* Only use PSA-based ciphers for TLS-1.2.
1266 * That's relevant at least for TLS-1.0, where
1267 * we assume that mbedtls_cipher_crypt() updates
1268 * the structure field for the IV, which the PSA-based
1269 * implementation currently doesn't. */
1270#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1271 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001273 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1274 cipher_info, taglen );
1275 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1278 return( ret );
1279 }
1280
1281 if( ret == 0 )
1282 {
1283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1284 psa_fallthrough = 0;
1285 }
1286 else
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1289 psa_fallthrough = 1;
1290 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001291 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001292 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001293 psa_fallthrough = 1;
1294#else
1295 psa_fallthrough = 1;
1296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001297
Hanno Beckercb1cc802018-11-17 22:27:38 +00001298 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001299#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001300 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001301 cipher_info ) ) != 0 )
1302 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001304 return( ret );
1305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001308 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 return( ret );
1313 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001316 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001320 return( ret );
1321 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_CIPHER_MODE_CBC)
1324 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1327 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001330 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001331 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1334 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337 return( ret );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001342 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001345 // Initialize compression
1346 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001348 {
Paul Bakker16770332013-10-11 09:59:44 +02001349 if( ssl->compress_buf == NULL )
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001352 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001353 if( ssl->compress_buf == NULL )
1354 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001356 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001357 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001358 }
1359 }
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001362
Paul Bakker48916f92012-09-16 19:57:18 +00001363 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1364 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001365
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001366 if( deflateInit( &transform->ctx_deflate,
1367 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001368 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1371 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001372 }
1373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 return( 0 );
1379}
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_SSL_PROTO_SSL3)
1382void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001384 mbedtls_md5_context md5;
1385 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 unsigned char pad_1[48];
1387 unsigned char pad_2[48];
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001391 mbedtls_md5_init( &md5 );
1392 mbedtls_sha1_init( &sha1 );
1393
1394 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1395 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Paul Bakker380da532012-04-18 16:10:25 +00001397 memset( pad_1, 0x36, 48 );
1398 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1401 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1402 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001404 mbedtls_md5_starts_ret( &md5 );
1405 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1406 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1407 mbedtls_md5_update_ret( &md5, hash, 16 );
1408 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1411 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1412 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001413
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001414 mbedtls_sha1_starts_ret( &sha1 );
1415 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1416 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1417 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1418 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001423 mbedtls_md5_free( &md5 );
1424 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001425
Paul Bakker380da532012-04-18 16:10:25 +00001426 return;
1427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1431void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Andrzej Kurekeb342242019-01-29 09:14:33 -05001444 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1458#if defined(MBEDTLS_SHA256_C)
1459void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001460{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001461#if defined(MBEDTLS_USE_PSA_CRYPTO)
1462 size_t hash_size;
1463 psa_status_t status;
1464 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1465
1466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1467 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1468 if( status != PSA_SUCCESS )
1469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1471 return;
1472 }
1473
1474 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1475 if( status != PSA_SUCCESS )
1476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1478 return;
1479 }
1480 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1482#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001483 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001485 mbedtls_sha256_init( &sha256 );
1486
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001488
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001489 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001490 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001496#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001497 return;
1498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SHA512_C)
1502void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001503{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001504#if defined(MBEDTLS_USE_PSA_CRYPTO)
1505 size_t hash_size;
1506 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001507 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001508
1509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001510 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001511 if( status != PSA_SUCCESS )
1512 {
1513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1514 return;
1515 }
1516
Andrzej Kurek972fba52019-01-30 03:29:12 -05001517 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001518 if( status != PSA_SUCCESS )
1519 {
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1521 return;
1522 }
1523 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1525#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001526 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001527
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001528 mbedtls_sha512_init( &sha512 );
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001531
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001532 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001533 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001538 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 return;
1541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_SHA512_C */
1543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1546int 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 +02001547{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001548 unsigned char *p = ssl->handshake->premaster;
1549 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001550 const unsigned char *psk = ssl->conf->psk;
1551 size_t psk_len = ssl->conf->psk_len;
1552
1553 /* If the psk callback was called, use its result */
1554 if( ssl->handshake->psk != NULL )
1555 {
1556 psk = ssl->handshake->psk;
1557 psk_len = ssl->handshake->psk_len;
1558 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
1560 /*
1561 * PMS = struct {
1562 * opaque other_secret<0..2^16-1>;
1563 * opaque psk<0..2^16-1>;
1564 * };
1565 * with "other_secret" depending on the particular key exchange
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1568 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001570 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001572
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001573 *(p++) = (unsigned char)( psk_len >> 8 );
1574 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001575
1576 if( end < p || (size_t)( end - p ) < psk_len )
1577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1578
1579 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001580 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 }
1582 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1584#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1585 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001586 {
1587 /*
1588 * other_secret already set by the ClientKeyExchange message,
1589 * and is 48 bytes long
1590 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001591 if( end - p < 2 )
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001594 *p++ = 0;
1595 *p++ = 48;
1596 p += 48;
1597 }
1598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1600#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1601 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001603 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001604 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001606 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001608 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001609 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 return( ret );
1613 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001614 *(p++) = (unsigned char)( len >> 8 );
1615 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 p += len;
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1622#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1623 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001624 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001625 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 size_t zlen;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001629 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001630 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 return( ret );
1634 }
1635
1636 *(p++) = (unsigned char)( zlen >> 8 );
1637 *(p++) = (unsigned char)( zlen );
1638 p += zlen;
1639
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1641 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001642 }
1643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 }
1649
1650 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001651 if( end - p < 2 )
1652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001653
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001654 *(p++) = (unsigned char)( psk_len >> 8 );
1655 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001656
1657 if( end < p || (size_t)( end - p ) < psk_len )
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001660 memcpy( p, psk, psk_len );
1661 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001662
1663 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1664
1665 return( 0 );
1666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001670/*
1671 * SSLv3.0 MAC functions
1672 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001673#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674static void ssl_mac( mbedtls_md_context_t *md_ctx,
1675 const unsigned char *secret,
1676 const unsigned char *buf, size_t len,
1677 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001678 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
1680 unsigned char header[11];
1681 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001682 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1684 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001685
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001686 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001688 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001689 else
Paul Bakker68884e32013-01-07 18:20:04 +01001690 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
1692 memcpy( header, ctr, 8 );
1693 header[ 8] = (unsigned char) type;
1694 header[ 9] = (unsigned char)( len >> 8 );
1695 header[10] = (unsigned char)( len );
1696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_starts( md_ctx );
1699 mbedtls_md_update( md_ctx, secret, md_size );
1700 mbedtls_md_update( md_ctx, padding, padlen );
1701 mbedtls_md_update( md_ctx, header, 11 );
1702 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001703 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Paul Bakker68884e32013-01-07 18:20:04 +01001705 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_md_starts( md_ctx );
1707 mbedtls_md_update( md_ctx, secret, md_size );
1708 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001709 mbedtls_md_update( md_ctx, out, md_size );
1710 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1715 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001716 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001717#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001718#endif
1719
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001720/* The function below is only used in the Lucky 13 counter-measure in
1721 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1722#if defined(SSL_SOME_MODES_USE_MAC) && \
1723 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1724 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1725 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1726/* This function makes sure every byte in the memory region is accessed
1727 * (in ascending addresses order) */
1728static void ssl_read_memory( unsigned char *p, size_t len )
1729{
1730 unsigned char acc = 0;
1731 volatile unsigned char force;
1732
1733 for( ; len != 0; p++, len-- )
1734 acc ^= *p;
1735
1736 force = acc;
1737 (void) force;
1738}
1739#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1740
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001741/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001747 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 }
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001760 ssl->out_msg, ssl->out_msglen );
1761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001763 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001765#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 if( mode == MBEDTLS_MODE_STREAM ||
1767 ( mode == MBEDTLS_MODE_CBC
1768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1769 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001770#endif
1771 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_PROTO_SSL3)
1774 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001775 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001776 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001777
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 ssl_mac( &ssl->transform_out->md_ctx_enc,
1779 ssl->transform_out->mac_enc,
1780 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001781 ssl->out_ctr, ssl->out_msgtype,
1782 mac );
1783
1784 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 }
1786 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1789 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 {
Hanno Becker992b6872017-11-09 18:57:39 +00001792 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1795 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1796 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1797 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001799 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001801
1802 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 ssl->out_msg + ssl->out_msglen,
1813 ssl->transform_out->maclen );
1814
1815 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001816 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001817 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001818#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 /*
1821 * Encrypt
1822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1824 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001826 int ret;
1827 size_t olen = 0;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 "including %d bytes of padding",
1831 ssl->out_msglen, 0 ) );
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001834 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001835 ssl->transform_out->ivlen,
1836 ssl->out_msg, ssl->out_msglen,
1837 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001840 return( ret );
1841 }
1842
1843 if( ssl->out_msglen != olen )
1844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 }
Paul Bakker68884e32013-01-07 18:20:04 +01001849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851#if defined(MBEDTLS_GCM_C) || \
1852 defined(MBEDTLS_CCM_C) || \
1853 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 mode == MBEDTLS_MODE_CCM ||
1856 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001857 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001858 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001859 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001860 unsigned char *enc_msg;
1861 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 unsigned char iv[12];
1863 mbedtls_ssl_transform *transform = ssl->transform_out;
1864 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001866 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001867
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 /*
1869 * Prepare additional authenticated data
1870 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001871 memcpy( add_data, ssl->out_ctr, 8 );
1872 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001874 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001875 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1876 add_data[12] = ssl->out_msglen & 0xFF;
1877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001879
Paul Bakker68884e32013-01-07 18:20:04 +01001880 /*
1881 * Generate IV
1882 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1884 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001885 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001886 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1887 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1888 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1889
1890 }
1891 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1892 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001893 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001894 unsigned char i;
1895
1896 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1897
1898 for( i = 0; i < 8; i++ )
1899 iv[i+4] ^= ssl->out_ctr[i];
1900 }
1901 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001902 {
1903 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001906 }
1907
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1909 iv, transform->ivlen );
1910 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1911 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001912
Paul Bakker68884e32013-01-07 18:20:04 +01001913 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001915 */
1916 enc_msg = ssl->out_msg;
1917 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001921 "including 0 bytes of padding",
1922 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923
Paul Bakker68884e32013-01-07 18:20:04 +01001924 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001925 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001926 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1928 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001929 add_data, 13,
1930 enc_msg, enc_msglen,
1931 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001932 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001935 return( ret );
1936 }
1937
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001938 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001942 }
1943
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001944 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001945 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1951#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001952 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001955 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001956 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001957 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1960 ssl->transform_out->ivlen;
1961 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 padlen = 0;
1963
1964 for( i = 0; i <= padlen; i++ )
1965 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1966
1967 ssl->out_msglen += padlen + 1;
1968
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001969 enc_msglen = ssl->out_msglen;
1970 enc_msg = ssl->out_msg;
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001974 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1975 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001978 {
1979 /*
1980 * Generate IV
1981 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001982 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001983 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001984 if( ret != 0 )
1985 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986
Paul Bakker92be97b2013-01-02 17:30:03 +01001987 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001988 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989
1990 /*
1991 * Fix pointer positions and message length with added IV
1992 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001993 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001995 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002001 ssl->out_msglen, ssl->transform_out->ivlen,
2002 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02002005 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002006 ssl->transform_out->ivlen,
2007 enc_msg, enc_msglen,
2008 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002011 return( ret );
2012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002013
Paul Bakkercca5b812013-08-31 17:40:26 +02002014 if( enc_msglen != olen )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002018 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2021 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002022 {
2023 /*
2024 * Save IV in SSL3 and TLS1
2025 */
2026 memcpy( ssl->transform_out->iv_enc,
2027 ssl->transform_out->cipher_ctx_enc.iv,
2028 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002030#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002033 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002034 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002035 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2036
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 /*
2038 * MAC(MAC_write_key, seq_num +
2039 * TLSCipherText.type +
2040 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002041 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002042 * IV + // except for TLS 1.0
2043 * ENC(content + padding + padding_length));
2044 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002045 unsigned char pseudo_hdr[13];
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002049 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2050 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002051 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2052 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2057 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002058 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002059 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002061
Hanno Becker3d8c9072018-01-05 16:24:22 +00002062 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2063 ssl->transform_out->maclen );
2064
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002065 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002066 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002072 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002078 /* Make extra sure authentication was performed, exactly once */
2079 if( auth_done != 1 )
2080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002083 }
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 return( 0 );
2088}
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002094#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002095 size_t padlen = 0, correct = 1;
2096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002107
Paul Bakker48916f92012-09-16 19:57:18 +00002108 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002111 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2116 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002117 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002118 int ret;
2119 size_t olen = 0;
2120
Paul Bakker68884e32013-01-07 18:20:04 +01002121 padlen = 0;
2122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002124 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002125 ssl->transform_in->ivlen,
2126 ssl->in_msg, ssl->in_msglen,
2127 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002130 return( ret );
2131 }
2132
2133 if( ssl->in_msglen != olen )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Paul Bakker68884e32013-01-07 18:20:04 +01002139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141#if defined(MBEDTLS_GCM_C) || \
2142 defined(MBEDTLS_CCM_C) || \
2143 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002145 mode == MBEDTLS_MODE_CCM ||
2146 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002147 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002148 int ret;
2149 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002150 unsigned char *dec_msg;
2151 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002152 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002153 unsigned char iv[12];
2154 mbedtls_ssl_transform *transform = ssl->transform_in;
2155 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002157 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002158
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002159 /*
2160 * Compute and update sizes
2161 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002162 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002165 "+ taglen (%d)", ssl->in_msglen,
2166 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002168 }
2169 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2170
Paul Bakker68884e32013-01-07 18:20:04 +01002171 dec_msg = ssl->in_msg;
2172 dec_msg_result = ssl->in_msg;
2173 ssl->in_msglen = dec_msglen;
2174
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002175 /*
2176 * Prepare additional authenticated data
2177 */
Paul Bakker68884e32013-01-07 18:20:04 +01002178 memcpy( add_data, ssl->in_ctr, 8 );
2179 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002181 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002182 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2183 add_data[12] = ssl->in_msglen & 0xFF;
2184
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002185 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002186
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002187 /*
2188 * Prepare IV
2189 */
2190 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2191 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002192 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002193 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2194 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002195
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002196 }
2197 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2198 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002199 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002200 unsigned char i;
2201
2202 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2203
2204 for( i = 0; i < 8; i++ )
2205 iv[i+4] ^= ssl->in_ctr[i];
2206 }
2207 else
2208 {
2209 /* Reminder if we ever add an AEAD mode with a different size */
2210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2212 }
2213
2214 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002216
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002217 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002218 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002221 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002222 add_data, 13,
2223 dec_msg, dec_msglen,
2224 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002225 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2230 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002231
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002232 return( ret );
2233 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002234 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002235
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002236 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002240 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2244#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002245 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 {
Paul Bakker45829992013-01-03 14:52:21 +01002248 /*
2249 * Decrypt and check the padding
2250 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002251 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 unsigned char *dec_msg;
2253 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002254 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002255 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002256 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002257
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 /*
Paul Bakker45829992013-01-03 14:52:21 +01002259 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2262 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002263 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002264#endif
Paul Bakker45829992013-01-03 14:52:21 +01002265
2266 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2267 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002270 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2271 ssl->transform_in->ivlen,
2272 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002274 }
2275
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002276 dec_msglen = ssl->in_msglen;
2277 dec_msg = ssl->in_msg;
2278 dec_msg_result = ssl->in_msg;
2279
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002280 /*
2281 * Authenticate before decrypt if enabled
2282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2284 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002285 {
Hanno Becker992b6872017-11-09 18:57:39 +00002286 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002287 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002291 dec_msglen -= ssl->transform_in->maclen;
2292 ssl->in_msglen -= ssl->transform_in->maclen;
2293
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002294 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2295 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2296 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2297 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2302 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002304 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002308 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002309 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 ssl->transform_in->maclen );
2311
Hanno Becker992b6872017-11-09 18:57:39 +00002312 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2313 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002318 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002322
2323 /*
2324 * Check length sanity
2325 */
2326 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002331 }
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002334 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002335 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002338 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002340 dec_msglen -= ssl->transform_in->ivlen;
2341 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002342
Paul Bakker48916f92012-09-16 19:57:18 +00002343 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002344 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002349 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002350 ssl->transform_in->ivlen,
2351 dec_msg, dec_msglen,
2352 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002355 return( ret );
2356 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002357
Paul Bakkercca5b812013-08-31 17:40:26 +02002358 if( dec_msglen != olen )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002362 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2365 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002366 {
2367 /*
2368 * Save IV in SSL3 and TLS1
2369 */
2370 memcpy( ssl->transform_in->iv_dec,
2371 ssl->transform_in->cipher_ctx_dec.iv,
2372 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002377
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002378 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002379 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_SSL_DEBUG_ALL)
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002383 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002384#endif
Paul Bakker45829992013-01-03 14:52:21 +01002385 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002386 correct = 0;
2387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_PROTO_SSL3)
2390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Paul Bakker48916f92012-09-16 19:57:18 +00002392 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_SSL_DEBUG_ALL)
2395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002397 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002398#endif
Paul Bakker45829992013-01-03 14:52:21 +01002399 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401 }
2402 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2406 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
2408 /*
Paul Bakker45829992013-01-03 14:52:21 +01002409 * TLSv1+: always check the padding up to the first failure
2410 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002412 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002413 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002415
Paul Bakker956c9e02013-12-19 14:42:28 +01002416 /*
2417 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002418 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002419 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002420 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002421 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002422 *
2423 * In both cases we reset padding_idx to a safe value (0) to
2424 * prevent out-of-buffer reads.
2425 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002426 correct &= ( padlen <= ssl->in_msglen );
2427 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002428 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002429
2430 padding_idx *= correct;
2431
Angus Grattonb512bc12018-06-19 15:57:50 +10002432 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002433 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002434 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002435 pad_count += real_count *
2436 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2437 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002438
2439 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002442 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002444#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002445 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002447 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2449 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002453 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002454
2455 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002459 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002465#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 * Authenticate if not done yet.
2472 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002474#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002478
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002479 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002481 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2482 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_PROTO_SSL3)
2485 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002486 {
2487 ssl_mac( &ssl->transform_in->md_ctx_dec,
2488 ssl->transform_in->mac_dec,
2489 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002490 ssl->in_ctr, ssl->in_msgtype,
2491 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002492 }
2493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2495#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2497 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002498 {
2499 /*
2500 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002501 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002502 *
2503 * Known timing attacks:
2504 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2505 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002506 * To compensate for different timings for the MAC calculation
2507 * depending on how much padding was removed (which is determined
2508 * by padlen), process extra_run more blocks through the hash
2509 * function.
2510 *
2511 * The formula in the paper is
2512 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2513 * where L1 is the size of the header plus the decrypted message
2514 * plus CBC padding and L2 is the size of the header plus the
2515 * decrypted message. This is for an underlying hash function
2516 * with 64-byte blocks.
2517 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2518 * correctly. We round down instead of up, so -56 is the correct
2519 * value for our calculations instead of -55.
2520 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002521 * Repeat the formula rather than defining a block_size variable.
2522 * This avoids requiring division by a variable at runtime
2523 * (which would be marginally less efficient and would require
2524 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002525 */
2526 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002527
2528 /*
2529 * The next two sizes are the minimum and maximum values of
2530 * in_msglen over all padlen values.
2531 *
2532 * They're independent of padlen, since we previously did
2533 * in_msglen -= padlen.
2534 *
2535 * Note that max_len + maclen is never more than the buffer
2536 * length, as we previously did in_msglen -= maclen too.
2537 */
2538 const size_t max_len = ssl->in_msglen + padlen;
2539 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2540
Gilles Peskine20b44082018-05-29 14:06:49 +02002541 switch( ssl->transform_in->ciphersuite_info->mac )
2542 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002543#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2544 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002545 case MBEDTLS_MD_MD5:
2546 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002548 /* 8 bytes of message size, 64-byte compression blocks */
2549 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2550 ( 13 + ssl->in_msglen + 8 ) / 64;
2551 break;
2552#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002553#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002554 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002555 /* 16 bytes of message size, 128-byte compression blocks */
2556 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2557 ( 13 + ssl->in_msglen + 16 ) / 128;
2558 break;
2559#endif
2560 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2563 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002564
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002565 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2568 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2569 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2570 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002572 /* Make sure we access everything even when padlen > 0. This
2573 * makes the synchronisation requirements for just-in-time
2574 * Prime+Probe attacks much tighter and hopefully impractical. */
2575 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002576 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002577
2578 /* Call mbedtls_md_process at least once due to cache attacks
2579 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002580 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002584
2585 /* Make sure we access all the memory that could contain the MAC,
2586 * before we check it in the next code block. This makes the
2587 * synchronisation requirements for just-in-time Prime+Probe
2588 * attacks much tighter and hopefully impractical. */
2589 ssl_read_memory( ssl->in_msg + min_len,
2590 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002591 }
2592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2594 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002600#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002601 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2602 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2603 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker992b6872017-11-09 18:57:39 +00002606 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2607 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_DEBUG_ALL)
2610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002611#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002612 correct = 0;
2613 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002614 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002615 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002616
2617 /*
2618 * Finally check the correct flag
2619 */
2620 if( correct == 0 )
2621 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002622#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002623
2624 /* Make extra sure authentication was performed, exactly once */
2625 if( auth_done != 1 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
2631 if( ssl->in_msglen == 0 )
2632 {
Angus Gratton34817922018-06-19 15:58:22 +10002633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2634 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2635 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2636 {
2637 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2639 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2640 }
2641#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2642
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 ssl->nb_zero++;
2644
2645 /*
2646 * Three or more empty messages may be a DoS attack
2647 * (excessive CPU consumption).
2648 */
2649 if( ssl->nb_zero > 3 )
2650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655 }
2656 else
2657 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002661 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002662 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002663 }
2664 else
2665#endif
2666 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002667 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002668 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2669 if( ++ssl->in_ctr[i - 1] != 0 )
2670 break;
2671
2672 /* The loop goes to its end iff the counter is wrapping */
2673 if( i == ssl_ep_len( ssl ) )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2676 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002677 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002678 }
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
2682 return( 0 );
2683}
2684
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002685#undef MAC_NONE
2686#undef MAC_PLAINTEXT
2687#undef MAC_CIPHERTEXT
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690/*
2691 * Compression/decompression functions
2692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002694{
2695 int ret;
2696 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002697 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002698 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002699 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002703 if( len_pre == 0 )
2704 return( 0 );
2705
Paul Bakker2770fbd2012-07-03 13:30:23 +00002706 memcpy( msg_pre, ssl->out_msg, len_pre );
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 ssl->out_msglen ) );
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712 ssl->out_msg, ssl->out_msglen );
2713
Paul Bakker48916f92012-09-16 19:57:18 +00002714 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2715 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2716 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002717 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718
Paul Bakker48916f92012-09-16 19:57:18 +00002719 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720 if( ret != Z_OK )
2721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2723 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724 }
2725
Angus Grattond8213d02016-05-25 20:56:48 +10002726 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002727 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730 ssl->out_msglen ) );
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733 ssl->out_msg, ssl->out_msglen );
2734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736
2737 return( 0 );
2738}
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741{
2742 int ret;
2743 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002744 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002746 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002750 if( len_pre == 0 )
2751 return( 0 );
2752
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 memcpy( msg_pre, ssl->in_msg, len_pre );
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756 ssl->in_msglen ) );
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759 ssl->in_msg, ssl->in_msglen );
2760
Paul Bakker48916f92012-09-16 19:57:18 +00002761 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2762 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2763 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002764 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002765 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002768 if( ret != Z_OK )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2771 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772 }
2773
Angus Grattond8213d02016-05-25 20:56:48 +10002774 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002775 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002778 ssl->in_msglen ) );
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 ssl->in_msg, ssl->in_msglen );
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784
2785 return( 0 );
2786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2790static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_DTLS)
2793static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002794{
2795 /* If renegotiation is not enforced, retransmit until we would reach max
2796 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002797 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002798 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002799 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002800 unsigned char doublings = 1;
2801
2802 while( ratio != 0 )
2803 {
2804 ++doublings;
2805 ratio >>= 1;
2806 }
2807
2808 if( ++ssl->renego_records_seen > doublings )
2809 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002811 return( 0 );
2812 }
2813 }
2814
2815 return( ssl_write_hello_request( ssl ) );
2816}
2817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002821 * Fill the input message buffer by appending data to it.
2822 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002823 *
2824 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2825 * available (from this read and/or a previous one). Otherwise, an error code
2826 * is returned (possibly EOF or WANT_READ).
2827 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002828 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2829 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2830 * since we always read a whole datagram at once.
2831 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002832 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002833 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836{
Paul Bakker23986e52011-04-24 08:57:21 +00002837 int ret;
2838 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002842 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002845 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002847 }
2848
Angus Grattond8213d02016-05-25 20:56:48 +10002849 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002853 }
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002858 uint32_t timeout;
2859
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002860 /* Just to be sure */
2861 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2862 {
2863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2864 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2866 }
2867
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002868 /*
2869 * The point is, we need to always read a full datagram at once, so we
2870 * sometimes read more then requested, and handle the additional data.
2871 * It could be the rest of the current record (while fetching the
2872 * header) and/or some other records in the same datagram.
2873 */
2874
2875 /*
2876 * Move to the next record in the already read datagram if applicable
2877 */
2878 if( ssl->next_record_offset != 0 )
2879 {
2880 if( ssl->in_left < ssl->next_record_offset )
2881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002884 }
2885
2886 ssl->in_left -= ssl->next_record_offset;
2887
2888 if( ssl->in_left != 0 )
2889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002891 ssl->next_record_offset ) );
2892 memmove( ssl->in_hdr,
2893 ssl->in_hdr + ssl->next_record_offset,
2894 ssl->in_left );
2895 }
2896
2897 ssl->next_record_offset = 0;
2898 }
2899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002902
2903 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002904 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002905 */
2906 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002909 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002910 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002911
2912 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002913 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002914 * are not at the beginning of a new record, the caller did something
2915 * wrong.
2916 */
2917 if( ssl->in_left != 0 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002921 }
2922
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002923 /*
2924 * Don't even try to read if time's out already.
2925 * This avoids by-passing the timer when repeatedly receiving messages
2926 * that will end up being dropped.
2927 */
2928 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002929 {
2930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002931 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002932 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002933 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002934 {
Angus Grattond8213d02016-05-25 20:56:48 +10002935 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002938 timeout = ssl->handshake->retransmit_timeout;
2939 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002940 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002943
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002944 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002945 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2946 timeout );
2947 else
2948 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002951
2952 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002954 }
2955
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002956 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002959 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002962 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002963 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002966 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002967 }
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002972 return( ret );
2973 }
2974
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002975 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002978 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002980 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002984 return( ret );
2985 }
2986
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002987 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002990 }
2991
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 if( ret < 0 )
2993 return( ret );
2994
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002995 ssl->in_left = ret;
2996 }
2997 else
2998#endif
2999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003001 ssl->in_left, nb_want ) );
3002
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003003 while( ssl->in_left < nb_want )
3004 {
3005 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003006
3007 if( ssl_check_timer( ssl ) != 0 )
3008 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3009 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003010 {
3011 if( ssl->f_recv_timeout != NULL )
3012 {
3013 ret = ssl->f_recv_timeout( ssl->p_bio,
3014 ssl->in_hdr + ssl->in_left, len,
3015 ssl->conf->read_timeout );
3016 }
3017 else
3018 {
3019 ret = ssl->f_recv( ssl->p_bio,
3020 ssl->in_hdr + ssl->in_left, len );
3021 }
3022 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003025 ssl->in_left, nb_want ) );
3026 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003027
3028 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003030
3031 if( ret < 0 )
3032 return( ret );
3033
mohammad160352aecb92018-03-28 23:41:40 -07003034 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003035 {
Darryl Green11999bb2018-03-13 15:22:58 +00003036 MBEDTLS_SSL_DEBUG_MSG( 1,
3037 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003038 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3040 }
3041
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003042 ssl->in_left += ret;
3043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 return( 0 );
3049}
3050
3051/*
3052 * Flush any data not yet written
3053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003055{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003056 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003057 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003061 if( ssl->f_send == NULL )
3062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003064 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003066 }
3067
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003068 /* Avoid incrementing counter if data is flushed */
3069 if( ssl->out_left == 0 )
3070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003072 return( 0 );
3073 }
3074
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 while( ssl->out_left > 0 )
3076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3078 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Hanno Becker2b1e3542018-08-06 11:19:13 +01003080 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003081 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
3085 if( ret <= 0 )
3086 return( ret );
3087
mohammad160352aecb92018-03-28 23:41:40 -07003088 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003089 {
Darryl Green11999bb2018-03-13 15:22:58 +00003090 MBEDTLS_SSL_DEBUG_MSG( 1,
3091 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003092 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3094 }
3095
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 ssl->out_left -= ret;
3097 }
3098
Hanno Becker2b1e3542018-08-06 11:19:13 +01003099#if defined(MBEDTLS_SSL_PROTO_DTLS)
3100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003101 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003102 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003103 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003104 else
3105#endif
3106 {
3107 ssl->out_hdr = ssl->out_buf + 8;
3108 }
3109 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
3113 return( 0 );
3114}
3115
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003116/*
3117 * Functions to handle the DTLS retransmission state machine
3118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120/*
3121 * Append current handshake message to current outgoing flight
3122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3127 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3128 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003131 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003135 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 }
3137
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003138 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003143 }
3144
3145 /* Copy current handshake message with headers */
3146 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3147 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003148 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149 msg->next = NULL;
3150
3151 /* Append to the current flight */
3152 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003153 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154 else
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157 while( cur->next != NULL )
3158 cur = cur->next;
3159 cur->next = msg;
3160 }
3161
Hanno Becker3b235902018-08-06 09:54:53 +01003162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163 return( 0 );
3164}
3165
3166/*
3167 * Free the current flight of handshake messages
3168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_ssl_flight_item *cur = flight;
3172 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173
3174 while( cur != NULL )
3175 {
3176 next = cur->next;
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_free( cur->p );
3179 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180
3181 cur = next;
3182 }
3183}
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3186static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003187#endif
3188
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003190 * Swap transform_out and out_ctr with the alternative ones
3191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003195 unsigned char tmp_out_ctr[8];
3196
3197 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003200 return;
3201 }
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003204
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003205 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003206 tmp_transform = ssl->transform_out;
3207 ssl->transform_out = ssl->handshake->alt_transform_out;
3208 ssl->handshake->alt_transform_out = tmp_transform;
3209
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003210 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003211 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3212 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003213 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003214
3215 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003216 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3219 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3224 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003225 }
3226 }
3227#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228}
3229
3230/*
3231 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003232 */
3233int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3234{
3235 int ret = 0;
3236
3237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3238
3239 ret = mbedtls_ssl_flight_transmit( ssl );
3240
3241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3242
3243 return( ret );
3244}
3245
3246/*
3247 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003248 *
3249 * Need to remember the current message in case flush_output returns
3250 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003251 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003253int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003255 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003259 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261
3262 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003263 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003264 ssl_swap_epochs( ssl );
3265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003267 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003268
3269 while( ssl->handshake->cur_msg != NULL )
3270 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003272 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003273
Hanno Beckere1dcb032018-08-17 16:47:58 +01003274 int const is_finished =
3275 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3276 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3277
Hanno Becker04da1892018-08-14 13:22:10 +01003278 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3279 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3280
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003281 /* Swap epochs before sending Finished: we can't do it after
3282 * sending ChangeCipherSpec, in case write returns WANT_READ.
3283 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003284 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003285 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003287 ssl_swap_epochs( ssl );
3288 }
3289
Hanno Becker67bc7c32018-08-06 11:33:50 +01003290 ret = ssl_get_remaining_payload_in_datagram( ssl );
3291 if( ret < 0 )
3292 return( ret );
3293 max_frag_len = (size_t) ret;
3294
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003295 /* CCS is copied as is, while HS messages may need fragmentation */
3296 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3297 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298 if( max_frag_len == 0 )
3299 {
3300 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3301 return( ret );
3302
3303 continue;
3304 }
3305
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003306 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003308 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003310 /* Update position inside current message */
3311 ssl->handshake->cur_msg_p += cur->len;
3312 }
3313 else
3314 {
3315 const unsigned char * const p = ssl->handshake->cur_msg_p;
3316 const size_t hs_len = cur->len - 12;
3317 const size_t frag_off = p - ( cur->p + 12 );
3318 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320
Hanno Beckere1dcb032018-08-17 16:47:58 +01003321 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003322 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003323 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
3329 continue;
3330 }
3331 max_hs_frag_len = max_frag_len - 12;
3332
3333 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3334 max_hs_frag_len : rem_len;
3335
3336 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003337 {
3338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003339 (unsigned) cur_hs_frag_len,
3340 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003341 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003342
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003343 /* Messages are stored with handshake headers as if not fragmented,
3344 * copy beginning of headers then fill fragmentation fields.
3345 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3346 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003348 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3349 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3350 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3351
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3353 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3354 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003355
3356 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3357
Hanno Becker3f7b9732018-08-28 09:53:25 +01003358 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003359 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3360 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003361 ssl->out_msgtype = cur->type;
3362
3363 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003364 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003365 }
3366
3367 /* If done with the current message move to the next one if any */
3368 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3369 {
3370 if( cur->next != NULL )
3371 {
3372 ssl->handshake->cur_msg = cur->next;
3373 ssl->handshake->cur_msg_p = cur->next->p + 12;
3374 }
3375 else
3376 {
3377 ssl->handshake->cur_msg = NULL;
3378 ssl->handshake->cur_msg_p = NULL;
3379 }
3380 }
3381
3382 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003383 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386 return( ret );
3387 }
3388 }
3389
Hanno Becker67bc7c32018-08-06 11:33:50 +01003390 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3391 return( ret );
3392
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003393 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3395 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003396 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3400 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003401
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003403
3404 return( 0 );
3405}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406
3407/*
3408 * To be called when the last message of an incoming flight is received.
3409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003411{
3412 /* We won't need to resend that one any more */
3413 ssl_flight_free( ssl->handshake->flight );
3414 ssl->handshake->flight = NULL;
3415 ssl->handshake->cur_msg = NULL;
3416
3417 /* The next incoming flight will start with this msg_seq */
3418 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3419
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003420 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003421 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003422
Hanno Becker0271f962018-08-16 13:23:47 +01003423 /* Clear future message buffering structure. */
3424 ssl_buffering_free( ssl );
3425
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003426 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003427 ssl_set_timer( ssl, 0 );
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3430 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433 }
3434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003437
3438/*
3439 * To be called when the last message of an outgoing flight is send.
3440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003442{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003443 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003444 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3447 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003450 }
3451 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455
Paul Bakker5121ce52009-01-03 21:22:43 +00003456/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003459
3460/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003461 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 *
3463 * - fill in handshake headers
3464 * - update handshake checksum
3465 * - DTLS: save message for resending
3466 * - then pass to the record layer
3467 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003468 * DTLS: except for HelloRequest, messages are only queued, and will only be
3469 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003470 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003471 * Inputs:
3472 * - ssl->out_msglen: 4 + actual handshake message len
3473 * (4 is the size of handshake headers for TLS)
3474 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3475 * - ssl->out_msg + 4: the handshake message body
3476 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003477 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 * - ssl->out_msglen: the length of the record contents
3479 * (including handshake headers but excluding record headers)
3480 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003481 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003482int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003484 int ret;
3485 const size_t hs_len = ssl->out_msglen - 4;
3486 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3489
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003490 /*
3491 * Sanity checks
3492 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003493 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003494 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3495 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003496 /* In SSLv3, the client might send a NoCertificate alert. */
3497#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3498 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3499 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3500 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3501#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3502 {
3503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3505 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003508 /* Whenever we send anything different from a
3509 * HelloRequest we should be in a handshake - double check. */
3510 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3511 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512 ssl->handshake == NULL )
3513 {
3514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003522 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003527
Hanno Beckerb50a2532018-08-06 11:52:54 +01003528 /* Double-check that we did not exceed the bounds
3529 * of the outgoing record buffer.
3530 * This should never fail as the various message
3531 * writing functions must obey the bounds of the
3532 * outgoing record buffer, but better be safe.
3533 *
3534 * Note: We deliberately do not check for the MTU or MFL here.
3535 */
3536 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3539 "size %u, maximum %u",
3540 (unsigned) ssl->out_msglen,
3541 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3543 }
3544
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003545 /*
3546 * Fill handshake headers
3547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003550 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3551 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3552 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003554 /*
3555 * DTLS has additional fields in the Handshake layer,
3556 * between the length field and the actual payload:
3557 * uint16 message_seq;
3558 * uint24 fragment_offset;
3559 * uint24 fragment_length;
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003563 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003564 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003565 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003566 {
3567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3568 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003569 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003570 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3572 }
3573
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003574 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003575 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003576
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003577 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003579 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003580 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3581 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3582 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003583 }
3584 else
3585 {
3586 ssl->out_msg[4] = 0;
3587 ssl->out_msg[5] = 0;
3588 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003589
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003590 /* Handshake hashes are computed without fragmentation,
3591 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003592 memset( ssl->out_msg + 6, 0x00, 3 );
3593 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003596
Hanno Becker0207e532018-08-28 10:28:28 +01003597 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003598 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3599 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 }
3601
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003605 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3606 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 {
3608 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 return( ret );
3612 }
3613 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003614 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003616 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003617 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003618 {
3619 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3620 return( ret );
3621 }
3622 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003623
3624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3625
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003626 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627}
3628
3629/*
3630 * Record layer functions
3631 */
3632
3633/*
3634 * Write current record.
3635 *
3636 * Uses:
3637 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3638 * - ssl->out_msglen: length of the record content (excl headers)
3639 * - ssl->out_msg: record content
3640 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003641int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003642{
3643 int ret, done = 0;
3644 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003645 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003646
3647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003650 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003652 {
3653 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656 return( ret );
3657 }
3658
3659 len = ssl->out_msglen;
3660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3664 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 ret = mbedtls_ssl_hw_record_write( ssl );
3669 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003674
3675 if( ret == 0 )
3676 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003679 if( !done )
3680 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003681 unsigned i;
3682 size_t protected_record_size;
3683
Paul Bakker05ef8352012-05-08 09:17:57 +00003684 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003686 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003687
Hanno Becker19859472018-08-06 09:40:20 +01003688 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003689 ssl->out_len[0] = (unsigned char)( len >> 8 );
3690 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003691
Paul Bakker48916f92012-09-16 19:57:18 +00003692 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003693 {
3694 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003697 return( ret );
3698 }
3699
3700 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003701 ssl->out_len[0] = (unsigned char)( len >> 8 );
3702 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 }
3704
Hanno Becker2b1e3542018-08-06 11:19:13 +01003705 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3706
3707#if defined(MBEDTLS_SSL_PROTO_DTLS)
3708 /* In case of DTLS, double-check that we don't exceed
3709 * the remaining space in the datagram. */
3710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3711 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003712 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003713 if( ret < 0 )
3714 return( ret );
3715
3716 if( protected_record_size > (size_t) ret )
3717 {
3718 /* Should never happen */
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3720 }
3721 }
3722#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003725 "version = [%d:%d], msglen = %d",
3726 ssl->out_hdr[0], ssl->out_hdr[1],
3727 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003730 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003731
3732 ssl->out_left += protected_record_size;
3733 ssl->out_hdr += protected_record_size;
3734 ssl_update_out_pointers( ssl, ssl->transform_out );
3735
Hanno Becker04484622018-08-06 09:49:38 +01003736 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3737 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3738 break;
3739
3740 /* The loop goes to its end iff the counter is wrapping */
3741 if( i == ssl_ep_len( ssl ) )
3742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3744 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3750 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003752 size_t remaining;
3753 ret = ssl_get_remaining_payload_in_datagram( ssl );
3754 if( ret < 0 )
3755 {
3756 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3757 ret );
3758 return( ret );
3759 }
3760
3761 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003763 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003765 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003766 else
3767 {
Hanno Becker513815a2018-08-20 11:56:09 +01003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 }
3770 }
3771#endif /* MBEDTLS_SSL_PROTO_DTLS */
3772
3773 if( ( flush == SSL_FORCE_FLUSH ) &&
3774 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 return( ret );
3778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
3782 return( 0 );
3783}
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003786
3787static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3788{
3789 if( ssl->in_msglen < ssl->in_hslen ||
3790 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3791 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3792 {
3793 return( 1 );
3794 }
3795 return( 0 );
3796}
Hanno Becker44650b72018-08-16 12:51:11 +01003797
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003798static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003799{
3800 return( ( ssl->in_msg[9] << 16 ) |
3801 ( ssl->in_msg[10] << 8 ) |
3802 ssl->in_msg[11] );
3803}
3804
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003805static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003806{
3807 return( ( ssl->in_msg[6] << 16 ) |
3808 ( ssl->in_msg[7] << 8 ) |
3809 ssl->in_msg[8] );
3810}
3811
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003812static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003813{
3814 uint32_t msg_len, frag_off, frag_len;
3815
3816 msg_len = ssl_get_hs_total_len( ssl );
3817 frag_off = ssl_get_hs_frag_off( ssl );
3818 frag_len = ssl_get_hs_frag_len( ssl );
3819
3820 if( frag_off > msg_len )
3821 return( -1 );
3822
3823 if( frag_len > msg_len - frag_off )
3824 return( -1 );
3825
3826 if( frag_len + 12 > ssl->in_msglen )
3827 return( -1 );
3828
3829 return( 0 );
3830}
3831
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003832/*
3833 * Mark bits in bitmask (used for DTLS HS reassembly)
3834 */
3835static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3836{
3837 unsigned int start_bits, end_bits;
3838
3839 start_bits = 8 - ( offset % 8 );
3840 if( start_bits != 8 )
3841 {
3842 size_t first_byte_idx = offset / 8;
3843
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003844 /* Special case */
3845 if( len <= start_bits )
3846 {
3847 for( ; len != 0; len-- )
3848 mask[first_byte_idx] |= 1 << ( start_bits - len );
3849
3850 /* Avoid potential issues with offset or len becoming invalid */
3851 return;
3852 }
3853
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003854 offset += start_bits; /* Now offset % 8 == 0 */
3855 len -= start_bits;
3856
3857 for( ; start_bits != 0; start_bits-- )
3858 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3859 }
3860
3861 end_bits = len % 8;
3862 if( end_bits != 0 )
3863 {
3864 size_t last_byte_idx = ( offset + len ) / 8;
3865
3866 len -= end_bits; /* Now len % 8 == 0 */
3867
3868 for( ; end_bits != 0; end_bits-- )
3869 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3870 }
3871
3872 memset( mask + offset / 8, 0xFF, len / 8 );
3873}
3874
3875/*
3876 * Check that bitmask is full
3877 */
3878static int ssl_bitmask_check( unsigned char *mask, size_t len )
3879{
3880 size_t i;
3881
3882 for( i = 0; i < len / 8; i++ )
3883 if( mask[i] != 0xFF )
3884 return( -1 );
3885
3886 for( i = 0; i < len % 8; i++ )
3887 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3888 return( -1 );
3889
3890 return( 0 );
3891}
3892
Hanno Becker56e205e2018-08-16 09:06:12 +01003893/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003894static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003895 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003896{
Hanno Becker56e205e2018-08-16 09:06:12 +01003897 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003898
Hanno Becker56e205e2018-08-16 09:06:12 +01003899 alloc_len = 12; /* Handshake header */
3900 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003901
Hanno Beckerd07df862018-08-16 09:14:58 +01003902 if( add_bitmap )
3903 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003904
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003905 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003906}
Hanno Becker56e205e2018-08-16 09:06:12 +01003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003909
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003910static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003911{
3912 return( ( ssl->in_msg[1] << 16 ) |
3913 ( ssl->in_msg[2] << 8 ) |
3914 ssl->in_msg[3] );
3915}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003916
Simon Butcher99000142016-10-13 17:21:01 +01003917int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003918{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003922 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003924 }
3925
Hanno Becker12555c62018-08-16 12:47:53 +01003926 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003929 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003930 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003934 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003935 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003936 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003937
Hanno Becker44650b72018-08-16 12:51:11 +01003938 if( ssl_check_hs_header( ssl ) != 0 )
3939 {
3940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3941 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3942 }
3943
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003944 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003945 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3946 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3947 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3948 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003949 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003950 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3953 recv_msg_seq,
3954 ssl->handshake->in_msg_seq ) );
3955 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3956 }
3957
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003958 /* Retransmit only on last message from previous flight, to avoid
3959 * too many retransmissions.
3960 * Besides, No sane server ever retransmits HelloVerifyRequest */
3961 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003965 "message_seq = %d, start_of_flight = %d",
3966 recv_msg_seq,
3967 ssl->handshake->in_flight_start_seq ) );
3968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003972 return( ret );
3973 }
3974 }
3975 else
3976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003978 "message_seq = %d, expected = %d",
3979 recv_msg_seq,
3980 ssl->handshake->in_msg_seq ) );
3981 }
3982
Hanno Becker90333da2017-10-10 11:27:13 +01003983 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003984 }
3985 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003986
Hanno Becker6d97ef52018-08-16 13:09:04 +01003987 /* Message reassembly is handled alongside buffering of future
3988 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003989 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003990 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003994 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003995 }
3996 }
3997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003999 /* With TLS we don't handle fragmentation (for now) */
4000 if( ssl->in_msglen < ssl->in_hslen )
4001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4003 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004 }
4005
Simon Butcher99000142016-10-13 17:21:01 +01004006 return( 0 );
4007}
4008
4009void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4010{
Hanno Becker0271f962018-08-16 13:23:47 +01004011 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004012
Hanno Becker0271f962018-08-16 13:23:47 +01004013 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004014 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004015 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004016 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004017
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004018 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004021 ssl->handshake != NULL )
4022 {
Hanno Becker0271f962018-08-16 13:23:47 +01004023 unsigned offset;
4024 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004025
Hanno Becker0271f962018-08-16 13:23:47 +01004026 /* Increment handshake sequence number */
4027 hs->in_msg_seq++;
4028
4029 /*
4030 * Clear up handshake buffering and reassembly structure.
4031 */
4032
4033 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004034 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004035
4036 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004037 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4038 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004039 offset++, hs_buf++ )
4040 {
4041 *hs_buf = *(hs_buf + 1);
4042 }
4043
4044 /* Create a fresh last entry */
4045 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004046 }
4047#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004048}
4049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004050/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004051 * DTLS anti-replay: RFC 6347 4.1.2.6
4052 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004053 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4054 * Bit n is set iff record number in_window_top - n has been seen.
4055 *
4056 * Usually, in_window_top is the last record number seen and the lsb of
4057 * in_window is set. The only exception is the initial state (record number 0
4058 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4061static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004062{
4063 ssl->in_window_top = 0;
4064 ssl->in_window = 0;
4065}
4066
4067static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4068{
4069 return( ( (uint64_t) buf[0] << 40 ) |
4070 ( (uint64_t) buf[1] << 32 ) |
4071 ( (uint64_t) buf[2] << 24 ) |
4072 ( (uint64_t) buf[3] << 16 ) |
4073 ( (uint64_t) buf[4] << 8 ) |
4074 ( (uint64_t) buf[5] ) );
4075}
4076
4077/*
4078 * Return 0 if sequence number is acceptable, -1 otherwise
4079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004081{
4082 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4083 uint64_t bit;
4084
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004085 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004086 return( 0 );
4087
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004088 if( rec_seqnum > ssl->in_window_top )
4089 return( 0 );
4090
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004091 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004092
4093 if( bit >= 64 )
4094 return( -1 );
4095
4096 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4097 return( -1 );
4098
4099 return( 0 );
4100}
4101
4102/*
4103 * Update replay window on new validated record
4104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004106{
4107 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4108
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004109 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004110 return;
4111
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 if( rec_seqnum > ssl->in_window_top )
4113 {
4114 /* Update window_top and the contents of the window */
4115 uint64_t shift = rec_seqnum - ssl->in_window_top;
4116
4117 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004118 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004120 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004121 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004122 ssl->in_window |= 1;
4123 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004124
4125 ssl->in_window_top = rec_seqnum;
4126 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004127 else
4128 {
4129 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004130 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131
4132 if( bit < 64 ) /* Always true, but be extra sure */
4133 ssl->in_window |= (uint64_t) 1 << bit;
4134 }
4135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004137
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004138#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004139/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004140static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4141
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004142/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004143 * Without any SSL context, check if a datagram looks like a ClientHello with
4144 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004145 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004146 *
4147 * - if cookie is valid, return 0
4148 * - if ClientHello looks superficially valid but cookie is not,
4149 * fill obuf and set olen, then
4150 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4151 * - otherwise return a specific error code
4152 */
4153static int ssl_check_dtls_clihlo_cookie(
4154 mbedtls_ssl_cookie_write_t *f_cookie_write,
4155 mbedtls_ssl_cookie_check_t *f_cookie_check,
4156 void *p_cookie,
4157 const unsigned char *cli_id, size_t cli_id_len,
4158 const unsigned char *in, size_t in_len,
4159 unsigned char *obuf, size_t buf_len, size_t *olen )
4160{
4161 size_t sid_len, cookie_len;
4162 unsigned char *p;
4163
4164 if( f_cookie_write == NULL || f_cookie_check == NULL )
4165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 /*
4168 * Structure of ClientHello with record and handshake headers,
4169 * and expected values. We don't need to check a lot, more checks will be
4170 * done when actually parsing the ClientHello - skipping those checks
4171 * avoids code duplication and does not make cookie forging any easier.
4172 *
4173 * 0-0 ContentType type; copied, must be handshake
4174 * 1-2 ProtocolVersion version; copied
4175 * 3-4 uint16 epoch; copied, must be 0
4176 * 5-10 uint48 sequence_number; copied
4177 * 11-12 uint16 length; (ignored)
4178 *
4179 * 13-13 HandshakeType msg_type; (ignored)
4180 * 14-16 uint24 length; (ignored)
4181 * 17-18 uint16 message_seq; copied
4182 * 19-21 uint24 fragment_offset; copied, must be 0
4183 * 22-24 uint24 fragment_length; (ignored)
4184 *
4185 * 25-26 ProtocolVersion client_version; (ignored)
4186 * 27-58 Random random; (ignored)
4187 * 59-xx SessionID session_id; 1 byte len + sid_len content
4188 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4189 * ...
4190 *
4191 * Minimum length is 61 bytes.
4192 */
4193 if( in_len < 61 ||
4194 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4195 in[3] != 0 || in[4] != 0 ||
4196 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4197 {
4198 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4199 }
4200
4201 sid_len = in[59];
4202 if( sid_len > in_len - 61 )
4203 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4204
4205 cookie_len = in[60 + sid_len];
4206 if( cookie_len > in_len - 60 )
4207 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4208
4209 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4210 cli_id, cli_id_len ) == 0 )
4211 {
4212 /* Valid cookie */
4213 return( 0 );
4214 }
4215
4216 /*
4217 * If we get here, we've got an invalid cookie, let's prepare HVR.
4218 *
4219 * 0-0 ContentType type; copied
4220 * 1-2 ProtocolVersion version; copied
4221 * 3-4 uint16 epoch; copied
4222 * 5-10 uint48 sequence_number; copied
4223 * 11-12 uint16 length; olen - 13
4224 *
4225 * 13-13 HandshakeType msg_type; hello_verify_request
4226 * 14-16 uint24 length; olen - 25
4227 * 17-18 uint16 message_seq; copied
4228 * 19-21 uint24 fragment_offset; copied
4229 * 22-24 uint24 fragment_length; olen - 25
4230 *
4231 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4232 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4233 *
4234 * Minimum length is 28.
4235 */
4236 if( buf_len < 28 )
4237 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4238
4239 /* Copy most fields and adapt others */
4240 memcpy( obuf, in, 25 );
4241 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4242 obuf[25] = 0xfe;
4243 obuf[26] = 0xff;
4244
4245 /* Generate and write actual cookie */
4246 p = obuf + 28;
4247 if( f_cookie_write( p_cookie,
4248 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4249 {
4250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4251 }
4252
4253 *olen = p - obuf;
4254
4255 /* Go back and fill length fields */
4256 obuf[27] = (unsigned char)( *olen - 28 );
4257
4258 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4259 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4260 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4261
4262 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4263 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4264
4265 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4266}
4267
4268/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004269 * Handle possible client reconnect with the same UDP quadruplet
4270 * (RFC 6347 Section 4.2.8).
4271 *
4272 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4273 * that looks like a ClientHello.
4274 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004275 * - if the input looks like a ClientHello without cookies,
4276 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004277 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004278 * - if the input looks like a ClientHello with a valid cookie,
4279 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004280 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004281 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004282 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004283 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004284 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4285 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004286 */
4287static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4288{
4289 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004290 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004291
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004292 ret = ssl_check_dtls_clihlo_cookie(
4293 ssl->conf->f_cookie_write,
4294 ssl->conf->f_cookie_check,
4295 ssl->conf->p_cookie,
4296 ssl->cli_id, ssl->cli_id_len,
4297 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004298 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004299
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004300 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4301
4302 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004303 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004304 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004305 * If the error is permanent we'll catch it later,
4306 * if it's not, then hopefully it'll work next time. */
4307 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4308
4309 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004310 }
4311
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004312 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004313 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004314 /* Got a valid cookie, partially reset context */
4315 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4316 {
4317 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4318 return( ret );
4319 }
4320
4321 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004322 }
4323
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004324 return( ret );
4325}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004326#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004327
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004329 * ContentType type;
4330 * ProtocolVersion version;
4331 * uint16 epoch; // DTLS only
4332 * uint48 sequence_number; // DTLS only
4333 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004334 *
4335 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004336 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004337 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4338 *
4339 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004340 * 1. proceed with the record if this function returns 0
4341 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4342 * 3. return CLIENT_RECONNECT if this function return that value
4343 * 4. drop the whole datagram if this function returns anything else.
4344 * Point 2 is needed when the peer is resending, and we have already received
4345 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004349 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 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 +02004352
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004354 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004355 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004359 ssl->in_msgtype,
4360 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004362 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4364 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4365 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4366 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004369
4370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004371 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4372 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004373 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4374#endif /* MBEDTLS_SSL_PROTO_DTLS */
4375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4376 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004379 }
4380
4381 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004382 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 }
4387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004388 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
4393
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004394 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004395 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004396 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4399 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004400 }
4401
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004402 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004403 * DTLS-related tests.
4404 * Check epoch before checking length constraint because
4405 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4406 * message gets duplicated before the corresponding Finished message,
4407 * the second ChangeCipherSpec should be discarded because it belongs
4408 * to an old epoch, but not because its length is shorter than
4409 * the minimum record length for packets using the new record transform.
4410 * Note that these two kinds of failures are handled differently,
4411 * as an unexpected record is silently skipped but an invalid
4412 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004413 */
4414#if defined(MBEDTLS_SSL_PROTO_DTLS)
4415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4416 {
4417 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4418
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004419 /* Check epoch (and sequence number) with DTLS */
4420 if( rec_epoch != ssl->in_epoch )
4421 {
4422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4423 "expected %d, received %d",
4424 ssl->in_epoch, rec_epoch ) );
4425
4426#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4427 /*
4428 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4429 * access the first byte of record content (handshake type), as we
4430 * have an active transform (possibly iv_len != 0), so use the
4431 * fact that the record header len is 13 instead.
4432 */
4433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4434 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4435 rec_epoch == 0 &&
4436 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4437 ssl->in_left > 13 &&
4438 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4441 "from the same port" ) );
4442 return( ssl_handle_possible_reconnect( ssl ) );
4443 }
4444 else
4445#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004446 {
4447 /* Consider buffering the record. */
4448 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4451 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4452 }
4453
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004454 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004455 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004456 }
4457
4458#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4459 /* Replay detection only works for the current epoch */
4460 if( rec_epoch == ssl->in_epoch &&
4461 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4464 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4465 }
4466#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004467
Hanno Becker52c6dc62017-05-26 16:07:36 +01004468 /* Drop unexpected ApplicationData records,
4469 * except at the beginning of renegotiations */
4470 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4471 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4472#if defined(MBEDTLS_SSL_RENEGOTIATION)
4473 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4474 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4475#endif
4476 )
4477 {
4478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4479 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4480 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004481 }
4482#endif /* MBEDTLS_SSL_PROTO_DTLS */
4483
Hanno Becker52c6dc62017-05-26 16:07:36 +01004484
4485 /* Check length against bounds of the current transform and version */
4486 if( ssl->transform_in == NULL )
4487 {
4488 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004489 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4492 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4493 }
4494 }
4495 else
4496 {
4497 if( ssl->in_msglen < ssl->transform_in->minlen )
4498 {
4499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4501 }
4502
4503#if defined(MBEDTLS_SSL_PROTO_SSL3)
4504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004505 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004506 {
4507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4509 }
4510#endif
4511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4512 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4513 /*
4514 * TLS encrypted messages can have up to 256 bytes of padding
4515 */
4516 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4517 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004518 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004519 {
4520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4522 }
4523#endif
4524 }
4525
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004526 return( 0 );
4527}
Paul Bakker5121ce52009-01-03 21:22:43 +00004528
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004529/*
4530 * If applicable, decrypt (and decompress) record content
4531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533{
4534 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4537 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4540 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 ret = mbedtls_ssl_hw_record_read( ssl );
4545 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004549 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004550
4551 if( ret == 0 )
4552 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004555 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 {
4557 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 return( ret );
4561 }
4562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 ssl->in_msg, ssl->in_msglen );
4565
Angus Grattond8213d02016-05-25 20:56:48 +10004566 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571 }
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004576 {
4577 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004580 return( ret );
4581 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004589 }
4590#endif
4591
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 return( 0 );
4593}
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004596
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004597/*
4598 * Read a record.
4599 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004600 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4601 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4602 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004603 */
Hanno Becker1097b342018-08-15 14:09:41 +01004604
4605/* Helper functions for mbedtls_ssl_read_record(). */
4606static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004607static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4608static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004609
Hanno Becker327c93b2018-08-15 13:56:18 +01004610int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004611 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004612{
4613 int ret;
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004616
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004617 if( ssl->keep_current_message == 0 )
4618 {
4619 do {
Simon Butcher99000142016-10-13 17:21:01 +01004620
Hanno Becker26994592018-08-15 14:14:59 +01004621 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004622 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004623 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004624
Hanno Beckere74d5562018-08-15 14:26:08 +01004625 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004626 {
Hanno Becker40f50842018-08-15 14:48:01 +01004627#if defined(MBEDTLS_SSL_PROTO_DTLS)
4628 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004629
Hanno Becker40f50842018-08-15 14:48:01 +01004630 /* We only check for buffered messages if the
4631 * current datagram is fully consumed. */
4632 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004633 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004634 {
Hanno Becker40f50842018-08-15 14:48:01 +01004635 if( ssl_load_buffered_message( ssl ) == 0 )
4636 have_buffered = 1;
4637 }
4638
4639 if( have_buffered == 0 )
4640#endif /* MBEDTLS_SSL_PROTO_DTLS */
4641 {
4642 ret = ssl_get_next_record( ssl );
4643 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4644 continue;
4645
4646 if( ret != 0 )
4647 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004648 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004649 return( ret );
4650 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004651 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004652 }
4653
4654 ret = mbedtls_ssl_handle_message_type( ssl );
4655
Hanno Becker40f50842018-08-15 14:48:01 +01004656#if defined(MBEDTLS_SSL_PROTO_DTLS)
4657 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4658 {
4659 /* Buffer future message */
4660 ret = ssl_buffer_message( ssl );
4661 if( ret != 0 )
4662 return( ret );
4663
4664 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4665 }
4666#endif /* MBEDTLS_SSL_PROTO_DTLS */
4667
Hanno Becker90333da2017-10-10 11:27:13 +01004668 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4669 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004670
4671 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004672 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004673 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004674 return( ret );
4675 }
4676
Hanno Becker327c93b2018-08-15 13:56:18 +01004677 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004678 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004679 {
4680 mbedtls_ssl_update_handshake_status( ssl );
4681 }
Simon Butcher99000142016-10-13 17:21:01 +01004682 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004683 else
Simon Butcher99000142016-10-13 17:21:01 +01004684 {
Hanno Becker02f59072018-08-15 14:00:24 +01004685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004686 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004687 }
4688
4689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4690
4691 return( 0 );
4692}
4693
Hanno Becker40f50842018-08-15 14:48:01 +01004694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004695static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004696{
Hanno Becker40f50842018-08-15 14:48:01 +01004697 if( ssl->in_left > ssl->next_record_offset )
4698 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004699
Hanno Becker40f50842018-08-15 14:48:01 +01004700 return( 0 );
4701}
4702
4703static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4704{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004705 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004706 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004707 int ret = 0;
4708
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004709 if( hs == NULL )
4710 return( -1 );
4711
Hanno Beckere00ae372018-08-20 09:39:42 +01004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4713
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004714 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4715 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4716 {
4717 /* Check if we have seen a ChangeCipherSpec before.
4718 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004719 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4722 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004723 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004724 }
4725
Hanno Becker39b8bc92018-08-28 17:17:13 +01004726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4728 ssl->in_msglen = 1;
4729 ssl->in_msg[0] = 1;
4730
4731 /* As long as they are equal, the exact value doesn't matter. */
4732 ssl->in_left = 0;
4733 ssl->next_record_offset = 0;
4734
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004735 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004736 goto exit;
4737 }
Hanno Becker37f95322018-08-16 13:55:32 +01004738
Hanno Beckerb8f50142018-08-28 10:01:34 +01004739#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004740 /* Debug only */
4741 {
4742 unsigned offset;
4743 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4744 {
4745 hs_buf = &hs->buffering.hs[offset];
4746 if( hs_buf->is_valid == 1 )
4747 {
4748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4749 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004750 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004751 }
4752 }
4753 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004754#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004755
4756 /* Check if we have buffered and/or fully reassembled the
4757 * next handshake message. */
4758 hs_buf = &hs->buffering.hs[0];
4759 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4760 {
4761 /* Synthesize a record containing the buffered HS message. */
4762 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4763 ( hs_buf->data[2] << 8 ) |
4764 hs_buf->data[3];
4765
4766 /* Double-check that we haven't accidentally buffered
4767 * a message that doesn't fit into the input buffer. */
4768 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4772 }
4773
4774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4775 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4776 hs_buf->data, msg_len + 12 );
4777
4778 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4779 ssl->in_hslen = msg_len + 12;
4780 ssl->in_msglen = msg_len + 12;
4781 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4782
4783 ret = 0;
4784 goto exit;
4785 }
4786 else
4787 {
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4789 hs->in_msg_seq ) );
4790 }
4791
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004792 ret = -1;
4793
4794exit:
4795
4796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4797 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004798}
4799
Hanno Beckera02b0b42018-08-21 17:20:27 +01004800static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4801 size_t desired )
4802{
4803 int offset;
4804 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4806 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004807
Hanno Becker01315ea2018-08-21 17:22:17 +01004808 /* Get rid of future records epoch first, if such exist. */
4809 ssl_free_buffered_record( ssl );
4810
4811 /* Check if we have enough space available now. */
4812 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4813 hs->buffering.total_bytes_buffered ) )
4814 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004816 return( 0 );
4817 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004818
Hanno Becker4f432ad2018-08-28 10:02:32 +01004819 /* We don't have enough space to buffer the next expected handshake
4820 * message. Remove buffers used for future messages to gain space,
4821 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004822 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4823 offset >= 0; offset-- )
4824 {
4825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4826 offset ) );
4827
Hanno Beckerb309b922018-08-23 13:18:05 +01004828 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004829
4830 /* Check if we have enough space available now. */
4831 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4832 hs->buffering.total_bytes_buffered ) )
4833 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004835 return( 0 );
4836 }
4837 }
4838
4839 return( -1 );
4840}
4841
Hanno Becker40f50842018-08-15 14:48:01 +01004842static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4843{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004844 int ret = 0;
4845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4846
4847 if( hs == NULL )
4848 return( 0 );
4849
4850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4851
4852 switch( ssl->in_msgtype )
4853 {
4854 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004856
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004857 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004858 break;
4859
4860 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004861 {
4862 unsigned recv_msg_seq_offset;
4863 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4864 mbedtls_ssl_hs_buffer *hs_buf;
4865 size_t msg_len = ssl->in_hslen - 12;
4866
4867 /* We should never receive an old handshake
4868 * message - double-check nonetheless. */
4869 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4873 }
4874
4875 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4876 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4877 {
4878 /* Silently ignore -- message too far in the future */
4879 MBEDTLS_SSL_DEBUG_MSG( 2,
4880 ( "Ignore future HS message with sequence number %u, "
4881 "buffering window %u - %u",
4882 recv_msg_seq, ssl->handshake->in_msg_seq,
4883 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4884
4885 goto exit;
4886 }
4887
4888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4889 recv_msg_seq, recv_msg_seq_offset ) );
4890
4891 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4892
4893 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004894 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004895 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004896 size_t reassembly_buf_sz;
4897
Hanno Becker37f95322018-08-16 13:55:32 +01004898 hs_buf->is_fragmented =
4899 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4900
4901 /* We copy the message back into the input buffer
4902 * after reassembly, so check that it's not too large.
4903 * This is an implementation-specific limitation
4904 * and not one from the standard, hence it is not
4905 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004907 {
4908 /* Ignore message */
4909 goto exit;
4910 }
4911
Hanno Beckere0b150f2018-08-21 15:51:03 +01004912 /* Check if we have enough space to buffer the message. */
4913 if( hs->buffering.total_bytes_buffered >
4914 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4915 {
4916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4918 }
4919
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004920 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4921 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004922
4923 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4924 hs->buffering.total_bytes_buffered ) )
4925 {
4926 if( recv_msg_seq_offset > 0 )
4927 {
4928 /* If we can't buffer a future message because
4929 * of space limitations -- ignore. */
4930 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",
4931 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4932 (unsigned) hs->buffering.total_bytes_buffered ) );
4933 goto exit;
4934 }
Hanno Beckere1801392018-08-21 16:51:05 +01004935 else
4936 {
4937 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",
4938 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4939 (unsigned) hs->buffering.total_bytes_buffered ) );
4940 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004941
Hanno Beckera02b0b42018-08-21 17:20:27 +01004942 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004943 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004944 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",
4945 (unsigned) msg_len,
4946 (unsigned) reassembly_buf_sz,
4947 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004948 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004949 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4950 goto exit;
4951 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004952 }
4953
4954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4955 msg_len ) );
4956
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004957 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4958 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004959 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004960 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004961 goto exit;
4962 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004963 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004964
4965 /* Prepare final header: copy msg_type, length and message_seq,
4966 * then add standardised fragment_offset and fragment_length */
4967 memcpy( hs_buf->data, ssl->in_msg, 6 );
4968 memset( hs_buf->data + 6, 0, 3 );
4969 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4970
4971 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004972
4973 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004974 }
4975 else
4976 {
4977 /* Make sure msg_type and length are consistent */
4978 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4981 /* Ignore */
4982 goto exit;
4983 }
4984 }
4985
Hanno Becker4422bbb2018-08-20 09:40:19 +01004986 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004987 {
4988 size_t frag_len, frag_off;
4989 unsigned char * const msg = hs_buf->data + 12;
4990
4991 /*
4992 * Check and copy current fragment
4993 */
4994
4995 /* Validation of header fields already done in
4996 * mbedtls_ssl_prepare_handshake_record(). */
4997 frag_off = ssl_get_hs_frag_off( ssl );
4998 frag_len = ssl_get_hs_frag_len( ssl );
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5001 frag_off, frag_len ) );
5002 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5003
5004 if( hs_buf->is_fragmented )
5005 {
5006 unsigned char * const bitmask = msg + msg_len;
5007 ssl_bitmask_set( bitmask, frag_off, frag_len );
5008 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5009 msg_len ) == 0 );
5010 }
5011 else
5012 {
5013 hs_buf->is_complete = 1;
5014 }
5015
5016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5017 hs_buf->is_complete ? "" : "not yet " ) );
5018 }
5019
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005020 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005021 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005022
5023 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005024 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005025 break;
5026 }
5027
5028exit:
5029
5030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5031 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005032}
5033#endif /* MBEDTLS_SSL_PROTO_DTLS */
5034
Hanno Becker1097b342018-08-15 14:09:41 +01005035static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005036{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005037 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005038 * Consume last content-layer message and potentially
5039 * update in_msglen which keeps track of the contents'
5040 * consumption state.
5041 *
5042 * (1) Handshake messages:
5043 * Remove last handshake message, move content
5044 * and adapt in_msglen.
5045 *
5046 * (2) Alert messages:
5047 * Consume whole record content, in_msglen = 0.
5048 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005049 * (3) Change cipher spec:
5050 * Consume whole record content, in_msglen = 0.
5051 *
5052 * (4) Application data:
5053 * Don't do anything - the record layer provides
5054 * the application data as a stream transport
5055 * and consumes through mbedtls_ssl_read only.
5056 *
5057 */
5058
5059 /* Case (1): Handshake messages */
5060 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005062 /* Hard assertion to be sure that no application data
5063 * is in flight, as corrupting ssl->in_msglen during
5064 * ssl->in_offt != NULL is fatal. */
5065 if( ssl->in_offt != NULL )
5066 {
5067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5069 }
5070
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005071 /*
5072 * Get next Handshake message in the current record
5073 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005074
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005076 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005077 * current handshake content: If DTLS handshake
5078 * fragmentation is used, that's the fragment
5079 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005080 * size here is faulty and should be changed at
5081 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005082 * (2) While it doesn't seem to cause problems, one
5083 * has to be very careful not to assume that in_hslen
5084 * is always <= in_msglen in a sensible communication.
5085 * Again, it's wrong for DTLS handshake fragmentation.
5086 * The following check is therefore mandatory, and
5087 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005088 * Additionally, ssl->in_hslen might be arbitrarily out of
5089 * bounds after handling a DTLS message with an unexpected
5090 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 */
5092 if( ssl->in_hslen < ssl->in_msglen )
5093 {
5094 ssl->in_msglen -= ssl->in_hslen;
5095 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5096 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005097
Hanno Becker4a810fb2017-05-24 16:27:30 +01005098 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5099 ssl->in_msg, ssl->in_msglen );
5100 }
5101 else
5102 {
5103 ssl->in_msglen = 0;
5104 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005105
Hanno Becker4a810fb2017-05-24 16:27:30 +01005106 ssl->in_hslen = 0;
5107 }
5108 /* Case (4): Application data */
5109 else if( ssl->in_offt != NULL )
5110 {
5111 return( 0 );
5112 }
5113 /* Everything else (CCS & Alerts) */
5114 else
5115 {
5116 ssl->in_msglen = 0;
5117 }
5118
Hanno Becker1097b342018-08-15 14:09:41 +01005119 return( 0 );
5120}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005121
Hanno Beckere74d5562018-08-15 14:26:08 +01005122static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5123{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005124 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005125 return( 1 );
5126
5127 return( 0 );
5128}
5129
Hanno Becker5f066e72018-08-16 14:56:31 +01005130#if defined(MBEDTLS_SSL_PROTO_DTLS)
5131
5132static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5133{
5134 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5135 if( hs == NULL )
5136 return;
5137
Hanno Becker01315ea2018-08-21 17:22:17 +01005138 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005139 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005140 hs->buffering.total_bytes_buffered -=
5141 hs->buffering.future_record.len;
5142
5143 mbedtls_free( hs->buffering.future_record.data );
5144 hs->buffering.future_record.data = NULL;
5145 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005146}
5147
5148static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5149{
5150 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5151 unsigned char * rec;
5152 size_t rec_len;
5153 unsigned rec_epoch;
5154
5155 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5156 return( 0 );
5157
5158 if( hs == NULL )
5159 return( 0 );
5160
Hanno Becker5f066e72018-08-16 14:56:31 +01005161 rec = hs->buffering.future_record.data;
5162 rec_len = hs->buffering.future_record.len;
5163 rec_epoch = hs->buffering.future_record.epoch;
5164
5165 if( rec == NULL )
5166 return( 0 );
5167
Hanno Becker4cb782d2018-08-20 11:19:05 +01005168 /* Only consider loading future records if the
5169 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005170 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005171 return( 0 );
5172
Hanno Becker5f066e72018-08-16 14:56:31 +01005173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5174
5175 if( rec_epoch != ssl->in_epoch )
5176 {
5177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5178 goto exit;
5179 }
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5182
5183 /* Double-check that the record is not too large */
5184 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5185 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5186 {
5187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5189 }
5190
5191 memcpy( ssl->in_hdr, rec, rec_len );
5192 ssl->in_left = rec_len;
5193 ssl->next_record_offset = 0;
5194
5195 ssl_free_buffered_record( ssl );
5196
5197exit:
5198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5199 return( 0 );
5200}
5201
5202static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5203{
5204 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5205 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005207
5208 /* Don't buffer future records outside handshakes. */
5209 if( hs == NULL )
5210 return( 0 );
5211
5212 /* Only buffer handshake records (we are only interested
5213 * in Finished messages). */
5214 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5215 return( 0 );
5216
5217 /* Don't buffer more than one future epoch record. */
5218 if( hs->buffering.future_record.data != NULL )
5219 return( 0 );
5220
Hanno Becker01315ea2018-08-21 17:22:17 +01005221 /* Don't buffer record if there's not enough buffering space remaining. */
5222 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5223 hs->buffering.total_bytes_buffered ) )
5224 {
5225 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",
5226 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5227 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005228 return( 0 );
5229 }
5230
Hanno Becker5f066e72018-08-16 14:56:31 +01005231 /* Buffer record */
5232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5233 ssl->in_epoch + 1 ) );
5234 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5235 rec_hdr_len + ssl->in_msglen );
5236
5237 /* ssl_parse_record_header() only considers records
5238 * of the next epoch as candidates for buffering. */
5239 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005240 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005241
5242 hs->buffering.future_record.data =
5243 mbedtls_calloc( 1, hs->buffering.future_record.len );
5244 if( hs->buffering.future_record.data == NULL )
5245 {
5246 /* If we run out of RAM trying to buffer a
5247 * record from the next epoch, just ignore. */
5248 return( 0 );
5249 }
5250
Hanno Becker01315ea2018-08-21 17:22:17 +01005251 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005252
Hanno Becker01315ea2018-08-21 17:22:17 +01005253 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005254 return( 0 );
5255}
5256
5257#endif /* MBEDTLS_SSL_PROTO_DTLS */
5258
Hanno Beckere74d5562018-08-15 14:26:08 +01005259static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005260{
5261 int ret;
5262
Hanno Becker5f066e72018-08-16 14:56:31 +01005263#if defined(MBEDTLS_SSL_PROTO_DTLS)
5264 /* We might have buffered a future record; if so,
5265 * and if the epoch matches now, load it.
5266 * On success, this call will set ssl->in_left to
5267 * the length of the buffered record, so that
5268 * the calls to ssl_fetch_input() below will
5269 * essentially be no-ops. */
5270 ret = ssl_load_buffered_record( ssl );
5271 if( ret != 0 )
5272 return( ret );
5273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005278 return( ret );
5279 }
5280
5281 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5285 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005286 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005287 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5288 {
5289 ret = ssl_buffer_future_record( ssl );
5290 if( ret != 0 )
5291 return( ret );
5292
5293 /* Fall through to handling of unexpected records */
5294 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5295 }
5296
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005297 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5298 {
5299 /* Skip unexpected record (but not whole datagram) */
5300 ssl->next_record_offset = ssl->in_msglen
5301 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005302
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5304 "(header)" ) );
5305 }
5306 else
5307 {
5308 /* Skip invalid record and the rest of the datagram */
5309 ssl->next_record_offset = 0;
5310 ssl->in_left = 0;
5311
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5313 "(header)" ) );
5314 }
5315
5316 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005317 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005318 }
5319#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005321 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005322
5323 /*
5324 * Read and optionally decrypt the message contents
5325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5327 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005330 return( ret );
5331 }
5332
5333 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005338 if( ssl->next_record_offset < ssl->in_left )
5339 {
5340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5341 }
5342 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005343 else
5344#endif
5345 ssl->in_left = 0;
5346
5347 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005351 {
5352 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5354 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005355 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005356 /* Except when waiting for Finished as a bad mac here
5357 * probably means something went wrong in the handshake
5358 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5359 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5360 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5361 {
5362#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5363 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5364 {
5365 mbedtls_ssl_send_alert_message( ssl,
5366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5367 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5368 }
5369#endif
5370 return( ret );
5371 }
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005374 if( ssl->conf->badmac_limit != 0 &&
5375 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5378 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005379 }
5380#endif
5381
Hanno Becker4a810fb2017-05-24 16:27:30 +01005382 /* As above, invalid records cause
5383 * dismissal of the whole datagram. */
5384
5385 ssl->next_record_offset = 0;
5386 ssl->in_left = 0;
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005389 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005390 }
5391
5392 return( ret );
5393 }
5394 else
5395#endif
5396 {
5397 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 mbedtls_ssl_send_alert_message( ssl,
5402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5403 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005404 }
5405#endif
5406 return( ret );
5407 }
5408 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005409
Simon Butcher99000142016-10-13 17:21:01 +01005410 return( 0 );
5411}
5412
5413int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5414{
5415 int ret;
5416
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005417 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005418 * Handle particular types of records
5419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Simon Butcher99000142016-10-13 17:21:01 +01005422 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5423 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005424 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Hanno Beckere678eaa2018-08-21 14:57:46 +01005428 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005429 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005430 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005431 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5433 ssl->in_msglen ) );
5434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005435 }
5436
Hanno Beckere678eaa2018-08-21 14:57:46 +01005437 if( ssl->in_msg[0] != 1 )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5440 ssl->in_msg[0] ) );
5441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5442 }
5443
5444#if defined(MBEDTLS_SSL_PROTO_DTLS)
5445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5446 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5447 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5448 {
5449 if( ssl->handshake == NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5452 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5453 }
5454
5455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5456 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5457 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005458#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005459 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005463 if( ssl->in_msglen != 2 )
5464 {
5465 /* Note: Standard allows for more than one 2 byte alert
5466 to be packed in a single message, but Mbed TLS doesn't
5467 currently support this. */
5468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5469 ssl->in_msglen ) );
5470 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5471 }
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 ssl->in_msg[0], ssl->in_msg[1] ) );
5475
5476 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005477 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005482 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 }
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5487 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5490 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005492
5493#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5494 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5495 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5496 {
Hanno Becker90333da2017-10-10 11:27:13 +01005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005498 /* Will be handled when trying to parse ServerHello */
5499 return( 0 );
5500 }
5501#endif
5502
5503#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5505 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5506 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5507 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5510 /* Will be handled in mbedtls_ssl_parse_certificate() */
5511 return( 0 );
5512 }
5513#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5514
5515 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005516 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 }
5518
Hanno Beckerc76c6192017-06-06 10:03:17 +01005519#if defined(MBEDTLS_SSL_PROTO_DTLS)
5520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5521 ssl->handshake != NULL &&
5522 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5523 {
5524 ssl_handshake_wrapup_free_hs_transform( ssl );
5525 }
5526#endif
5527
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 return( 0 );
5529}
5530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005532{
5533 int ret;
5534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5537 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005538 {
5539 return( ret );
5540 }
5541
5542 return( 0 );
5543}
5544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005546 unsigned char level,
5547 unsigned char message )
5548{
5549 int ret;
5550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005551 if( ssl == NULL || ssl->conf == NULL )
5552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005558 ssl->out_msglen = 2;
5559 ssl->out_msg[0] = level;
5560 ssl->out_msg[1] = message;
5561
Hanno Becker67bc7c32018-08-06 11:33:50 +01005562 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005565 return( ret );
5566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005568
5569 return( 0 );
5570}
5571
Paul Bakker5121ce52009-01-03 21:22:43 +00005572/*
5573 * Handshake functions
5574 */
Hanno Becker21489932019-02-05 13:20:55 +00005575#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005576/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Hanno Becker7177a882019-02-05 13:36:46 +00005583 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005586 ssl->state++;
5587 return( 0 );
5588 }
5589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005592}
5593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599
Hanno Becker7177a882019-02-05 13:36:46 +00005600 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005603 ssl->state++;
5604 return( 0 );
5605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005609}
Gilles Peskinef9828522017-05-03 12:28:43 +02005610
Hanno Becker21489932019-02-05 13:20:55 +00005611#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005612/* Some certificate support -> implement write and parse */
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 const mbedtls_x509_crt *crt;
5619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005622
Hanno Becker7177a882019-02-05 13:36:46 +00005623 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005626 ssl->state++;
5627 return( 0 );
5628 }
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
5633 if( ssl->client_auth == 0 )
5634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 ssl->state++;
5637 return( 0 );
5638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 /*
5642 * If using SSLv3 and got no cert, send an Alert message
5643 * (otherwise an empty Certificate message will be sent).
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5646 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
5648 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5650 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5651 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 goto write_msg;
5655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#endif /* MBEDTLS_SSL_CLI_C */
5659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5665 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 }
5667 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * 0 . 0 handshake type
5674 * 1 . 3 handshake length
5675 * 4 . 6 length of all certs
5676 * 7 . 9 length of cert. 1
5677 * 10 . n-1 peer certificate
5678 * n . n+2 length of cert. 2
5679 * n+3 . ... upper level cert, etc.
5680 */
5681 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
Paul Bakker29087132010-03-21 21:03:34 +00005684 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
5686 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005687 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005690 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 }
5693
5694 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5695 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5696 ssl->out_msg[i + 2] = (unsigned char)( n );
5697
5698 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5699 i += n; crt = crt->next;
5700 }
5701
5702 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5703 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5704 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5705
5706 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5708 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005710#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005711write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 ssl->state++;
5715
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005716 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 return( ret );
5720 }
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Paul Bakkered27a042013-04-18 22:46:23 +02005724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725}
5726
Hanno Becker84879e32019-01-31 07:44:03 +00005727#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005728
5729#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005730static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5731 unsigned char *crt_buf,
5732 size_t crt_buf_len )
5733{
5734 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5735
5736 if( peer_crt == NULL )
5737 return( -1 );
5738
5739 if( peer_crt->raw.len != crt_buf_len )
5740 return( -1 );
5741
Hanno Becker46f34d02019-02-08 14:00:04 +00005742 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743}
Hanno Becker177475a2019-02-05 17:02:46 +00005744#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5745static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5746 unsigned char *crt_buf,
5747 size_t crt_buf_len )
5748{
5749 int ret;
5750 unsigned char const * const peer_cert_digest =
5751 ssl->session->peer_cert_digest;
5752 mbedtls_md_type_t const peer_cert_digest_type =
5753 ssl->session->peer_cert_digest_type;
5754 mbedtls_md_info_t const * const digest_info =
5755 mbedtls_md_info_from_type( peer_cert_digest_type );
5756 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5757 size_t digest_len;
5758
5759 if( peer_cert_digest == NULL || digest_info == NULL )
5760 return( -1 );
5761
5762 digest_len = mbedtls_md_get_size( digest_info );
5763 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5764 return( -1 );
5765
5766 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5767 if( ret != 0 )
5768 return( -1 );
5769
5770 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5771}
5772#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005773#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005774
Hanno Becker1294a0b2019-02-05 12:38:15 +00005775static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5776{
5777 if( session->peer_cert != NULL )
5778 {
5779 mbedtls_x509_crt_free( session->peer_cert );
5780 mbedtls_free( session->peer_cert );
5781 session->peer_cert = NULL;
5782 }
Hanno Becker9198ad12019-02-05 17:00:50 +00005783
5784#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5785 if( session->peer_cert_digest != NULL )
5786 {
5787 /* Zeroization is not necessary. */
5788 mbedtls_free( session->peer_cert_digest );
5789 session->peer_cert_digest = NULL;
5790 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5791 session->peer_cert_digest_len = 0;
5792 }
5793#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005794}
5795
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005796/*
5797 * Once the certificate message is read, parse it into a cert chain and
5798 * perform basic checks, but leave actual verification to the caller
5799 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005800static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5801 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005803 int ret;
5804#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5805 int crt_cnt=0;
5806#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005807 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005808 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5814 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5819 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005828
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005832 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005833
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005834 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 }
5842
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005843 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5844 i += 3;
5845
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005846 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 while( i < ssl->in_hslen )
5848 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005849 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005850 if ( i + 3 > ssl->in_hslen ) {
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005852 mbedtls_ssl_send_alert_message( ssl,
5853 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5854 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005855 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5856 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005857 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5858 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 if( ssl->in_msg[i] != 0 )
5860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005862 mbedtls_ssl_send_alert_message( ssl,
5863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005868 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5870 | (unsigned int) ssl->in_msg[i + 2];
5871 i += 3;
5872
5873 if( n < 128 || i + n > ssl->in_hslen )
5874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005876 mbedtls_ssl_send_alert_message( ssl,
5877 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5878 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880 }
5881
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005883#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5884 if( crt_cnt++ == 0 &&
5885 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5886 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005887 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005888 /* During client-side renegotiation, check that the server's
5889 * end-CRTs hasn't changed compared to the initial handshake,
5890 * mitigating the triple handshake attack. On success, reuse
5891 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5893 if( ssl_check_peer_crt_unchanged( ssl,
5894 &ssl->in_msg[i],
5895 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005896 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5898 mbedtls_ssl_send_alert_message( ssl,
5899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5900 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5901 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005902 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005903
5904 /* Now we can safely free the original chain. */
5905 ssl_clear_peer_cert( ssl->session );
5906 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005907#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5908
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005909 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005910 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005911 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005913 case 0: /*ok*/
5914 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5915 /* Ignore certificate with an unknown algorithm: maybe a
5916 prior certificate was already trusted. */
5917 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005918
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005919 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5920 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5921 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005922
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005923 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5924 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5925 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005926
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005927 default:
5928 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5929 crt_parse_der_failed:
5930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5931 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5932 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 }
5934
5935 i += n;
5936 }
5937
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005938 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005939 return( 0 );
5940}
5941
Hanno Becker4a55f632019-02-05 12:49:06 +00005942#if defined(MBEDTLS_SSL_SRV_C)
5943static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5944{
5945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5946 return( -1 );
5947
5948#if defined(MBEDTLS_SSL_PROTO_SSL3)
5949 /*
5950 * Check if the client sent an empty certificate
5951 */
5952 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5953 {
5954 if( ssl->in_msglen == 2 &&
5955 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5956 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5957 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5960 return( 0 );
5961 }
5962
5963 return( -1 );
5964 }
5965#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5966
5967#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5968 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5969 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5970 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5971 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5972 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5975 return( 0 );
5976 }
5977
5978 return( -1 );
5979#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5980 MBEDTLS_SSL_PROTO_TLS1_2 */
5981}
5982#endif /* MBEDTLS_SSL_SRV_C */
5983
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005984/* Check if a certificate message is expected.
5985 * Return either
5986 * - SSL_CERTIFICATE_EXPECTED, or
5987 * - SSL_CERTIFICATE_SKIP
5988 * indicating whether a Certificate message is expected or not.
5989 */
5990#define SSL_CERTIFICATE_EXPECTED 0
5991#define SSL_CERTIFICATE_SKIP 1
5992static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5993 int authmode )
5994{
5995 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5996 ssl->transform_negotiate->ciphersuite_info;
5997
5998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5999 return( SSL_CERTIFICATE_SKIP );
6000
6001#if defined(MBEDTLS_SSL_SRV_C)
6002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6003 {
6004 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6005 return( SSL_CERTIFICATE_SKIP );
6006
6007 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6008 {
6009 /* NOTE: Is it intentional that we set verify_result
6010 * to SKIP_VERIFY on server-side only? */
6011 ssl->session_negotiate->verify_result =
6012 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6013 return( SSL_CERTIFICATE_SKIP );
6014 }
6015 }
6016#endif /* MBEDTLS_SSL_SRV_C */
6017
6018 return( SSL_CERTIFICATE_EXPECTED );
6019}
6020
Hanno Becker68636192019-02-05 14:36:34 +00006021static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6022 int authmode,
6023 mbedtls_x509_crt *chain,
6024 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006025{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006026 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6028 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00006029 mbedtls_x509_crt *ca_chain;
6030 mbedtls_x509_crl *ca_crl;
6031
6032 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6033 return( 0 );
6034
6035#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6036 if( ssl->handshake->sni_ca_chain != NULL )
6037 {
6038 ca_chain = ssl->handshake->sni_ca_chain;
6039 ca_crl = ssl->handshake->sni_ca_crl;
6040 }
6041 else
6042#endif
6043 {
6044 ca_chain = ssl->conf->ca_chain;
6045 ca_crl = ssl->conf->ca_crl;
6046 }
6047
6048 /*
6049 * Main check: verify certificate
6050 */
6051 ret = mbedtls_x509_crt_verify_restartable(
6052 chain,
6053 ca_chain, ca_crl,
6054 ssl->conf->cert_profile,
6055 ssl->hostname,
6056 &ssl->session_negotiate->verify_result,
6057 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6058
6059 if( ret != 0 )
6060 {
6061 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6062 }
6063
6064#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6065 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6066 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6067#endif
6068
6069 /*
6070 * Secondary checks: always done, but change 'ret' only if it was 0
6071 */
6072
6073#if defined(MBEDTLS_ECP_C)
6074 {
6075 const mbedtls_pk_context *pk = &chain->pk;
6076
6077 /* If certificate uses an EC key, make sure the curve is OK */
6078 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6079 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6080 {
6081 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6082
6083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6084 if( ret == 0 )
6085 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6086 }
6087 }
6088#endif /* MBEDTLS_ECP_C */
6089
6090 if( mbedtls_ssl_check_cert_usage( chain,
6091 ciphersuite_info,
6092 ! ssl->conf->endpoint,
6093 &ssl->session_negotiate->verify_result ) != 0 )
6094 {
6095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6096 if( ret == 0 )
6097 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6098 }
6099
6100 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6101 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6102 * with details encoded in the verification flags. All other kinds
6103 * of error codes, including those from the user provided f_vrfy
6104 * functions, are treated as fatal and lead to a failure of
6105 * ssl_parse_certificate even if verification was optional. */
6106 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6107 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6108 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6109 {
6110 ret = 0;
6111 }
6112
6113 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6116 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6117 }
6118
6119 if( ret != 0 )
6120 {
6121 uint8_t alert;
6122
6123 /* The certificate may have been rejected for several reasons.
6124 Pick one and send the corresponding alert. Which alert to send
6125 may be a subject of debate in some cases. */
6126 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6127 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6128 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6129 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6130 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6131 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6132 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6133 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6134 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6135 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6136 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6137 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6138 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6139 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6140 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6141 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6142 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6143 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6144 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6145 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6146 else
6147 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6148 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6149 alert );
6150 }
6151
6152#if defined(MBEDTLS_DEBUG_C)
6153 if( ssl->session_negotiate->verify_result != 0 )
6154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6156 ssl->session_negotiate->verify_result ) );
6157 }
6158 else
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6161 }
6162#endif /* MBEDTLS_DEBUG_C */
6163
6164 return( ret );
6165}
6166
6167int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6168{
6169 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006170 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006171#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6172 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6173 ? ssl->handshake->sni_authmode
6174 : ssl->conf->authmode;
6175#else
6176 const int authmode = ssl->conf->authmode;
6177#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006178 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006179 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006180
6181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6182
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006183 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6184 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006185 {
6186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006187 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006188 }
6189
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006190#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6191 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006192 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006193 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006194 chain = ssl->handshake->ecrs_peer_cert;
6195 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006196 goto crt_verify;
6197 }
6198#endif
6199
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006200 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006201 {
6202 /* mbedtls_ssl_read_record may have sent an alert already. We
6203 let it decide whether to alert. */
6204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006205 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006206 }
6207
Hanno Becker4a55f632019-02-05 12:49:06 +00006208#if defined(MBEDTLS_SSL_SRV_C)
6209 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6210 {
6211 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006212
6213 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006214 ret = 0;
6215 else
6216 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006217
Hanno Becker6bdfab22019-02-05 13:11:17 +00006218 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006219 }
6220#endif /* MBEDTLS_SSL_SRV_C */
6221
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006222 /* Clear existing peer CRT structure in case we tried to
6223 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006224 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006225
6226 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6227 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006228 {
6229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6230 sizeof( mbedtls_x509_crt ) ) );
6231 mbedtls_ssl_send_alert_message( ssl,
6232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6233 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006234
Hanno Becker3dad3112019-02-05 17:19:52 +00006235 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6236 goto exit;
6237 }
6238 mbedtls_x509_crt_init( chain );
6239
6240 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006241 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006242 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006243
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006244#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6245 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006246 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006247
6248crt_verify:
6249 if( ssl->handshake->ecrs_enabled)
6250 rs_ctx = &ssl->handshake->ecrs_ctx;
6251#endif
6252
Hanno Becker68636192019-02-05 14:36:34 +00006253 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006254 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006255 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006256 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006258#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker3dad3112019-02-05 17:19:52 +00006259
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006260 /* Remember digest of the peer's end-CRT. */
6261 ssl->session_negotiate->peer_cert_digest =
6262 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6263 if( ssl->session_negotiate->peer_cert_digest == NULL )
6264 {
6265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6266 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6267 mbedtls_ssl_send_alert_message( ssl,
6268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6269 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker3dad3112019-02-05 17:19:52 +00006270
6271 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6272 goto exit;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006273 }
6274 ret = mbedtls_md( mbedtls_md_info_from_type(
Hanno Becker3dad3112019-02-05 17:19:52 +00006275 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6276 chain->raw.p, chain->raw.len,
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006277 ssl->session_negotiate->peer_cert_digest );
6278 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006279 goto exit;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006280
6281 ssl->session_negotiate->peer_cert_digest_type =
6282 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6283 ssl->session_negotiate->peer_cert_digest_len =
6284 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6285#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6286
Hanno Becker3dad3112019-02-05 17:19:52 +00006287 ssl->session_negotiate->peer_cert = chain;
6288 chain = NULL;
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291
Hanno Becker6bdfab22019-02-05 13:11:17 +00006292exit:
6293
Hanno Becker3dad3112019-02-05 17:19:52 +00006294 if( ret == 0 )
6295 ssl->state++;
6296
6297#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6298 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6299 {
6300 ssl->handshake->ecrs_peer_cert = chain;
6301 chain = NULL;
6302 }
6303#endif
6304
6305 if( chain != NULL )
6306 {
6307 mbedtls_x509_crt_free( chain );
6308 mbedtls_free( chain );
6309 }
6310
Paul Bakker5121ce52009-01-03 21:22:43 +00006311 return( ret );
6312}
Hanno Becker21489932019-02-05 13:20:55 +00006313#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006316{
6317 int ret;
6318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006322 ssl->out_msglen = 1;
6323 ssl->out_msg[0] = 1;
6324
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 ssl->state++;
6326
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006327 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006330 return( ret );
6331 }
6332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006334
6335 return( 0 );
6336}
6337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339{
6340 int ret;
6341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Hanno Becker327c93b2018-08-15 13:56:18 +01006344 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 return( ret );
6348 }
6349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6354 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
6357
Hanno Beckere678eaa2018-08-21 14:57:46 +01006358 /* CCS records are only accepted if they have length 1 and content '1',
6359 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006360
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006361 /*
6362 * Switch to our negotiated transform and session parameters for inbound
6363 * data.
6364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006366 ssl->transform_in = ssl->transform_negotiate;
6367 ssl->session_in = ssl->session_negotiate;
6368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006373 ssl_dtls_replay_reset( ssl );
6374#endif
6375
6376 /* Increment epoch */
6377 if( ++ssl->in_epoch == 0 )
6378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006380 /* This is highly unlikely to happen for legitimate reasons, so
6381 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006383 }
6384 }
6385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006387 memset( ssl->in_ctr, 0, 8 );
6388
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006389 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6392 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6398 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006400 }
6401 }
6402#endif
6403
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 ssl->state++;
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006407
6408 return( 0 );
6409}
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6412 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006413{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006414 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6417 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6418 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006419 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006420 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6423#if defined(MBEDTLS_SHA512_C)
6424 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006425 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6426 else
6427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428#if defined(MBEDTLS_SHA256_C)
6429 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006430 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006431 else
6432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006436 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006437 }
Paul Bakker380da532012-04-18 16:10:25 +00006438}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6443 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006444 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6445 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6448#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006449#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006450 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006451 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6452#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006453 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006454#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006458 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006459 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006460#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006461 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006462#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006463#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006465}
6466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006468 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6471 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006472 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6473 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6476#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006477#if defined(MBEDTLS_USE_PSA_CRYPTO)
6478 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6479#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006480 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006481#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006485 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006486#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006487 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006488#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006491}
6492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6494 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6495static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006496 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006497{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006498 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6499 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006500}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006501#endif
Paul Bakker380da532012-04-18 16:10:25 +00006502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6504#if defined(MBEDTLS_SHA256_C)
6505static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006506 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006507{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006508#if defined(MBEDTLS_USE_PSA_CRYPTO)
6509 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6510#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006511 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006512#endif
Paul Bakker380da532012-04-18 16:10:25 +00006513}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006514#endif
Paul Bakker380da532012-04-18 16:10:25 +00006515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516#if defined(MBEDTLS_SHA512_C)
6517static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006518 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006519{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006521 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006522#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006523 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006524#endif
Paul Bakker380da532012-04-18 16:10:25 +00006525}
Paul Bakker769075d2012-11-24 11:26:46 +01006526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006530static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006532{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006533 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006534 mbedtls_md5_context md5;
6535 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006536
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 unsigned char padbuf[48];
6538 unsigned char md5sum[16];
6539 unsigned char sha1sum[20];
6540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006542 if( !session )
6543 session = ssl->session;
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006546
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006547 mbedtls_md5_init( &md5 );
6548 mbedtls_sha1_init( &sha1 );
6549
6550 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6551 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552
6553 /*
6554 * SSLv3:
6555 * hash =
6556 * MD5( master + pad2 +
6557 * MD5( handshake + sender + master + pad1 ) )
6558 * + SHA1( master + pad2 +
6559 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 */
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006563 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6564 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006565#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006568 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6569 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006573 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Paul Bakker1ef83d62012-04-11 12:09:53 +00006575 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006577 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6578 mbedtls_md5_update_ret( &md5, session->master, 48 );
6579 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6580 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006582 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6583 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6584 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6585 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006586
Paul Bakker1ef83d62012-04-11 12:09:53 +00006587 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006589 mbedtls_md5_starts_ret( &md5 );
6590 mbedtls_md5_update_ret( &md5, session->master, 48 );
6591 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6592 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6593 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006595 mbedtls_sha1_starts_ret( &sha1 );
6596 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6597 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6598 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6599 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006603 mbedtls_md5_free( &md5 );
6604 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006606 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6607 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6608 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006615static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006617{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006618 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006619 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006620 mbedtls_md5_context md5;
6621 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006622 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006625 if( !session )
6626 session = ssl->session;
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006629
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006630 mbedtls_md5_init( &md5 );
6631 mbedtls_sha1_init( &sha1 );
6632
6633 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6634 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636 /*
6637 * TLSv1:
6638 * hash = PRF( master, finished_label,
6639 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6640 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006643 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6644 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006645#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006648 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6649 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006650#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006653 ? "client finished"
6654 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006655
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006656 mbedtls_md5_finish_ret( &md5, padbuf );
6657 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006658
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006659 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006660 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006663
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006664 mbedtls_md5_free( &md5 );
6665 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006666
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006667 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6674#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006675static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006677{
6678 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006679 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006680 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006681#if defined(MBEDTLS_USE_PSA_CRYPTO)
6682 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006683 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006684 psa_status_t status;
6685#else
6686 mbedtls_sha256_context sha256;
6687#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006690 if( !session )
6691 session = ssl->session;
6692
Andrzej Kurekeb342242019-01-29 09:14:33 -05006693 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6694 ? "client finished"
6695 : "server finished";
6696
6697#if defined(MBEDTLS_USE_PSA_CRYPTO)
6698 sha256_psa = psa_hash_operation_init();
6699
6700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6701
6702 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6703 if( status != PSA_SUCCESS )
6704 {
6705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6706 return;
6707 }
6708
6709 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6710 if( status != PSA_SUCCESS )
6711 {
6712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6713 return;
6714 }
6715 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6716#else
6717
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006718 mbedtls_sha256_init( &sha256 );
6719
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006721
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006722 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006723
6724 /*
6725 * TLSv1.2:
6726 * hash = PRF( master, finished_label,
6727 * Hash( handshake ) )[0.11]
6728 */
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if !defined(MBEDTLS_SHA256_ALT)
6731 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006732 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006733#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006734
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006735 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006736 mbedtls_sha256_free( &sha256 );
6737#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006738
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006739 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006740 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006743
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006744 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006751static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006753{
6754 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006755 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006756 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006757#if defined(MBEDTLS_USE_PSA_CRYPTO)
6758 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006759 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006760 psa_status_t status;
6761#else
6762 mbedtls_sha512_context sha512;
6763#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006766 if( !session )
6767 session = ssl->session;
6768
Andrzej Kurekeb342242019-01-29 09:14:33 -05006769 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6770 ? "client finished"
6771 : "server finished";
6772
6773#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006774 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006775
6776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6777
Andrzej Kurek972fba52019-01-30 03:29:12 -05006778 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006779 if( status != PSA_SUCCESS )
6780 {
6781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6782 return;
6783 }
6784
Andrzej Kurek972fba52019-01-30 03:29:12 -05006785 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006786 if( status != PSA_SUCCESS )
6787 {
6788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6789 return;
6790 }
6791 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6792#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006793 mbedtls_sha512_init( &sha512 );
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006796
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006797 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006798
6799 /*
6800 * TLSv1.2:
6801 * hash = PRF( master, finished_label,
6802 * Hash( handshake ) )[0.11]
6803 */
6804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006806 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6807 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006808#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006809
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006810 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006811 mbedtls_sha512_free( &sha512 );
6812#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006813
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006814 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006815 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006818
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006819 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823#endif /* MBEDTLS_SHA512_C */
6824#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006829
6830 /*
6831 * Free our handshake params
6832 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006833 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006835 ssl->handshake = NULL;
6836
6837 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006838 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006839 */
6840 if( ssl->transform )
6841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 mbedtls_ssl_transform_free( ssl->transform );
6843 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006844 }
6845 ssl->transform = ssl->transform_negotiate;
6846 ssl->transform_negotiate = NULL;
6847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006849}
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006852{
6853 int resume = ssl->handshake->resume;
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857#if defined(MBEDTLS_SSL_RENEGOTIATION)
6858 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006861 ssl->renego_records_seen = 0;
6862 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006863#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006864
6865 /*
6866 * Free the previous session and switch in the current one
6867 */
Paul Bakker0a597072012-09-25 21:55:46 +00006868 if( ssl->session )
6869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006871 /* RFC 7366 3.1: keep the EtM state */
6872 ssl->session_negotiate->encrypt_then_mac =
6873 ssl->session->encrypt_then_mac;
6874#endif
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 mbedtls_ssl_session_free( ssl->session );
6877 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006878 }
6879 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006880 ssl->session_negotiate = NULL;
6881
Paul Bakker0a597072012-09-25 21:55:46 +00006882 /*
6883 * Add cache entry
6884 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006885 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006886 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006887 resume == 0 )
6888 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006889 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006891 }
Paul Bakker0a597072012-09-25 21:55:46 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006895 ssl->handshake->flight != NULL )
6896 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006897 /* Cancel handshake timer */
6898 ssl_set_timer( ssl, 0 );
6899
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006900 /* Keep last flight around in case we need to resend it:
6901 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006903 }
6904 else
6905#endif
6906 ssl_handshake_wrapup_free_hs_transform( ssl );
6907
Paul Bakker48916f92012-09-16 19:57:18 +00006908 ssl->state++;
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006911}
6912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006915 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006918
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006919 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006920
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006921 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006923 /*
6924 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6925 * may define some other value. Currently (early 2016), no defined
6926 * ciphersuite does this (and this is unlikely to change as activity has
6927 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006932 ssl->verify_data_len = hash_len;
6933 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006934#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006935
Paul Bakker5121ce52009-01-03 21:22:43 +00006936 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6938 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006939
6940 /*
6941 * In case of session resuming, invert the client and server
6942 * ChangeCipherSpec messages order.
6943 */
Paul Bakker0a597072012-09-25 21:55:46 +00006944 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006951 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006953#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 }
6955 else
6956 ssl->state++;
6957
Paul Bakker48916f92012-09-16 19:57:18 +00006958 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006959 * Switch to our negotiated transform and session parameters for outbound
6960 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006966 {
6967 unsigned char i;
6968
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006969 /* Remember current epoch settings for resending */
6970 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006971 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006972
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006973 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006974 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006975
6976 /* Increment epoch */
6977 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006978 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006979 break;
6980
6981 /* The loop goes to its end iff the counter is wrapping */
6982 if( i == 0 )
6983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6985 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006986 }
6987 }
6988 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006990 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006992 ssl->transform_out = ssl->transform_negotiate;
6993 ssl->session_out = ssl->session_negotiate;
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6996 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7001 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007002 }
7003 }
7004#endif
7005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007009#endif
7010
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007011 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007012 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007014 return( ret );
7015 }
7016
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007017#if defined(MBEDTLS_SSL_PROTO_DTLS)
7018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7019 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7020 {
7021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7022 return( ret );
7023 }
7024#endif
7025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
7028 return( 0 );
7029}
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007032#define SSL_MAX_HASH_LEN 36
7033#else
7034#define SSL_MAX_HASH_LEN 12
7035#endif
7036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038{
Paul Bakker23986e52011-04-24 08:57:21 +00007039 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007040 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007041 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007045 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
Hanno Becker327c93b2018-08-15 13:56:18 +01007047 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050 return( ret );
7051 }
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7057 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059 }
7060
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007061 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#if defined(MBEDTLS_SSL_PROTO_SSL3)
7063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007064 hash_len = 36;
7065 else
7066#endif
7067 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7070 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007073 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7074 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076 }
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007079 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7083 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007085 }
7086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007088 ssl->verify_data_len = hash_len;
7089 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007090#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007091
Paul Bakker0a597072012-09-25 21:55:46 +00007092 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007095 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 }
7103 else
7104 ssl->state++;
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007109#endif
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007112
7113 return( 0 );
7114}
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7121 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7122 mbedtls_md5_init( &handshake->fin_md5 );
7123 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007124 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7125 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7128#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007129#if defined(MBEDTLS_USE_PSA_CRYPTO)
7130 handshake->fin_sha256_psa = psa_hash_operation_init();
7131 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7132#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007134 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007135#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007138#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007139 handshake->fin_sha384_psa = psa_hash_operation_init();
7140 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007141#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007143 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007144#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007147
7148 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007149
7150#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7151 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7152 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7153#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_DHM_C)
7156 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if defined(MBEDTLS_ECDH_C)
7159 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007160#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007161#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007162 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007163#if defined(MBEDTLS_SSL_CLI_C)
7164 handshake->ecjpake_cache = NULL;
7165 handshake->ecjpake_cache_len = 0;
7166#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007167#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007168
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007169#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007170 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007171#endif
7172
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007173#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7174 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7175#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007176}
7177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7183 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 mbedtls_md_init( &transform->md_ctx_enc );
7186 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007187}
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007192}
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007195{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007196 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007197 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007199 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007201 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007202 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007203
7204 /*
7205 * Either the pointers are now NULL or cleared properly and can be freed.
7206 * Now allocate missing structures.
7207 */
7208 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007209 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007210 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007211 }
Paul Bakker48916f92012-09-16 19:57:18 +00007212
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007213 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007214 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007215 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007216 }
Paul Bakker48916f92012-09-16 19:57:18 +00007217
Paul Bakker82788fb2014-10-20 13:59:19 +02007218 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007219 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007220 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007221 }
Paul Bakker48916f92012-09-16 19:57:18 +00007222
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007223 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007224 if( ssl->handshake == NULL ||
7225 ssl->transform_negotiate == NULL ||
7226 ssl->session_negotiate == NULL )
7227 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 mbedtls_free( ssl->handshake );
7231 mbedtls_free( ssl->transform_negotiate );
7232 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007233
7234 ssl->handshake = NULL;
7235 ssl->transform_negotiate = NULL;
7236 ssl->session_negotiate = NULL;
7237
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007239 }
7240
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007241 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007243 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007244 ssl_handshake_params_init( ssl->handshake );
7245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7248 {
7249 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007250
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007251 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7252 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7253 else
7254 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007255
7256 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007257 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007258#endif
7259
Paul Bakker48916f92012-09-16 19:57:18 +00007260 return( 0 );
7261}
7262
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007263#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007264/* Dummy cookie callbacks for defaults */
7265static int ssl_cookie_write_dummy( void *ctx,
7266 unsigned char **p, unsigned char *end,
7267 const unsigned char *cli_id, size_t cli_id_len )
7268{
7269 ((void) ctx);
7270 ((void) p);
7271 ((void) end);
7272 ((void) cli_id);
7273 ((void) cli_id_len);
7274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007276}
7277
7278static int ssl_cookie_check_dummy( void *ctx,
7279 const unsigned char *cookie, size_t cookie_len,
7280 const unsigned char *cli_id, size_t cli_id_len )
7281{
7282 ((void) ctx);
7283 ((void) cookie);
7284 ((void) cookie_len);
7285 ((void) cli_id);
7286 ((void) cli_id_len);
7287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007289}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007290#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007291
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007292/* Once ssl->out_hdr as the address of the beginning of the
7293 * next outgoing record is set, deduce the other pointers.
7294 *
7295 * Note: For TLS, we save the implicit record sequence number
7296 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7297 * and the caller has to make sure there's space for this.
7298 */
7299
7300static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7301 mbedtls_ssl_transform *transform )
7302{
7303#if defined(MBEDTLS_SSL_PROTO_DTLS)
7304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7305 {
7306 ssl->out_ctr = ssl->out_hdr + 3;
7307 ssl->out_len = ssl->out_hdr + 11;
7308 ssl->out_iv = ssl->out_hdr + 13;
7309 }
7310 else
7311#endif
7312 {
7313 ssl->out_ctr = ssl->out_hdr - 8;
7314 ssl->out_len = ssl->out_hdr + 3;
7315 ssl->out_iv = ssl->out_hdr + 5;
7316 }
7317
7318 /* Adjust out_msg to make space for explicit IV, if used. */
7319 if( transform != NULL &&
7320 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7321 {
7322 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7323 }
7324 else
7325 ssl->out_msg = ssl->out_iv;
7326}
7327
7328/* Once ssl->in_hdr as the address of the beginning of the
7329 * next incoming record is set, deduce the other pointers.
7330 *
7331 * Note: For TLS, we save the implicit record sequence number
7332 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7333 * and the caller has to make sure there's space for this.
7334 */
7335
7336static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7337 mbedtls_ssl_transform *transform )
7338{
7339#if defined(MBEDTLS_SSL_PROTO_DTLS)
7340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7341 {
7342 ssl->in_ctr = ssl->in_hdr + 3;
7343 ssl->in_len = ssl->in_hdr + 11;
7344 ssl->in_iv = ssl->in_hdr + 13;
7345 }
7346 else
7347#endif
7348 {
7349 ssl->in_ctr = ssl->in_hdr - 8;
7350 ssl->in_len = ssl->in_hdr + 3;
7351 ssl->in_iv = ssl->in_hdr + 5;
7352 }
7353
7354 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7355 if( transform != NULL &&
7356 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7357 {
7358 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7359 }
7360 else
7361 ssl->in_msg = ssl->in_iv;
7362}
7363
Paul Bakker5121ce52009-01-03 21:22:43 +00007364/*
7365 * Initialize an SSL context
7366 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007367void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7368{
7369 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7370}
7371
7372/*
7373 * Setup an SSL context
7374 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007375
7376static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7377{
7378 /* Set the incoming and outgoing record pointers. */
7379#if defined(MBEDTLS_SSL_PROTO_DTLS)
7380 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7381 {
7382 ssl->out_hdr = ssl->out_buf;
7383 ssl->in_hdr = ssl->in_buf;
7384 }
7385 else
7386#endif /* MBEDTLS_SSL_PROTO_DTLS */
7387 {
7388 ssl->out_hdr = ssl->out_buf + 8;
7389 ssl->in_hdr = ssl->in_buf + 8;
7390 }
7391
7392 /* Derive other internal pointers. */
7393 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7394 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7395}
7396
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007397int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007398 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007399{
Paul Bakker48916f92012-09-16 19:57:18 +00007400 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007402 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007403
7404 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007405 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007406 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007407
7408 /* Set to NULL in case of an error condition */
7409 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007410
Angus Grattond8213d02016-05-25 20:56:48 +10007411 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7412 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007413 {
Angus Grattond8213d02016-05-25 20:56:48 +10007414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007415 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007416 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007417 }
7418
7419 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7420 if( ssl->out_buf == NULL )
7421 {
7422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007423 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007424 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007425 }
7426
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007427 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007428
Paul Bakker48916f92012-09-16 19:57:18 +00007429 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007430 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
7432 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007433
7434error:
7435 mbedtls_free( ssl->in_buf );
7436 mbedtls_free( ssl->out_buf );
7437
7438 ssl->conf = NULL;
7439
7440 ssl->in_buf = NULL;
7441 ssl->out_buf = NULL;
7442
7443 ssl->in_hdr = NULL;
7444 ssl->in_ctr = NULL;
7445 ssl->in_len = NULL;
7446 ssl->in_iv = NULL;
7447 ssl->in_msg = NULL;
7448
7449 ssl->out_hdr = NULL;
7450 ssl->out_ctr = NULL;
7451 ssl->out_len = NULL;
7452 ssl->out_iv = NULL;
7453 ssl->out_msg = NULL;
7454
k-stachowiak9f7798e2018-07-31 16:52:32 +02007455 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007456}
7457
7458/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007459 * Reset an initialized and used SSL context for re-use while retaining
7460 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007461 *
7462 * If partial is non-zero, keep data in the input buffer and client ID.
7463 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007464 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007465static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007466{
Paul Bakker48916f92012-09-16 19:57:18 +00007467 int ret;
7468
Hanno Becker7e772132018-08-10 12:38:21 +01007469#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7470 !defined(MBEDTLS_SSL_SRV_C)
7471 ((void) partial);
7472#endif
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007475
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007476 /* Cancel any possibly running timer */
7477 ssl_set_timer( ssl, 0 );
7478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#if defined(MBEDTLS_SSL_RENEGOTIATION)
7480 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007481 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007482
7483 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7485 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007488
Paul Bakker7eb013f2011-10-06 12:37:39 +00007489 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007490 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007491
7492 ssl->in_msgtype = 0;
7493 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007495 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007496 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007499 ssl_dtls_replay_reset( ssl );
7500#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007501
7502 ssl->in_hslen = 0;
7503 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007504
7505 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007506
7507 ssl->out_msgtype = 0;
7508 ssl->out_msglen = 0;
7509 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7511 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007512 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007513#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007514
Hanno Becker19859472018-08-06 09:40:20 +01007515 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7516
Paul Bakker48916f92012-09-16 19:57:18 +00007517 ssl->transform_in = NULL;
7518 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007519
Hanno Becker78640902018-08-13 16:35:15 +01007520 ssl->session_in = NULL;
7521 ssl->session_out = NULL;
7522
Angus Grattond8213d02016-05-25 20:56:48 +10007523 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007524
7525#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007526 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007527#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7528 {
7529 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007530 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007531 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7534 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7537 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7540 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007541 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007542 }
7543#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007544
Paul Bakker48916f92012-09-16 19:57:18 +00007545 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 mbedtls_ssl_transform_free( ssl->transform );
7548 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007549 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007550 }
Paul Bakker48916f92012-09-16 19:57:18 +00007551
Paul Bakkerc0463502013-02-14 11:19:38 +01007552 if( ssl->session )
7553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 mbedtls_ssl_session_free( ssl->session );
7555 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007556 ssl->session = NULL;
7557 }
7558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007560 ssl->alpn_chosen = NULL;
7561#endif
7562
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007563#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007564#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007565 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007566#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007567 {
7568 mbedtls_free( ssl->cli_id );
7569 ssl->cli_id = NULL;
7570 ssl->cli_id_len = 0;
7571 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007572#endif
7573
Paul Bakker48916f92012-09-16 19:57:18 +00007574 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7575 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007576
7577 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007578}
7579
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007580/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007581 * Reset an initialized and used SSL context for re-use while retaining
7582 * all application-set variables, function pointers and data.
7583 */
7584int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7585{
7586 return( ssl_session_reset_int( ssl, 0 ) );
7587}
7588
7589/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007590 * SSL set accessors
7591 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007592void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007594 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007595}
7596
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007597void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007599 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007600}
7601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007603void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007605 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007606}
7607#endif
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007610void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007612 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007613}
7614#endif
7615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007617
Hanno Becker1841b0a2018-08-24 11:13:57 +01007618void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7619 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007620{
7621 ssl->disable_datagram_packing = !allow_packing;
7622}
7623
7624void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7625 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007627 conf->hs_timeout_min = min;
7628 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007629}
7630#endif
7631
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007632void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007634 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007635}
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007639 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007640 void *p_vrfy )
7641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007642 conf->f_vrfy = f_vrfy;
7643 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007646
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007647void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007648 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007649 void *p_rng )
7650{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007651 conf->f_rng = f_rng;
7652 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007653}
7654
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007655void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007656 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007657 void *p_dbg )
7658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007659 conf->f_dbg = f_dbg;
7660 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007661}
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007664 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007665 mbedtls_ssl_send_t *f_send,
7666 mbedtls_ssl_recv_t *f_recv,
7667 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007668{
7669 ssl->p_bio = p_bio;
7670 ssl->f_send = f_send;
7671 ssl->f_recv = f_recv;
7672 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007673}
7674
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007675#if defined(MBEDTLS_SSL_PROTO_DTLS)
7676void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7677{
7678 ssl->mtu = mtu;
7679}
7680#endif
7681
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007682void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007683{
7684 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007685}
7686
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007687void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7688 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007689 mbedtls_ssl_set_timer_t *f_set_timer,
7690 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007691{
7692 ssl->p_timer = p_timer;
7693 ssl->f_set_timer = f_set_timer;
7694 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007695
7696 /* Make sure we start with no timer running */
7697 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007698}
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007701void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007702 void *p_cache,
7703 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7704 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007706 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007707 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007708 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#if defined(MBEDTLS_SSL_CLI_C)
7713int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007715 int ret;
7716
7717 if( ssl == NULL ||
7718 session == NULL ||
7719 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007720 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007723 }
7724
Hanno Becker52055ae2019-02-06 14:30:46 +00007725 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7726 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007727 return( ret );
7728
Paul Bakker0a597072012-09-25 21:55:46 +00007729 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007730
7731 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007734
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007735void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007736 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007738 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7739 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7740 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7741 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007742}
7743
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007744void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007745 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007746 int major, int minor )
7747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007749 return;
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007752 return;
7753
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007755}
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007758void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007759 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007760{
7761 conf->cert_profile = profile;
7762}
7763
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007764/* Append a new keycert entry to a (possibly empty) list */
7765static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7766 mbedtls_x509_crt *cert,
7767 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007768{
niisato8ee24222018-06-25 19:05:48 +09007769 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007770
niisato8ee24222018-06-25 19:05:48 +09007771 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7772 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007774
niisato8ee24222018-06-25 19:05:48 +09007775 new_cert->cert = cert;
7776 new_cert->key = key;
7777 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007778
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007779 /* Update head is the list was null, else add to the end */
7780 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007781 {
niisato8ee24222018-06-25 19:05:48 +09007782 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007783 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007784 else
7785 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007786 mbedtls_ssl_key_cert *cur = *head;
7787 while( cur->next != NULL )
7788 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007789 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007790 }
7791
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007792 return( 0 );
7793}
7794
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007795int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007796 mbedtls_x509_crt *own_cert,
7797 mbedtls_pk_context *pk_key )
7798{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007799 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007800}
7801
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007802void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007803 mbedtls_x509_crt *ca_chain,
7804 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007806 conf->ca_chain = ca_chain;
7807 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007810
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007811#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7812int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7813 mbedtls_x509_crt *own_cert,
7814 mbedtls_pk_context *pk_key )
7815{
7816 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7817 own_cert, pk_key ) );
7818}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007819
7820void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7821 mbedtls_x509_crt *ca_chain,
7822 mbedtls_x509_crl *ca_crl )
7823{
7824 ssl->handshake->sni_ca_chain = ca_chain;
7825 ssl->handshake->sni_ca_crl = ca_crl;
7826}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007827
7828void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7829 int authmode )
7830{
7831 ssl->handshake->sni_authmode = authmode;
7832}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007833#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7834
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007835#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007836/*
7837 * Set EC J-PAKE password for current handshake
7838 */
7839int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7840 const unsigned char *pw,
7841 size_t pw_len )
7842{
7843 mbedtls_ecjpake_role role;
7844
Janos Follath8eb64132016-06-03 15:40:57 +01007845 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7847
7848 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7849 role = MBEDTLS_ECJPAKE_SERVER;
7850 else
7851 role = MBEDTLS_ECJPAKE_CLIENT;
7852
7853 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7854 role,
7855 MBEDTLS_MD_SHA256,
7856 MBEDTLS_ECP_DP_SECP256R1,
7857 pw, pw_len ) );
7858}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007859#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007862
7863static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7864{
7865 /* Remove reference to existing PSK, if any. */
7866#if defined(MBEDTLS_USE_PSA_CRYPTO)
7867 if( conf->psk_opaque != 0 )
7868 {
7869 /* The maintenance of the PSK key slot is the
7870 * user's responsibility. */
7871 conf->psk_opaque = 0;
7872 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007873 /* This and the following branch should never
7874 * be taken simultaenously as we maintain the
7875 * invariant that raw and opaque PSKs are never
7876 * configured simultaneously. As a safeguard,
7877 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007878#endif /* MBEDTLS_USE_PSA_CRYPTO */
7879 if( conf->psk != NULL )
7880 {
7881 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7882
7883 mbedtls_free( conf->psk );
7884 conf->psk = NULL;
7885 conf->psk_len = 0;
7886 }
7887
7888 /* Remove reference to PSK identity, if any. */
7889 if( conf->psk_identity != NULL )
7890 {
7891 mbedtls_free( conf->psk_identity );
7892 conf->psk_identity = NULL;
7893 conf->psk_identity_len = 0;
7894 }
7895}
7896
Hanno Becker7390c712018-11-15 13:33:04 +00007897/* This function assumes that PSK identity in the SSL config is unset.
7898 * It checks that the provided identity is well-formed and attempts
7899 * to make a copy of it in the SSL config.
7900 * On failure, the PSK identity in the config remains unset. */
7901static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7902 unsigned char const *psk_identity,
7903 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007904{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007905 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007906 if( psk_identity == NULL ||
7907 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007908 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007909 {
7910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7911 }
7912
Hanno Becker7390c712018-11-15 13:33:04 +00007913 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7914 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007915 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007916
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007917 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007918 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007919
7920 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007921}
7922
Hanno Becker7390c712018-11-15 13:33:04 +00007923int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7924 const unsigned char *psk, size_t psk_len,
7925 const unsigned char *psk_identity, size_t psk_identity_len )
7926{
7927 int ret;
7928 /* Remove opaque/raw PSK + PSK Identity */
7929 ssl_conf_remove_psk( conf );
7930
7931 /* Check and set raw PSK */
7932 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7934 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7935 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7936 conf->psk_len = psk_len;
7937 memcpy( conf->psk, psk, conf->psk_len );
7938
7939 /* Check and set PSK Identity */
7940 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7941 if( ret != 0 )
7942 ssl_conf_remove_psk( conf );
7943
7944 return( ret );
7945}
7946
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007947static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7948{
7949#if defined(MBEDTLS_USE_PSA_CRYPTO)
7950 if( ssl->handshake->psk_opaque != 0 )
7951 {
7952 ssl->handshake->psk_opaque = 0;
7953 }
7954 else
7955#endif /* MBEDTLS_USE_PSA_CRYPTO */
7956 if( ssl->handshake->psk != NULL )
7957 {
7958 mbedtls_platform_zeroize( ssl->handshake->psk,
7959 ssl->handshake->psk_len );
7960 mbedtls_free( ssl->handshake->psk );
7961 ssl->handshake->psk_len = 0;
7962 }
7963}
7964
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007965int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7966 const unsigned char *psk, size_t psk_len )
7967{
7968 if( psk == NULL || ssl->handshake == NULL )
7969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7970
7971 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7973
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007974 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007975
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007976 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007977 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007978
7979 ssl->handshake->psk_len = psk_len;
7980 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7981
7982 return( 0 );
7983}
7984
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007985#if defined(MBEDTLS_USE_PSA_CRYPTO)
7986int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007987 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007988 const unsigned char *psk_identity,
7989 size_t psk_identity_len )
7990{
Hanno Becker7390c712018-11-15 13:33:04 +00007991 int ret;
7992 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007993 ssl_conf_remove_psk( conf );
7994
Hanno Becker7390c712018-11-15 13:33:04 +00007995 /* Check and set opaque PSK */
7996 if( psk_slot == 0 )
7997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007998 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007999
8000 /* Check and set PSK Identity */
8001 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8002 psk_identity_len );
8003 if( ret != 0 )
8004 ssl_conf_remove_psk( conf );
8005
8006 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008007}
8008
8009int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008010 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008011{
8012 if( psk_slot == 0 || ssl->handshake == NULL )
8013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8014
8015 ssl_remove_psk( ssl );
8016 ssl->handshake->psk_opaque = psk_slot;
8017 return( 0 );
8018}
8019#endif /* MBEDTLS_USE_PSA_CRYPTO */
8020
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008021void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008023 size_t),
8024 void *p_psk )
8025{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008026 conf->f_psk = f_psk;
8027 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008030
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008031#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008032
8033#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008034int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035{
8036 int ret;
8037
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008038 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8039 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8040 {
8041 mbedtls_mpi_free( &conf->dhm_P );
8042 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008043 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008045
8046 return( 0 );
8047}
Hanno Becker470a8c42017-10-04 15:28:46 +01008048#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008049
Hanno Beckera90658f2017-10-04 15:29:08 +01008050int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8051 const unsigned char *dhm_P, size_t P_len,
8052 const unsigned char *dhm_G, size_t G_len )
8053{
8054 int ret;
8055
8056 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8057 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8058 {
8059 mbedtls_mpi_free( &conf->dhm_P );
8060 mbedtls_mpi_free( &conf->dhm_G );
8061 return( ret );
8062 }
8063
8064 return( 0 );
8065}
Paul Bakker5121ce52009-01-03 21:22:43 +00008066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008067int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008068{
8069 int ret;
8070
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008071 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8072 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8073 {
8074 mbedtls_mpi_free( &conf->dhm_P );
8075 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008076 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008077 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008078
8079 return( 0 );
8080}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008081#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008082
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008083#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8084/*
8085 * Set the minimum length for Diffie-Hellman parameters
8086 */
8087void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8088 unsigned int bitlen )
8089{
8090 conf->dhm_min_bitlen = bitlen;
8091}
8092#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8093
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008094#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008095/*
8096 * Set allowed/preferred hashes for handshake signatures
8097 */
8098void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8099 const int *hashes )
8100{
8101 conf->sig_hashes = hashes;
8102}
Hanno Becker947194e2017-04-07 13:25:49 +01008103#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008104
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008105#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008106/*
8107 * Set the allowed elliptic curves
8108 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008109void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008110 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008111{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008112 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008113}
Hanno Becker947194e2017-04-07 13:25:49 +01008114#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008115
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008116#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008118{
Hanno Becker947194e2017-04-07 13:25:49 +01008119 /* Initialize to suppress unnecessary compiler warning */
8120 size_t hostname_len = 0;
8121
8122 /* Check if new hostname is valid before
8123 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008124 if( hostname != NULL )
8125 {
8126 hostname_len = strlen( hostname );
8127
8128 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8130 }
8131
8132 /* Now it's clear that we will overwrite the old hostname,
8133 * so we can free it safely */
8134
8135 if( ssl->hostname != NULL )
8136 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008137 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008138 mbedtls_free( ssl->hostname );
8139 }
8140
8141 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008142
Paul Bakker5121ce52009-01-03 21:22:43 +00008143 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008144 {
8145 ssl->hostname = NULL;
8146 }
8147 else
8148 {
8149 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008150 if( ssl->hostname == NULL )
8151 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008152
Hanno Becker947194e2017-04-07 13:25:49 +01008153 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008154
Hanno Becker947194e2017-04-07 13:25:49 +01008155 ssl->hostname[hostname_len] = '\0';
8156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008157
8158 return( 0 );
8159}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008160#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008161
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008162#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008163void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008165 const unsigned char *, size_t),
8166 void *p_sni )
8167{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008168 conf->f_sni = f_sni;
8169 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008174int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008175{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008176 size_t cur_len, tot_len;
8177 const char **p;
8178
8179 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008180 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8181 * MUST NOT be truncated."
8182 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008183 */
8184 tot_len = 0;
8185 for( p = protos; *p != NULL; p++ )
8186 {
8187 cur_len = strlen( *p );
8188 tot_len += cur_len;
8189
8190 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008192 }
8193
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008194 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008195
8196 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008197}
8198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008200{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008201 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008204
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008205void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008207 conf->max_major_ver = major;
8208 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008209}
8210
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008211void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008213 conf->min_major_ver = major;
8214 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008215}
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008218void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008219{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008220 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008221}
8222#endif
8223
Janos Follath088ce432017-04-10 12:42:31 +01008224#if defined(MBEDTLS_SSL_SRV_C)
8225void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8226 char cert_req_ca_list )
8227{
8228 conf->cert_req_ca_list = cert_req_ca_list;
8229}
8230#endif
8231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008233void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008234{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008235 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008236}
8237#endif
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008240void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008241{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008242 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008243}
8244#endif
8245
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008246#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008247void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008248{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008249 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008250}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008251#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008254int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008257 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008260 }
8261
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008262 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008263
8264 return( 0 );
8265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008269void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008270{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008271 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008276void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008277{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008278 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008279}
8280#endif
8281
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008282void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008284 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008285}
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008288void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008290 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008291}
8292
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008293void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008295 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008296}
8297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008298void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008299 const unsigned char period[8] )
8300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008301 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008306#if defined(MBEDTLS_SSL_CLI_C)
8307void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008308{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008309 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008310}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008311#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008312
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008313#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008314void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8315 mbedtls_ssl_ticket_write_t *f_ticket_write,
8316 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8317 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008318{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008319 conf->f_ticket_write = f_ticket_write;
8320 conf->f_ticket_parse = f_ticket_parse;
8321 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008322}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008325
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008326#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8327void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8328 mbedtls_ssl_export_keys_t *f_export_keys,
8329 void *p_export_keys )
8330{
8331 conf->f_export_keys = f_export_keys;
8332 conf->p_export_keys = p_export_keys;
8333}
8334#endif
8335
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008336#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008337void mbedtls_ssl_conf_async_private_cb(
8338 mbedtls_ssl_config *conf,
8339 mbedtls_ssl_async_sign_t *f_async_sign,
8340 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8341 mbedtls_ssl_async_resume_t *f_async_resume,
8342 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008343 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008344{
8345 conf->f_async_sign_start = f_async_sign;
8346 conf->f_async_decrypt_start = f_async_decrypt;
8347 conf->f_async_resume = f_async_resume;
8348 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008349 conf->p_async_config_data = async_config_data;
8350}
8351
Gilles Peskine8f97af72018-04-26 11:46:10 +02008352void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8353{
8354 return( conf->p_async_config_data );
8355}
8356
Gilles Peskine1febfef2018-04-30 11:54:39 +02008357void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008358{
8359 if( ssl->handshake == NULL )
8360 return( NULL );
8361 else
8362 return( ssl->handshake->user_async_ctx );
8363}
8364
Gilles Peskine1febfef2018-04-30 11:54:39 +02008365void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008366 void *ctx )
8367{
8368 if( ssl->handshake != NULL )
8369 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008370}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008371#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008372
Paul Bakker5121ce52009-01-03 21:22:43 +00008373/*
8374 * SSL get accessors
8375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008377{
8378 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8379}
8380
Hanno Becker8b170a02017-10-10 11:51:19 +01008381int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8382{
8383 /*
8384 * Case A: We're currently holding back
8385 * a message for further processing.
8386 */
8387
8388 if( ssl->keep_current_message == 1 )
8389 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008391 return( 1 );
8392 }
8393
8394 /*
8395 * Case B: Further records are pending in the current datagram.
8396 */
8397
8398#if defined(MBEDTLS_SSL_PROTO_DTLS)
8399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8400 ssl->in_left > ssl->next_record_offset )
8401 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008403 return( 1 );
8404 }
8405#endif /* MBEDTLS_SSL_PROTO_DTLS */
8406
8407 /*
8408 * Case C: A handshake message is being processed.
8409 */
8410
Hanno Becker8b170a02017-10-10 11:51:19 +01008411 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8412 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008414 return( 1 );
8415 }
8416
8417 /*
8418 * Case D: An application data message is being processed
8419 */
8420 if( ssl->in_offt != NULL )
8421 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008423 return( 1 );
8424 }
8425
8426 /*
8427 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008428 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008429 * we implement support for multiple alerts in single records.
8430 */
8431
8432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8433 return( 0 );
8434}
8435
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008436uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008437{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008438 if( ssl->session != NULL )
8439 return( ssl->session->verify_result );
8440
8441 if( ssl->session_negotiate != NULL )
8442 return( ssl->session_negotiate->verify_result );
8443
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008444 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008445}
8446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008448{
Paul Bakker926c8e42013-03-06 10:23:34 +01008449 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008450 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008453}
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008459 {
8460 switch( ssl->minor_ver )
8461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008463 return( "DTLSv1.0" );
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008466 return( "DTLSv1.2" );
8467
8468 default:
8469 return( "unknown (DTLS)" );
8470 }
8471 }
8472#endif
8473
Paul Bakker43ca69c2011-01-15 17:35:19 +00008474 switch( ssl->minor_ver )
8475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008477 return( "SSLv3.0" );
8478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008480 return( "TLSv1.0" );
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008483 return( "TLSv1.1" );
8484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008486 return( "TLSv1.2" );
8487
Paul Bakker43ca69c2011-01-15 17:35:19 +00008488 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008489 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008490 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008491}
8492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008494{
Hanno Becker3136ede2018-08-17 15:28:19 +01008495 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008497 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008498
Hanno Becker78640902018-08-13 16:35:15 +01008499 if( transform == NULL )
8500 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#if defined(MBEDTLS_ZLIB_SUPPORT)
8503 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8504 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008505#endif
8506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 case MBEDTLS_MODE_GCM:
8510 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008511 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008513 transform_expansion = transform->minlen;
8514 break;
8515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008517
8518 block_size = mbedtls_cipher_get_block_size(
8519 &transform->cipher_ctx_enc );
8520
Hanno Becker3136ede2018-08-17 15:28:19 +01008521 /* Expansion due to the addition of the MAC. */
8522 transform_expansion += transform->maclen;
8523
8524 /* Expansion due to the addition of CBC padding;
8525 * Theoretically up to 256 bytes, but we never use
8526 * more than the block size of the underlying cipher. */
8527 transform_expansion += block_size;
8528
8529 /* For TLS 1.1 or higher, an explicit IV is added
8530 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008531#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8532 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008533 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008534#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008535
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008536 break;
8537
8538 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008541 }
8542
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008543 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008544}
8545
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008546#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8547size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8548{
8549 size_t max_len;
8550
8551 /*
8552 * Assume mfl_code is correct since it was checked when set
8553 */
Angus Grattond8213d02016-05-25 20:56:48 +10008554 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008555
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008556 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008557 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008558 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008559 {
Angus Grattond8213d02016-05-25 20:56:48 +10008560 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008561 }
8562
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008563 /* During a handshake, use the value being negotiated */
8564 if( ssl->session_negotiate != NULL &&
8565 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8566 {
8567 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8568 }
8569
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008570 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008571}
8572#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8573
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008574#if defined(MBEDTLS_SSL_PROTO_DTLS)
8575static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8576{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008577 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8579 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8580 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8581 return ( 0 );
8582
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008583 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8584 return( ssl->mtu );
8585
8586 if( ssl->mtu == 0 )
8587 return( ssl->handshake->mtu );
8588
8589 return( ssl->mtu < ssl->handshake->mtu ?
8590 ssl->mtu : ssl->handshake->mtu );
8591}
8592#endif /* MBEDTLS_SSL_PROTO_DTLS */
8593
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008594int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8595{
8596 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8597
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008598#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8599 !defined(MBEDTLS_SSL_PROTO_DTLS)
8600 (void) ssl;
8601#endif
8602
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008603#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8604 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8605
8606 if( max_len > mfl )
8607 max_len = mfl;
8608#endif
8609
8610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008611 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008612 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008613 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008614 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8615 const size_t overhead = (size_t) ret;
8616
8617 if( ret < 0 )
8618 return( ret );
8619
8620 if( mtu <= overhead )
8621 {
8622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8623 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8624 }
8625
8626 if( max_len > mtu - overhead )
8627 max_len = mtu - overhead;
8628 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008630
Hanno Becker0defedb2018-08-10 12:35:02 +01008631#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8632 !defined(MBEDTLS_SSL_PROTO_DTLS)
8633 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008634#endif
8635
8636 return( (int) max_len );
8637}
8638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_X509_CRT_PARSE_C)
8640const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008641{
8642 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008643 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008644
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008645 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008650int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8651 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008652{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008653 if( ssl == NULL ||
8654 dst == NULL ||
8655 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008656 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008659 }
8660
Hanno Becker52055ae2019-02-06 14:30:46 +00008661 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008664
Paul Bakker5121ce52009-01-03 21:22:43 +00008665/*
Paul Bakker1961b702013-01-25 14:49:24 +01008666 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008671
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008672 if( ssl == NULL || ssl->conf == NULL )
8673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008676 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008682#endif
8683
Paul Bakker1961b702013-01-25 14:49:24 +01008684 return( ret );
8685}
8686
8687/*
8688 * Perform the SSL handshake
8689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008691{
8692 int ret = 0;
8693
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008694 if( ssl == NULL || ssl->conf == NULL )
8695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008702
8703 if( ret != 0 )
8704 break;
8705 }
8706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008708
8709 return( ret );
8710}
8711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#if defined(MBEDTLS_SSL_RENEGOTIATION)
8713#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008714/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008715 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008718{
8719 int ret;
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008722
8723 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8725 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008726
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008727 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008728 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008730 return( ret );
8731 }
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008734
8735 return( 0 );
8736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008738
8739/*
8740 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 * - any side: calling mbedtls_ssl_renegotiate(),
8742 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8743 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008744 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008745 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008749{
8750 int ret;
8751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008753
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008754 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8755 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008756
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008757 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8758 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008760 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008762 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008764 ssl->handshake->out_msg_seq = 1;
8765 else
8766 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008767 }
8768#endif
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8771 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008776 return( ret );
8777 }
8778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008780
8781 return( 0 );
8782}
8783
8784/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008785 * Renegotiate current connection on client,
8786 * or request renegotiation on server
8787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008791
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008792 if( ssl == NULL || ssl->conf == NULL )
8793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008796 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008797 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008803
8804 /* Did we already try/start sending HelloRequest? */
8805 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008807
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008808 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008813 /*
8814 * On client, either start the renegotiation process or,
8815 * if already in progress, continue the handshake
8816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008821
8822 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008825 return( ret );
8826 }
8827 }
8828 else
8829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008833 return( ret );
8834 }
8835 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008837
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008838 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008839}
8840
8841/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008842 * Check record counters and renegotiate if they're above the limit.
8843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008845{
Andres AG2196c7f2016-12-15 17:01:16 +00008846 size_t ep_len = ssl_ep_len( ssl );
8847 int in_ctr_cmp;
8848 int out_ctr_cmp;
8849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8851 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008852 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008853 {
8854 return( 0 );
8855 }
8856
Andres AG2196c7f2016-12-15 17:01:16 +00008857 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8858 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008859 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008860 ssl->conf->renego_period + ep_len, 8 - ep_len );
8861
8862 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008863 {
8864 return( 0 );
8865 }
8866
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008871
8872/*
8873 * Receive application data decrypted from the SSL layer
8874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008877 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008878 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008879
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008880 if( ssl == NULL || ssl->conf == NULL )
8881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008889 return( ret );
8890
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008891 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008893 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008894 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008895 return( ret );
8896 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008897 }
8898#endif
8899
Hanno Becker4a810fb2017-05-24 16:27:30 +01008900 /*
8901 * Check if renegotiation is necessary and/or handshake is
8902 * in process. If yes, perform/continue, and fall through
8903 * if an unexpected packet is received while the client
8904 * is waiting for the ServerHello.
8905 *
8906 * (There is no equivalent to the last condition on
8907 * the server-side as it is not treated as within
8908 * a handshake while waiting for the ClientHello
8909 * after a renegotiation request.)
8910 */
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008913 ret = ssl_check_ctr_renegotiate( ssl );
8914 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8915 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008918 return( ret );
8919 }
8920#endif
8921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008925 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8926 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008929 return( ret );
8930 }
8931 }
8932
Hanno Beckere41158b2017-10-23 13:30:32 +01008933 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008934 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008935 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008936 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008937 if( ssl->f_get_timer != NULL &&
8938 ssl->f_get_timer( ssl->p_timer ) == -1 )
8939 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008940 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008941 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008942
Hanno Becker327c93b2018-08-15 13:56:18 +01008943 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008944 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008945 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8946 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008947
Hanno Becker4a810fb2017-05-24 16:27:30 +01008948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8949 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008950 }
8951
8952 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008954 {
8955 /*
8956 * OpenSSL sends empty messages to randomize the IV
8957 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008958 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008961 return( 0 );
8962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008964 return( ret );
8965 }
8966 }
8967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008971
Hanno Becker4a810fb2017-05-24 16:27:30 +01008972 /*
8973 * - For client-side, expect SERVER_HELLO_REQUEST.
8974 * - For server-side, expect CLIENT_HELLO.
8975 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8976 */
8977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008981 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008984
8985 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008988 {
8989 continue;
8990 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008991#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008993 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008994#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008995
Hanno Becker4a810fb2017-05-24 16:27:30 +01008996#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009001
9002 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009004 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009005 {
9006 continue;
9007 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009010 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009011#endif /* MBEDTLS_SSL_SRV_C */
9012
Hanno Becker21df7f92017-10-17 11:03:26 +01009013#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009014 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009015 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9016 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9017 ssl->conf->allow_legacy_renegotiation ==
9018 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9019 {
9020 /*
9021 * Accept renegotiation request
9022 */
Paul Bakker48916f92012-09-16 19:57:18 +00009023
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009024 /* DTLS clients need to know renego is server-initiated */
9025#if defined(MBEDTLS_SSL_PROTO_DTLS)
9026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9027 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9028 {
9029 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9030 }
9031#endif
9032 ret = ssl_start_renegotiation( ssl );
9033 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9034 ret != 0 )
9035 {
9036 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9037 return( ret );
9038 }
9039 }
9040 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009041#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009042 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009043 /*
9044 * Refuse renegotiation
9045 */
9046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049#if defined(MBEDTLS_SSL_PROTO_SSL3)
9050 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009051 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009052 /* SSLv3 does not have a "no_renegotiation" warning, so
9053 we send a fatal alert and abort the connection. */
9054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9055 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009057 }
9058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9061 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9062 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9065 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9066 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009067 {
9068 return( ret );
9069 }
Paul Bakker48916f92012-09-16 19:57:18 +00009070 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9073 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009077 }
Paul Bakker48916f92012-09-16 19:57:18 +00009078 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009079
Hanno Becker90333da2017-10-10 11:27:13 +01009080 /* At this point, we don't know whether the renegotiation has been
9081 * completed or not. The cases to consider are the following:
9082 * 1) The renegotiation is complete. In this case, no new record
9083 * has been read yet.
9084 * 2) The renegotiation is incomplete because the client received
9085 * an application data record while awaiting the ServerHello.
9086 * 3) The renegotiation is incomplete because the client received
9087 * a non-handshake, non-application data message while awaiting
9088 * the ServerHello.
9089 * In each of these case, looping will be the proper action:
9090 * - For 1), the next iteration will read a new record and check
9091 * if it's application data.
9092 * - For 2), the loop condition isn't satisfied as application data
9093 * is present, hence continue is the same as break
9094 * - For 3), the loop condition is satisfied and read_record
9095 * will re-deliver the message that was held back by the client
9096 * when expecting the ServerHello.
9097 */
9098 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009099 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009102 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009103 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009104 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009105 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009108 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009110 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009111 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009112 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009119 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009120 }
9121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9125 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009126 }
9127
9128 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009129
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009130 /* We're going to return something now, cancel timer,
9131 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009133 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009134
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009136 /* If we requested renego but received AppData, resend HelloRequest.
9137 * Do it now, after setting in_offt, to avoid taking this branch
9138 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009140 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009142 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009143 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009146 return( ret );
9147 }
9148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009150#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009151 }
9152
9153 n = ( len < ssl->in_msglen )
9154 ? len : ssl->in_msglen;
9155
9156 memcpy( buf, ssl->in_offt, n );
9157 ssl->in_msglen -= n;
9158
9159 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009160 {
9161 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009162 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009163 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009165 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009166 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009167 /* more data available */
9168 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009172
Paul Bakker23986e52011-04-24 08:57:21 +00009173 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009174}
9175
9176/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009177 * Send application data to be encrypted by the SSL layer, taking care of max
9178 * fragment length and buffer size.
9179 *
9180 * According to RFC 5246 Section 6.2.1:
9181 *
9182 * Zero-length fragments of Application data MAY be sent as they are
9183 * potentially useful as a traffic analysis countermeasure.
9184 *
9185 * Therefore, it is possible that the input message length is 0 and the
9186 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009187 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009188static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009189 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009190{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009191 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9192 const size_t max_len = (size_t) ret;
9193
9194 if( ret < 0 )
9195 {
9196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9197 return( ret );
9198 }
9199
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009200 if( len > max_len )
9201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009206 "maximum fragment length: %d > %d",
9207 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009209 }
9210 else
9211#endif
9212 len = max_len;
9213 }
Paul Bakker887bd502011-06-08 13:10:54 +00009214
Paul Bakker5121ce52009-01-03 21:22:43 +00009215 if( ssl->out_left != 0 )
9216 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009217 /*
9218 * The user has previously tried to send the data and
9219 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9220 * written. In this case, we expect the high-level write function
9221 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009226 return( ret );
9227 }
9228 }
Paul Bakker887bd502011-06-08 13:10:54 +00009229 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009230 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009231 /*
9232 * The user is trying to send a message the first time, so we need to
9233 * copy the data into the internal buffers and setup the data structure
9234 * to keep track of partial writes
9235 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009236 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009238 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009239
Hanno Becker67bc7c32018-08-06 11:33:50 +01009240 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009243 return( ret );
9244 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009245 }
9246
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009247 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009248}
9249
9250/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009251 * Write application data, doing 1/n-1 splitting if necessary.
9252 *
9253 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009254 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009255 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009258static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009259 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009260{
9261 int ret;
9262
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009263 if( ssl->conf->cbc_record_splitting ==
9264 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009265 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9267 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9268 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009269 {
9270 return( ssl_write_real( ssl, buf, len ) );
9271 }
9272
9273 if( ssl->split_done == 0 )
9274 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009275 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009276 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009277 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009278 }
9279
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009280 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9281 return( ret );
9282 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009283
9284 return( ret + 1 );
9285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009287
9288/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009289 * Write application data (public-facing wrapper)
9290 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009291int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009292{
9293 int ret;
9294
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009296
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009297 if( ssl == NULL || ssl->conf == NULL )
9298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9299
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009300#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009301 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9302 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009303 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009304 return( ret );
9305 }
9306#endif
9307
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009308 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009309 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009310 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009311 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009313 return( ret );
9314 }
9315 }
9316
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009317#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009318 ret = ssl_write_split( ssl, buf, len );
9319#else
9320 ret = ssl_write_real( ssl, buf, len );
9321#endif
9322
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009324
9325 return( ret );
9326}
9327
9328/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009329 * Notify the peer that the connection is being closed
9330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009332{
9333 int ret;
9334
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009335 if( ssl == NULL || ssl->conf == NULL )
9336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009339
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009340 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9346 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9347 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009350 return( ret );
9351 }
9352 }
9353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009355
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009356 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009357}
9358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009360{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009361 if( transform == NULL )
9362 return;
9363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009365 deflateEnd( &transform->ctx_deflate );
9366 inflateEnd( &transform->ctx_inflate );
9367#endif
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9370 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 mbedtls_md_free( &transform->md_ctx_enc );
9373 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009374
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009375 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009376}
9377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#if defined(MBEDTLS_X509_CRT_PARSE_C)
9379static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009382
9383 while( cur != NULL )
9384 {
9385 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009387 cur = next;
9388 }
9389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009391
Hanno Becker0271f962018-08-16 13:23:47 +01009392#if defined(MBEDTLS_SSL_PROTO_DTLS)
9393
9394static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9395{
9396 unsigned offset;
9397 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9398
9399 if( hs == NULL )
9400 return;
9401
Hanno Becker283f5ef2018-08-24 09:34:47 +01009402 ssl_free_buffered_record( ssl );
9403
Hanno Becker0271f962018-08-16 13:23:47 +01009404 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009405 ssl_buffering_free_slot( ssl, offset );
9406}
9407
9408static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9409 uint8_t slot )
9410{
9411 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9412 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009413
9414 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9415 return;
9416
Hanno Beckere605b192018-08-21 15:59:07 +01009417 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009418 {
Hanno Beckere605b192018-08-21 15:59:07 +01009419 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009420 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009421 mbedtls_free( hs_buf->data );
9422 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009423 }
9424}
9425
9426#endif /* MBEDTLS_SSL_PROTO_DTLS */
9427
Gilles Peskine9b562d52018-04-25 20:32:43 +02009428void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009429{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009430 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9431
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009432 if( handshake == NULL )
9433 return;
9434
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009435#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9436 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9437 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009438 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009439 handshake->async_in_progress = 0;
9440 }
9441#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9442
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009443#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9444 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9445 mbedtls_md5_free( &handshake->fin_md5 );
9446 mbedtls_sha1_free( &handshake->fin_sha1 );
9447#endif
9448#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9449#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009450#if defined(MBEDTLS_USE_PSA_CRYPTO)
9451 psa_hash_abort( &handshake->fin_sha256_psa );
9452#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009453 mbedtls_sha256_free( &handshake->fin_sha256 );
9454#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009455#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009456#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009458 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009459#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009460 mbedtls_sha512_free( &handshake->fin_sha512 );
9461#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009462#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009463#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_DHM_C)
9466 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#if defined(MBEDTLS_ECDH_C)
9469 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009470#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009471#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009472 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009473#if defined(MBEDTLS_SSL_CLI_C)
9474 mbedtls_free( handshake->ecjpake_cache );
9475 handshake->ecjpake_cache = NULL;
9476 handshake->ecjpake_cache_len = 0;
9477#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009478#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009479
Janos Follath4ae5c292016-02-10 11:27:43 +00009480#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9481 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009482 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009484#endif
9485
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009486#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9487 if( handshake->psk != NULL )
9488 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009489 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009490 mbedtls_free( handshake->psk );
9491 }
9492#endif
9493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9495 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009496 /*
9497 * Free only the linked list wrapper, not the keys themselves
9498 * since the belong to the SNI callback
9499 */
9500 if( handshake->sni_key_cert != NULL )
9501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009503
9504 while( cur != NULL )
9505 {
9506 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009508 cur = next;
9509 }
9510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009512
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009513#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009514 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009515 if( handshake->ecrs_peer_cert != NULL )
9516 {
9517 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9518 mbedtls_free( handshake->ecrs_peer_cert );
9519 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009520#endif
9521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#if defined(MBEDTLS_SSL_PROTO_DTLS)
9523 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009524 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009525 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009526#endif
9527
Hanno Becker4a63ed42019-01-08 11:39:35 +00009528#if defined(MBEDTLS_ECDH_C) && \
9529 defined(MBEDTLS_USE_PSA_CRYPTO)
9530 psa_destroy_key( handshake->ecdh_psa_privkey );
9531#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9532
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009533 mbedtls_platform_zeroize( handshake,
9534 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009535}
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009538{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009539 if( session == NULL )
9540 return;
9541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009543 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009544#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009545
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009546#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009548#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009549
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009550 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009551}
9552
Paul Bakker5121ce52009-01-03 21:22:43 +00009553/*
9554 * Free an SSL context
9555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009557{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009558 if( ssl == NULL )
9559 return;
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009562
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009563 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009564 {
Angus Grattond8213d02016-05-25 20:56:48 +10009565 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009567 }
9568
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009569 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009570 {
Angus Grattond8213d02016-05-25 20:56:48 +10009571 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009573 }
9574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009576 if( ssl->compress_buf != NULL )
9577 {
Angus Grattond8213d02016-05-25 20:56:48 +10009578 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009580 }
9581#endif
9582
Paul Bakker48916f92012-09-16 19:57:18 +00009583 if( ssl->transform )
9584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 mbedtls_ssl_transform_free( ssl->transform );
9586 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009587 }
9588
9589 if( ssl->handshake )
9590 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009591 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9593 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 mbedtls_free( ssl->handshake );
9596 mbedtls_free( ssl->transform_negotiate );
9597 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009598 }
9599
Paul Bakkerc0463502013-02-14 11:19:38 +01009600 if( ssl->session )
9601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 mbedtls_ssl_session_free( ssl->session );
9603 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009604 }
9605
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009606#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009607 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009608 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009609 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009611 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009612#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9615 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9618 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009619 }
9620#endif
9621
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009622#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009624#endif
9625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009627
Paul Bakker86f04f42013-02-14 11:20:09 +01009628 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009629 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009630}
9631
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009632/*
9633 * Initialze mbedtls_ssl_config
9634 */
9635void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9636{
9637 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9638}
9639
Simon Butcherc97b6972015-12-27 23:48:17 +00009640#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009641static int ssl_preset_default_hashes[] = {
9642#if defined(MBEDTLS_SHA512_C)
9643 MBEDTLS_MD_SHA512,
9644 MBEDTLS_MD_SHA384,
9645#endif
9646#if defined(MBEDTLS_SHA256_C)
9647 MBEDTLS_MD_SHA256,
9648 MBEDTLS_MD_SHA224,
9649#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009650#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009651 MBEDTLS_MD_SHA1,
9652#endif
9653 MBEDTLS_MD_NONE
9654};
Simon Butcherc97b6972015-12-27 23:48:17 +00009655#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009656
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009657static int ssl_preset_suiteb_ciphersuites[] = {
9658 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9659 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9660 0
9661};
9662
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009663#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009664static int ssl_preset_suiteb_hashes[] = {
9665 MBEDTLS_MD_SHA256,
9666 MBEDTLS_MD_SHA384,
9667 MBEDTLS_MD_NONE
9668};
9669#endif
9670
9671#if defined(MBEDTLS_ECP_C)
9672static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9673 MBEDTLS_ECP_DP_SECP256R1,
9674 MBEDTLS_ECP_DP_SECP384R1,
9675 MBEDTLS_ECP_DP_NONE
9676};
9677#endif
9678
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009679/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009680 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009681 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009682int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009683 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009684{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009685#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009686 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009687#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009688
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009689 /* Use the functions here so that they are covered in tests,
9690 * but otherwise access member directly for efficiency */
9691 mbedtls_ssl_conf_endpoint( conf, endpoint );
9692 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009693
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009694 /*
9695 * Things that are common to all presets
9696 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009697#if defined(MBEDTLS_SSL_CLI_C)
9698 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9699 {
9700 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9701#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9702 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9703#endif
9704 }
9705#endif
9706
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009707#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009708 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009709#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009710
9711#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9712 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9713#endif
9714
9715#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9716 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9717#endif
9718
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009719#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9720 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9721#endif
9722
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009723#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009724 conf->f_cookie_write = ssl_cookie_write_dummy;
9725 conf->f_cookie_check = ssl_cookie_check_dummy;
9726#endif
9727
9728#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9729 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9730#endif
9731
Janos Follath088ce432017-04-10 12:42:31 +01009732#if defined(MBEDTLS_SSL_SRV_C)
9733 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9734#endif
9735
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009736#if defined(MBEDTLS_SSL_PROTO_DTLS)
9737 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9738 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9739#endif
9740
9741#if defined(MBEDTLS_SSL_RENEGOTIATION)
9742 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009743 memset( conf->renego_period, 0x00, 2 );
9744 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009745#endif
9746
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009747#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9748 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9749 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009750 const unsigned char dhm_p[] =
9751 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9752 const unsigned char dhm_g[] =
9753 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9754
Hanno Beckera90658f2017-10-04 15:29:08 +01009755 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9756 dhm_p, sizeof( dhm_p ),
9757 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009758 {
9759 return( ret );
9760 }
9761 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009762#endif
9763
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009764 /*
9765 * Preset-specific defaults
9766 */
9767 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009768 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009769 /*
9770 * NSA Suite B
9771 */
9772 case MBEDTLS_SSL_PRESET_SUITEB:
9773 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9774 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9775 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9776 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9777
9778 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9779 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9780 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9781 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9782 ssl_preset_suiteb_ciphersuites;
9783
9784#if defined(MBEDTLS_X509_CRT_PARSE_C)
9785 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009786#endif
9787
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009788#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009789 conf->sig_hashes = ssl_preset_suiteb_hashes;
9790#endif
9791
9792#if defined(MBEDTLS_ECP_C)
9793 conf->curve_list = ssl_preset_suiteb_curves;
9794#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009795 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009796
9797 /*
9798 * Default
9799 */
9800 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009801 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9802 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9803 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9804 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9805 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9806 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9807 MBEDTLS_SSL_MIN_MINOR_VERSION :
9808 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009809 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9810 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9811
9812#if defined(MBEDTLS_SSL_PROTO_DTLS)
9813 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9814 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9815#endif
9816
9817 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9818 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9819 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9820 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9821 mbedtls_ssl_list_ciphersuites();
9822
9823#if defined(MBEDTLS_X509_CRT_PARSE_C)
9824 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9825#endif
9826
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009827#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009828 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009829#endif
9830
9831#if defined(MBEDTLS_ECP_C)
9832 conf->curve_list = mbedtls_ecp_grp_id_list();
9833#endif
9834
9835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9836 conf->dhm_min_bitlen = 1024;
9837#endif
9838 }
9839
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009840 return( 0 );
9841}
9842
9843/*
9844 * Free mbedtls_ssl_config
9845 */
9846void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9847{
9848#if defined(MBEDTLS_DHM_C)
9849 mbedtls_mpi_free( &conf->dhm_P );
9850 mbedtls_mpi_free( &conf->dhm_G );
9851#endif
9852
9853#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9854 if( conf->psk != NULL )
9855 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009856 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009857 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009858 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009859 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009860 }
9861
9862 if( conf->psk_identity != NULL )
9863 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009864 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009865 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009866 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009867 conf->psk_identity_len = 0;
9868 }
9869#endif
9870
9871#if defined(MBEDTLS_X509_CRT_PARSE_C)
9872 ssl_key_cert_free( conf->key_cert );
9873#endif
9874
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009875 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009876}
9877
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009878#if defined(MBEDTLS_PK_C) && \
9879 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009880/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885#if defined(MBEDTLS_RSA_C)
9886 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9887 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889#if defined(MBEDTLS_ECDSA_C)
9890 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9891 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009894}
9895
Hanno Becker7e5437a2017-04-28 17:15:26 +01009896unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9897{
9898 switch( type ) {
9899 case MBEDTLS_PK_RSA:
9900 return( MBEDTLS_SSL_SIG_RSA );
9901 case MBEDTLS_PK_ECDSA:
9902 case MBEDTLS_PK_ECKEY:
9903 return( MBEDTLS_SSL_SIG_ECDSA );
9904 default:
9905 return( MBEDTLS_SSL_SIG_ANON );
9906 }
9907}
9908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009910{
9911 switch( sig )
9912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_RSA_C)
9914 case MBEDTLS_SSL_SIG_RSA:
9915 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#if defined(MBEDTLS_ECDSA_C)
9918 case MBEDTLS_SSL_SIG_ECDSA:
9919 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009920#endif
9921 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009923 }
9924}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009925#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009926
Hanno Becker7e5437a2017-04-28 17:15:26 +01009927#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9928 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9929
9930/* Find an entry in a signature-hash set matching a given hash algorithm. */
9931mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9932 mbedtls_pk_type_t sig_alg )
9933{
9934 switch( sig_alg )
9935 {
9936 case MBEDTLS_PK_RSA:
9937 return( set->rsa );
9938 case MBEDTLS_PK_ECDSA:
9939 return( set->ecdsa );
9940 default:
9941 return( MBEDTLS_MD_NONE );
9942 }
9943}
9944
9945/* Add a signature-hash-pair to a signature-hash set */
9946void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9947 mbedtls_pk_type_t sig_alg,
9948 mbedtls_md_type_t md_alg )
9949{
9950 switch( sig_alg )
9951 {
9952 case MBEDTLS_PK_RSA:
9953 if( set->rsa == MBEDTLS_MD_NONE )
9954 set->rsa = md_alg;
9955 break;
9956
9957 case MBEDTLS_PK_ECDSA:
9958 if( set->ecdsa == MBEDTLS_MD_NONE )
9959 set->ecdsa = md_alg;
9960 break;
9961
9962 default:
9963 break;
9964 }
9965}
9966
9967/* Allow exactly one hash algorithm for each signature. */
9968void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9969 mbedtls_md_type_t md_alg )
9970{
9971 set->rsa = md_alg;
9972 set->ecdsa = md_alg;
9973}
9974
9975#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9976 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9977
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009978/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009979 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009982{
9983 switch( hash )
9984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985#if defined(MBEDTLS_MD5_C)
9986 case MBEDTLS_SSL_HASH_MD5:
9987 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989#if defined(MBEDTLS_SHA1_C)
9990 case MBEDTLS_SSL_HASH_SHA1:
9991 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993#if defined(MBEDTLS_SHA256_C)
9994 case MBEDTLS_SSL_HASH_SHA224:
9995 return( MBEDTLS_MD_SHA224 );
9996 case MBEDTLS_SSL_HASH_SHA256:
9997 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999#if defined(MBEDTLS_SHA512_C)
10000 case MBEDTLS_SSL_HASH_SHA384:
10001 return( MBEDTLS_MD_SHA384 );
10002 case MBEDTLS_SSL_HASH_SHA512:
10003 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010004#endif
10005 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010007 }
10008}
10009
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010010/*
10011 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10012 */
10013unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10014{
10015 switch( md )
10016 {
10017#if defined(MBEDTLS_MD5_C)
10018 case MBEDTLS_MD_MD5:
10019 return( MBEDTLS_SSL_HASH_MD5 );
10020#endif
10021#if defined(MBEDTLS_SHA1_C)
10022 case MBEDTLS_MD_SHA1:
10023 return( MBEDTLS_SSL_HASH_SHA1 );
10024#endif
10025#if defined(MBEDTLS_SHA256_C)
10026 case MBEDTLS_MD_SHA224:
10027 return( MBEDTLS_SSL_HASH_SHA224 );
10028 case MBEDTLS_MD_SHA256:
10029 return( MBEDTLS_SSL_HASH_SHA256 );
10030#endif
10031#if defined(MBEDTLS_SHA512_C)
10032 case MBEDTLS_MD_SHA384:
10033 return( MBEDTLS_SSL_HASH_SHA384 );
10034 case MBEDTLS_MD_SHA512:
10035 return( MBEDTLS_SSL_HASH_SHA512 );
10036#endif
10037 default:
10038 return( MBEDTLS_SSL_HASH_NONE );
10039 }
10040}
10041
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010042#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010043/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010044 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010045 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010046 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010047int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010050
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010051 if( ssl->conf->curve_list == NULL )
10052 return( -1 );
10053
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010054 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010055 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010056 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010057
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010058 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010059}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010060#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010061
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010062#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010063/*
10064 * Check if a hash proposed by the peer is in our list.
10065 * Return 0 if we're willing to use it, -1 otherwise.
10066 */
10067int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10068 mbedtls_md_type_t md )
10069{
10070 const int *cur;
10071
10072 if( ssl->conf->sig_hashes == NULL )
10073 return( -1 );
10074
10075 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10076 if( *cur == (int) md )
10077 return( 0 );
10078
10079 return( -1 );
10080}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010081#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#if defined(MBEDTLS_X509_CRT_PARSE_C)
10084int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10085 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010086 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010087 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010088{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010089 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010091 int usage = 0;
10092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010094 const char *ext_oid;
10095 size_t ext_len;
10096#endif
10097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10099 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010100 ((void) cert);
10101 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010102 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010103#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10106 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010107 {
10108 /* Server part of the key exchange */
10109 switch( ciphersuite->key_exchange )
10110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 case MBEDTLS_KEY_EXCHANGE_RSA:
10112 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010113 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010114 break;
10115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10117 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10118 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10119 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010120 break;
10121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10123 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010124 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010125 break;
10126
10127 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 case MBEDTLS_KEY_EXCHANGE_NONE:
10129 case MBEDTLS_KEY_EXCHANGE_PSK:
10130 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10131 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010132 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010133 usage = 0;
10134 }
10135 }
10136 else
10137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10139 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010140 }
10141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010143 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010144 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010145 ret = -1;
10146 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010147#else
10148 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10152 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10155 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010156 }
10157 else
10158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10160 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010161 }
10162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010164 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010165 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010166 ret = -1;
10167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010169
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010170 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010173
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010174/*
10175 * Convert version numbers to/from wire format
10176 * and, for DTLS, to/from TLS equivalent.
10177 *
10178 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010179 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010180 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10181 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010184 unsigned char ver[2] )
10185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186#if defined(MBEDTLS_SSL_PROTO_DTLS)
10187 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010190 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10191
10192 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10193 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10194 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010195 else
10196#else
10197 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010198#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010199 {
10200 ver[0] = (unsigned char) major;
10201 ver[1] = (unsigned char) minor;
10202 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010203}
10204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010206 const unsigned char ver[2] )
10207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_SSL_PROTO_DTLS)
10209 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010210 {
10211 *major = 255 - ver[0] + 2;
10212 *minor = 255 - ver[1] + 1;
10213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010215 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10216 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010217 else
10218#else
10219 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010220#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010221 {
10222 *major = ver[0];
10223 *minor = ver[1];
10224 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010225}
10226
Simon Butcher99000142016-10-13 17:21:01 +010010227int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10228{
10229#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10230 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10231 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10232
10233 switch( md )
10234 {
10235#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10236#if defined(MBEDTLS_MD5_C)
10237 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010238 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010239#endif
10240#if defined(MBEDTLS_SHA1_C)
10241 case MBEDTLS_SSL_HASH_SHA1:
10242 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10243 break;
10244#endif
10245#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10246#if defined(MBEDTLS_SHA512_C)
10247 case MBEDTLS_SSL_HASH_SHA384:
10248 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10249 break;
10250#endif
10251#if defined(MBEDTLS_SHA256_C)
10252 case MBEDTLS_SSL_HASH_SHA256:
10253 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10254 break;
10255#endif
10256 default:
10257 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10258 }
10259
10260 return 0;
10261#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10262 (void) ssl;
10263 (void) md;
10264
10265 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10267}
10268
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10270 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10271int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10272 unsigned char *output,
10273 unsigned char *data, size_t data_len )
10274{
10275 int ret = 0;
10276 mbedtls_md5_context mbedtls_md5;
10277 mbedtls_sha1_context mbedtls_sha1;
10278
10279 mbedtls_md5_init( &mbedtls_md5 );
10280 mbedtls_sha1_init( &mbedtls_sha1 );
10281
10282 /*
10283 * digitally-signed struct {
10284 * opaque md5_hash[16];
10285 * opaque sha_hash[20];
10286 * };
10287 *
10288 * md5_hash
10289 * MD5(ClientHello.random + ServerHello.random
10290 * + ServerParams);
10291 * sha_hash
10292 * SHA(ClientHello.random + ServerHello.random
10293 * + ServerParams);
10294 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010295 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010296 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010298 goto exit;
10299 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010300 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010301 ssl->handshake->randbytes, 64 ) ) != 0 )
10302 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010304 goto exit;
10305 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010306 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010307 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010309 goto exit;
10310 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010311 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010312 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010314 goto exit;
10315 }
10316
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010317 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010318 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010320 goto exit;
10321 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010322 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010323 ssl->handshake->randbytes, 64 ) ) != 0 )
10324 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010326 goto exit;
10327 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010328 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010329 data_len ) ) != 0 )
10330 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010332 goto exit;
10333 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010334 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010335 output + 16 ) ) != 0 )
10336 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010338 goto exit;
10339 }
10340
10341exit:
10342 mbedtls_md5_free( &mbedtls_md5 );
10343 mbedtls_sha1_free( &mbedtls_sha1 );
10344
10345 if( ret != 0 )
10346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10347 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10348
10349 return( ret );
10350
10351}
10352#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10353 MBEDTLS_SSL_PROTO_TLS1_1 */
10354
10355#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10356 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010357
10358#if defined(MBEDTLS_USE_PSA_CRYPTO)
10359int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10360 unsigned char *hash, size_t *hashlen,
10361 unsigned char *data, size_t data_len,
10362 mbedtls_md_type_t md_alg )
10363{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010364 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010365 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010366 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10367
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010369
10370 if( ( status = psa_hash_setup( &hash_operation,
10371 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010372 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010373 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010374 goto exit;
10375 }
10376
Andrzej Kurek814feff2019-01-14 04:35:19 -050010377 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10378 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010379 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010380 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010381 goto exit;
10382 }
10383
Andrzej Kurek814feff2019-01-14 04:35:19 -050010384 if( ( status = psa_hash_update( &hash_operation,
10385 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010386 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010387 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010388 goto exit;
10389 }
10390
Andrzej Kurek814feff2019-01-14 04:35:19 -050010391 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10392 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010393 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010394 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010395 goto exit;
10396 }
10397
10398exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010399 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010400 {
10401 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10402 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010403 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010404 {
10405 case PSA_ERROR_NOT_SUPPORTED:
10406 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010407 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010408 case PSA_ERROR_BUFFER_TOO_SMALL:
10409 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10410 case PSA_ERROR_INSUFFICIENT_MEMORY:
10411 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10412 default:
10413 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10414 }
10415 }
10416 return( 0 );
10417}
10418
10419#else
10420
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010421int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010422 unsigned char *hash, size_t *hashlen,
10423 unsigned char *data, size_t data_len,
10424 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010425{
10426 int ret = 0;
10427 mbedtls_md_context_t ctx;
10428 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010429 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010430
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010432
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010433 mbedtls_md_init( &ctx );
10434
10435 /*
10436 * digitally-signed struct {
10437 * opaque client_random[32];
10438 * opaque server_random[32];
10439 * ServerDHParams params;
10440 * };
10441 */
10442 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10443 {
10444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10445 goto exit;
10446 }
10447 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10448 {
10449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10450 goto exit;
10451 }
10452 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10453 {
10454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10455 goto exit;
10456 }
10457 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10458 {
10459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10460 goto exit;
10461 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010462 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010463 {
10464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10465 goto exit;
10466 }
10467
10468exit:
10469 mbedtls_md_free( &ctx );
10470
10471 if( ret != 0 )
10472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10473 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10474
10475 return( ret );
10476}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010477#endif /* MBEDTLS_USE_PSA_CRYPTO */
10478
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010479#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10480 MBEDTLS_SSL_PROTO_TLS1_2 */
10481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482#endif /* MBEDTLS_SSL_TLS_C */