blob: 6a35486135c8a4a379efc409bdf8cbb0bf28564f [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 Beckerdef9bdc2019-01-30 14:46:46 +00005728static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5729 unsigned char *crt_buf,
5730 size_t crt_buf_len )
5731{
5732 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5733
5734 if( peer_crt == NULL )
5735 return( -1 );
5736
5737 if( peer_crt->raw.len != crt_buf_len )
5738 return( -1 );
5739
Hanno Becker46f34d02019-02-08 14:00:04 +00005740 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005741}
Hanno Becker84879e32019-01-31 07:44:03 +00005742#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743
Hanno Becker1294a0b2019-02-05 12:38:15 +00005744static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5745{
5746 if( session->peer_cert != NULL )
5747 {
5748 mbedtls_x509_crt_free( session->peer_cert );
5749 mbedtls_free( session->peer_cert );
5750 session->peer_cert = NULL;
5751 }
Hanno Becker9198ad12019-02-05 17:00:50 +00005752
5753#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5754 if( session->peer_cert_digest != NULL )
5755 {
5756 /* Zeroization is not necessary. */
5757 mbedtls_free( session->peer_cert_digest );
5758 session->peer_cert_digest = NULL;
5759 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5760 session->peer_cert_digest_len = 0;
5761 }
5762#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005763}
5764
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005765/*
5766 * Once the certificate message is read, parse it into a cert chain and
5767 * perform basic checks, but leave actual verification to the caller
5768 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005769static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5770 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005771{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005772 int ret;
5773#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5774 int crt_cnt=0;
5775#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005776 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005777 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005782 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5783 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 }
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5788 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 }
5795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005797
Paul Bakker5121ce52009-01-03 21:22:43 +00005798 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005800 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005801 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005802
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005803 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005807 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5808 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810 }
5811
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005812 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5813 i += 3;
5814
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005815 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 while( i < ssl->in_hslen )
5817 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005818 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005819 if ( i + 3 > ssl->in_hslen ) {
5820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005821 mbedtls_ssl_send_alert_message( ssl,
5822 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5825 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005826 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5827 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 if( ssl->in_msg[i] != 0 )
5829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005831 mbedtls_ssl_send_alert_message( ssl,
5832 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 }
5836
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005837 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5839 | (unsigned int) ssl->in_msg[i + 2];
5840 i += 3;
5841
5842 if( n < 128 || i + n > ssl->in_hslen )
5843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005845 mbedtls_ssl_send_alert_message( ssl,
5846 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 }
5850
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005851 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005852#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5853 if( crt_cnt++ == 0 &&
5854 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5855 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005856 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005857 /* During client-side renegotiation, check that the server's
5858 * end-CRTs hasn't changed compared to the initial handshake,
5859 * mitigating the triple handshake attack. On success, reuse
5860 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5862 if( ssl_check_peer_crt_unchanged( ssl,
5863 &ssl->in_msg[i],
5864 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005865 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5867 mbedtls_ssl_send_alert_message( ssl,
5868 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5869 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5870 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005871 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005872
5873 /* Now we can safely free the original chain. */
5874 ssl_clear_peer_cert( ssl->session );
5875 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005876#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5877
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005878 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005879 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005880 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 case 0: /*ok*/
5883 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5884 /* Ignore certificate with an unknown algorithm: maybe a
5885 prior certificate was already trusted. */
5886 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005887
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005888 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5889 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5890 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005891
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005892 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5893 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5894 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005895
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005896 default:
5897 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5898 crt_parse_der_failed:
5899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5900 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5901 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 }
5903
5904 i += n;
5905 }
5906
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005907 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005908 return( 0 );
5909}
5910
Hanno Becker4a55f632019-02-05 12:49:06 +00005911#if defined(MBEDTLS_SSL_SRV_C)
5912static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5913{
5914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5915 return( -1 );
5916
5917#if defined(MBEDTLS_SSL_PROTO_SSL3)
5918 /*
5919 * Check if the client sent an empty certificate
5920 */
5921 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5922 {
5923 if( ssl->in_msglen == 2 &&
5924 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5925 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5926 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5927 {
5928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5929 return( 0 );
5930 }
5931
5932 return( -1 );
5933 }
5934#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5935
5936#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5937 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5938 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5939 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5940 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5941 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5944 return( 0 );
5945 }
5946
5947 return( -1 );
5948#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5949 MBEDTLS_SSL_PROTO_TLS1_2 */
5950}
5951#endif /* MBEDTLS_SSL_SRV_C */
5952
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005953/* Check if a certificate message is expected.
5954 * Return either
5955 * - SSL_CERTIFICATE_EXPECTED, or
5956 * - SSL_CERTIFICATE_SKIP
5957 * indicating whether a Certificate message is expected or not.
5958 */
5959#define SSL_CERTIFICATE_EXPECTED 0
5960#define SSL_CERTIFICATE_SKIP 1
5961static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5962 int authmode )
5963{
5964 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5965 ssl->transform_negotiate->ciphersuite_info;
5966
5967 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5968 return( SSL_CERTIFICATE_SKIP );
5969
5970#if defined(MBEDTLS_SSL_SRV_C)
5971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5972 {
5973 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5974 return( SSL_CERTIFICATE_SKIP );
5975
5976 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5977 {
5978 /* NOTE: Is it intentional that we set verify_result
5979 * to SKIP_VERIFY on server-side only? */
5980 ssl->session_negotiate->verify_result =
5981 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5982 return( SSL_CERTIFICATE_SKIP );
5983 }
5984 }
5985#endif /* MBEDTLS_SSL_SRV_C */
5986
5987 return( SSL_CERTIFICATE_EXPECTED );
5988}
5989
Hanno Becker68636192019-02-05 14:36:34 +00005990static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5991 int authmode,
5992 mbedtls_x509_crt *chain,
5993 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005994{
Hanno Becker6bdfab22019-02-05 13:11:17 +00005995 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005996 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5997 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00005998 mbedtls_x509_crt *ca_chain;
5999 mbedtls_x509_crl *ca_crl;
6000
6001 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6002 return( 0 );
6003
6004#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6005 if( ssl->handshake->sni_ca_chain != NULL )
6006 {
6007 ca_chain = ssl->handshake->sni_ca_chain;
6008 ca_crl = ssl->handshake->sni_ca_crl;
6009 }
6010 else
6011#endif
6012 {
6013 ca_chain = ssl->conf->ca_chain;
6014 ca_crl = ssl->conf->ca_crl;
6015 }
6016
6017 /*
6018 * Main check: verify certificate
6019 */
6020 ret = mbedtls_x509_crt_verify_restartable(
6021 chain,
6022 ca_chain, ca_crl,
6023 ssl->conf->cert_profile,
6024 ssl->hostname,
6025 &ssl->session_negotiate->verify_result,
6026 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6027
6028 if( ret != 0 )
6029 {
6030 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6031 }
6032
6033#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6034 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6035 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6036#endif
6037
6038 /*
6039 * Secondary checks: always done, but change 'ret' only if it was 0
6040 */
6041
6042#if defined(MBEDTLS_ECP_C)
6043 {
6044 const mbedtls_pk_context *pk = &chain->pk;
6045
6046 /* If certificate uses an EC key, make sure the curve is OK */
6047 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6048 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6049 {
6050 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6051
6052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6053 if( ret == 0 )
6054 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6055 }
6056 }
6057#endif /* MBEDTLS_ECP_C */
6058
6059 if( mbedtls_ssl_check_cert_usage( chain,
6060 ciphersuite_info,
6061 ! ssl->conf->endpoint,
6062 &ssl->session_negotiate->verify_result ) != 0 )
6063 {
6064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6065 if( ret == 0 )
6066 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6067 }
6068
6069 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6070 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6071 * with details encoded in the verification flags. All other kinds
6072 * of error codes, including those from the user provided f_vrfy
6073 * functions, are treated as fatal and lead to a failure of
6074 * ssl_parse_certificate even if verification was optional. */
6075 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6076 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6077 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6078 {
6079 ret = 0;
6080 }
6081
6082 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6083 {
6084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6085 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6086 }
6087
6088 if( ret != 0 )
6089 {
6090 uint8_t alert;
6091
6092 /* The certificate may have been rejected for several reasons.
6093 Pick one and send the corresponding alert. Which alert to send
6094 may be a subject of debate in some cases. */
6095 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6096 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6097 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6098 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6099 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6100 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6102 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6104 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6106 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6107 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6108 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6109 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6110 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6111 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6112 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6113 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6114 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6115 else
6116 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6118 alert );
6119 }
6120
6121#if defined(MBEDTLS_DEBUG_C)
6122 if( ssl->session_negotiate->verify_result != 0 )
6123 {
6124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6125 ssl->session_negotiate->verify_result ) );
6126 }
6127 else
6128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6130 }
6131#endif /* MBEDTLS_DEBUG_C */
6132
6133 return( ret );
6134}
6135
6136int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6137{
6138 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006139 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006140#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6141 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6142 ? ssl->handshake->sni_authmode
6143 : ssl->conf->authmode;
6144#else
6145 const int authmode = ssl->conf->authmode;
6146#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006147 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006148
6149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6150
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006151 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6152 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006153 {
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006155 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006156 }
6157
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006158#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6159 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006160 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006161 {
6162 goto crt_verify;
6163 }
6164#endif
6165
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006166 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006167 {
6168 /* mbedtls_ssl_read_record may have sent an alert already. We
6169 let it decide whether to alert. */
6170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6171 return( ret );
6172 }
6173
Hanno Becker4a55f632019-02-05 12:49:06 +00006174#if defined(MBEDTLS_SSL_SRV_C)
6175 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6176 {
6177 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006178
6179 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006180 ret = 0;
6181 else
6182 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006183
Hanno Becker6bdfab22019-02-05 13:11:17 +00006184 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006185 }
6186#endif /* MBEDTLS_SSL_SRV_C */
6187
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006188 /* Clear existing peer CRT structure in case we tried to
6189 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006190 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006191 ssl->session_negotiate->peer_cert =
6192 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6193 if( ssl->session_negotiate->peer_cert == NULL )
6194 {
6195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6196 sizeof( mbedtls_x509_crt ) ) );
6197 mbedtls_ssl_send_alert_message( ssl,
6198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6199 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6200 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6201 }
6202 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Becker7a955a02019-02-05 13:08:01 +00006203
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006204 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6205 if( ret != 0 )
Hanno Beckerfcd9e712019-02-05 14:35:46 +00006206 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006207
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006208#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6209 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006210 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006211
6212crt_verify:
6213 if( ssl->handshake->ecrs_enabled)
6214 rs_ctx = &ssl->handshake->ecrs_ctx;
6215#endif
6216
Hanno Becker68636192019-02-05 14:36:34 +00006217 ret = ssl_parse_certificate_verify( ssl, authmode,
6218 ssl->session_negotiate->peer_cert,
6219 rs_ctx );
6220 if( ret != 0 )
6221 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006222
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006223#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6224 /* Remember digest of the peer's end-CRT. */
6225 ssl->session_negotiate->peer_cert_digest =
6226 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6227 if( ssl->session_negotiate->peer_cert_digest == NULL )
6228 {
6229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6230 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6231 mbedtls_ssl_send_alert_message( ssl,
6232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6233 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6235 }
6236 ret = mbedtls_md( mbedtls_md_info_from_type(
6237 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6238 ssl->session_negotiate->peer_cert->raw.p,
6239 ssl->session_negotiate->peer_cert->raw.len,
6240 ssl->session_negotiate->peer_cert_digest );
6241 if( ret != 0 )
6242 return( ret );
6243
6244 ssl->session_negotiate->peer_cert_digest_type =
6245 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6246 ssl->session_negotiate->peer_cert_digest_len =
6247 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6248#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
Hanno Becker6bdfab22019-02-05 13:11:17 +00006252exit:
6253
6254 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 return( ret );
6256}
Hanno Becker21489932019-02-05 13:20:55 +00006257#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260{
6261 int ret;
6262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 ssl->out_msglen = 1;
6267 ssl->out_msg[0] = 1;
6268
Paul Bakker5121ce52009-01-03 21:22:43 +00006269 ssl->state++;
6270
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006271 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006272 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 return( ret );
6275 }
6276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
6279 return( 0 );
6280}
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006283{
6284 int ret;
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287
Hanno Becker327c93b2018-08-15 13:56:18 +01006288 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 return( ret );
6292 }
6293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6298 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 }
6301
Hanno Beckere678eaa2018-08-21 14:57:46 +01006302 /* CCS records are only accepted if they have length 1 and content '1',
6303 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006304
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006305 /*
6306 * Switch to our negotiated transform and session parameters for inbound
6307 * data.
6308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006310 ssl->transform_in = ssl->transform_negotiate;
6311 ssl->session_in = ssl->session_negotiate;
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006317 ssl_dtls_replay_reset( ssl );
6318#endif
6319
6320 /* Increment epoch */
6321 if( ++ssl->in_epoch == 0 )
6322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006324 /* This is highly unlikely to happen for legitimate reasons, so
6325 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006327 }
6328 }
6329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006331 memset( ssl->in_ctr, 0, 8 );
6332
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006333 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6336 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6342 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006344 }
6345 }
6346#endif
6347
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 ssl->state++;
6349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006351
6352 return( 0 );
6353}
6354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6356 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006357{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006358 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6361 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6362 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006363 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006364 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6367#if defined(MBEDTLS_SHA512_C)
6368 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006369 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6370 else
6371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if defined(MBEDTLS_SHA256_C)
6373 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006374 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006375 else
6376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006380 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006381 }
Paul Bakker380da532012-04-18 16:10:25 +00006382}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6387 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006388 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6389 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6392#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006393#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006394 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006395 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6396#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006397 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006398#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006402 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006403 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006404#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006405 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006406#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006409}
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006412 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6415 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006416 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6417 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6420#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006421#if defined(MBEDTLS_USE_PSA_CRYPTO)
6422 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6423#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006424 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006425#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006428#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006429 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006430#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006431 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006432#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006435}
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6438 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6439static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006440 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006441{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006442 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6443 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006444}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006445#endif
Paul Bakker380da532012-04-18 16:10:25 +00006446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6448#if defined(MBEDTLS_SHA256_C)
6449static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006450 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006451{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006452#if defined(MBEDTLS_USE_PSA_CRYPTO)
6453 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6454#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006455 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006456#endif
Paul Bakker380da532012-04-18 16:10:25 +00006457}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006458#endif
Paul Bakker380da532012-04-18 16:10:25 +00006459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_SHA512_C)
6461static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006462 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006463{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006464#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006465 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006466#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006467 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006468#endif
Paul Bakker380da532012-04-18 16:10:25 +00006469}
Paul Bakker769075d2012-11-24 11:26:46 +01006470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006474static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006476{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006477 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006478 mbedtls_md5_context md5;
6479 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 unsigned char padbuf[48];
6482 unsigned char md5sum[16];
6483 unsigned char sha1sum[20];
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006486 if( !session )
6487 session = ssl->session;
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006491 mbedtls_md5_init( &md5 );
6492 mbedtls_sha1_init( &sha1 );
6493
6494 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6495 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
6497 /*
6498 * SSLv3:
6499 * hash =
6500 * MD5( master + pad2 +
6501 * MD5( handshake + sender + master + pad1 ) )
6502 * + SHA1( master + pad2 +
6503 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 */
6505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006507 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6508 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006512 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6513 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006514#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006517 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Paul Bakker1ef83d62012-04-11 12:09:53 +00006519 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006520
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006521 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6522 mbedtls_md5_update_ret( &md5, session->master, 48 );
6523 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6524 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006526 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6527 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6528 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6529 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006530
Paul Bakker1ef83d62012-04-11 12:09:53 +00006531 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006533 mbedtls_md5_starts_ret( &md5 );
6534 mbedtls_md5_update_ret( &md5, session->master, 48 );
6535 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6536 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6537 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006538
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006539 mbedtls_sha1_starts_ret( &sha1 );
6540 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6541 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6542 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6543 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006547 mbedtls_md5_free( &md5 );
6548 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006550 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6551 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6552 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006559static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006562 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006563 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006564 mbedtls_md5_context md5;
6565 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006566 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006569 if( !session )
6570 session = ssl->session;
6571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006573
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006574 mbedtls_md5_init( &md5 );
6575 mbedtls_sha1_init( &sha1 );
6576
6577 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6578 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579
Paul Bakker1ef83d62012-04-11 12:09:53 +00006580 /*
6581 * TLSv1:
6582 * hash = PRF( master, finished_label,
6583 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6584 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006587 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6588 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006589#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006592 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6593 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006594#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006597 ? "client finished"
6598 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006599
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006600 mbedtls_md5_finish_ret( &md5, padbuf );
6601 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006602
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006603 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006604 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006607
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006608 mbedtls_md5_free( &md5 );
6609 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006610
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006611 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6618#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006619static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006621{
6622 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006623 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006624 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006625#if defined(MBEDTLS_USE_PSA_CRYPTO)
6626 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006627 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006628 psa_status_t status;
6629#else
6630 mbedtls_sha256_context sha256;
6631#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006634 if( !session )
6635 session = ssl->session;
6636
Andrzej Kurekeb342242019-01-29 09:14:33 -05006637 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6638 ? "client finished"
6639 : "server finished";
6640
6641#if defined(MBEDTLS_USE_PSA_CRYPTO)
6642 sha256_psa = psa_hash_operation_init();
6643
6644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6645
6646 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6647 if( status != PSA_SUCCESS )
6648 {
6649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6650 return;
6651 }
6652
6653 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6654 if( status != PSA_SUCCESS )
6655 {
6656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6657 return;
6658 }
6659 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6660#else
6661
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006662 mbedtls_sha256_init( &sha256 );
6663
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006665
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006666 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006667
6668 /*
6669 * TLSv1.2:
6670 * hash = PRF( master, finished_label,
6671 * Hash( handshake ) )[0.11]
6672 */
6673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if !defined(MBEDTLS_SHA256_ALT)
6675 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006676 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006677#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006678
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006679 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006680 mbedtls_sha256_free( &sha256 );
6681#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006682
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006683 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006684 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006687
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006688 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006695static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006697{
6698 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006699 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006700 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006701#if defined(MBEDTLS_USE_PSA_CRYPTO)
6702 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006703 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006704 psa_status_t status;
6705#else
6706 mbedtls_sha512_context sha512;
6707#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006710 if( !session )
6711 session = ssl->session;
6712
Andrzej Kurekeb342242019-01-29 09:14:33 -05006713 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6714 ? "client finished"
6715 : "server finished";
6716
6717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006718 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006719
6720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6721
Andrzej Kurek972fba52019-01-30 03:29:12 -05006722 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006723 if( status != PSA_SUCCESS )
6724 {
6725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6726 return;
6727 }
6728
Andrzej Kurek972fba52019-01-30 03:29:12 -05006729 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006730 if( status != PSA_SUCCESS )
6731 {
6732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6733 return;
6734 }
6735 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6736#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006737 mbedtls_sha512_init( &sha512 );
6738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006740
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006741 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006742
6743 /*
6744 * TLSv1.2:
6745 * hash = PRF( master, finished_label,
6746 * Hash( handshake ) )[0.11]
6747 */
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006750 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6751 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006752#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006753
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006754 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006755 mbedtls_sha512_free( &sha512 );
6756#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006757
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006758 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006759 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006762
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006763 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767#endif /* MBEDTLS_SHA512_C */
6768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006773
6774 /*
6775 * Free our handshake params
6776 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006777 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006779 ssl->handshake = NULL;
6780
6781 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006782 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006783 */
6784 if( ssl->transform )
6785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 mbedtls_ssl_transform_free( ssl->transform );
6787 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006788 }
6789 ssl->transform = ssl->transform_negotiate;
6790 ssl->transform_negotiate = NULL;
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006793}
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006796{
6797 int resume = ssl->handshake->resume;
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SSL_RENEGOTIATION)
6802 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006805 ssl->renego_records_seen = 0;
6806 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006807#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006808
6809 /*
6810 * Free the previous session and switch in the current one
6811 */
Paul Bakker0a597072012-09-25 21:55:46 +00006812 if( ssl->session )
6813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006815 /* RFC 7366 3.1: keep the EtM state */
6816 ssl->session_negotiate->encrypt_then_mac =
6817 ssl->session->encrypt_then_mac;
6818#endif
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 mbedtls_ssl_session_free( ssl->session );
6821 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006822 }
6823 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006824 ssl->session_negotiate = NULL;
6825
Paul Bakker0a597072012-09-25 21:55:46 +00006826 /*
6827 * Add cache entry
6828 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006829 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006830 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006831 resume == 0 )
6832 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006833 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006835 }
Paul Bakker0a597072012-09-25 21:55:46 +00006836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006838 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006839 ssl->handshake->flight != NULL )
6840 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006841 /* Cancel handshake timer */
6842 ssl_set_timer( ssl, 0 );
6843
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006844 /* Keep last flight around in case we need to resend it:
6845 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006847 }
6848 else
6849#endif
6850 ssl_handshake_wrapup_free_hs_transform( ssl );
6851
Paul Bakker48916f92012-09-16 19:57:18 +00006852 ssl->state++;
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006855}
6856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006858{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006859 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006862
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006863 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006864
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006865 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006866
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006867 /*
6868 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6869 * may define some other value. Currently (early 2016), no defined
6870 * ciphersuite does this (and this is unlikely to change as activity has
6871 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006876 ssl->verify_data_len = hash_len;
6877 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006878#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006879
Paul Bakker5121ce52009-01-03 21:22:43 +00006880 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6882 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006883
6884 /*
6885 * In case of session resuming, invert the client and server
6886 * ChangeCipherSpec messages order.
6887 */
Paul Bakker0a597072012-09-25 21:55:46 +00006888 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006895 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006897#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006898 }
6899 else
6900 ssl->state++;
6901
Paul Bakker48916f92012-09-16 19:57:18 +00006902 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006903 * Switch to our negotiated transform and session parameters for outbound
6904 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006910 {
6911 unsigned char i;
6912
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006913 /* Remember current epoch settings for resending */
6914 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006915 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006916
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006917 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006918 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006919
6920 /* Increment epoch */
6921 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006922 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006923 break;
6924
6925 /* The loop goes to its end iff the counter is wrapping */
6926 if( i == 0 )
6927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6929 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006930 }
6931 }
6932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006934 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006936 ssl->transform_out = ssl->transform_negotiate;
6937 ssl->session_out = ssl->session_negotiate;
6938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6940 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6945 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006946 }
6947 }
6948#endif
6949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006953#endif
6954
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006955 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 return( ret );
6959 }
6960
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006961#if defined(MBEDTLS_SSL_PROTO_DTLS)
6962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6963 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6964 {
6965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6966 return( ret );
6967 }
6968#endif
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971
6972 return( 0 );
6973}
6974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006976#define SSL_MAX_HASH_LEN 36
6977#else
6978#define SSL_MAX_HASH_LEN 12
6979#endif
6980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006982{
Paul Bakker23986e52011-04-24 08:57:21 +00006983 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006984 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006985 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006989 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
Hanno Becker327c93b2018-08-15 13:56:18 +01006991 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994 return( ret );
6995 }
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7001 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 }
7004
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007005 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#if defined(MBEDTLS_SSL_PROTO_SSL3)
7007 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007008 hash_len = 36;
7009 else
7010#endif
7011 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7014 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7018 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007020 }
7021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007023 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7027 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 }
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007032 ssl->verify_data_len = hash_len;
7033 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007034#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007035
Paul Bakker0a597072012-09-25 21:55:46 +00007036 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007043 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 }
7047 else
7048 ssl->state++;
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007053#endif
7054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
7057 return( 0 );
7058}
7059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7065 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7066 mbedtls_md5_init( &handshake->fin_md5 );
7067 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007068 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7069 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7072#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007073#if defined(MBEDTLS_USE_PSA_CRYPTO)
7074 handshake->fin_sha256_psa = psa_hash_operation_init();
7075 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7076#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007078 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007079#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007083 handshake->fin_sha384_psa = psa_hash_operation_init();
7084 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007085#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007087 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007088#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007091
7092 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007093
7094#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7095 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7096 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7097#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if defined(MBEDTLS_DHM_C)
7100 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_ECDH_C)
7103 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007104#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007105#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007106 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007107#if defined(MBEDTLS_SSL_CLI_C)
7108 handshake->ecjpake_cache = NULL;
7109 handshake->ecjpake_cache_len = 0;
7110#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007111#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007112
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007113#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007114 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007115#endif
7116
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007117#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7118 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7119#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007120}
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7127 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 mbedtls_md_init( &transform->md_ctx_enc );
7130 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007131}
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007136}
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007139{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007140 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007141 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007143 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007145 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007146 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007147
7148 /*
7149 * Either the pointers are now NULL or cleared properly and can be freed.
7150 * Now allocate missing structures.
7151 */
7152 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007153 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007154 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007155 }
Paul Bakker48916f92012-09-16 19:57:18 +00007156
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007157 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007158 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007159 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007160 }
Paul Bakker48916f92012-09-16 19:57:18 +00007161
Paul Bakker82788fb2014-10-20 13:59:19 +02007162 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007163 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007164 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007165 }
Paul Bakker48916f92012-09-16 19:57:18 +00007166
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007167 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007168 if( ssl->handshake == NULL ||
7169 ssl->transform_negotiate == NULL ||
7170 ssl->session_negotiate == NULL )
7171 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 mbedtls_free( ssl->handshake );
7175 mbedtls_free( ssl->transform_negotiate );
7176 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007177
7178 ssl->handshake = NULL;
7179 ssl->transform_negotiate = NULL;
7180 ssl->session_negotiate = NULL;
7181
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007182 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007183 }
7184
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007185 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007187 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007188 ssl_handshake_params_init( ssl->handshake );
7189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7192 {
7193 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007194
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7196 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7197 else
7198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007199
7200 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007201 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007202#endif
7203
Paul Bakker48916f92012-09-16 19:57:18 +00007204 return( 0 );
7205}
7206
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007207#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007208/* Dummy cookie callbacks for defaults */
7209static int ssl_cookie_write_dummy( void *ctx,
7210 unsigned char **p, unsigned char *end,
7211 const unsigned char *cli_id, size_t cli_id_len )
7212{
7213 ((void) ctx);
7214 ((void) p);
7215 ((void) end);
7216 ((void) cli_id);
7217 ((void) cli_id_len);
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007220}
7221
7222static int ssl_cookie_check_dummy( void *ctx,
7223 const unsigned char *cookie, size_t cookie_len,
7224 const unsigned char *cli_id, size_t cli_id_len )
7225{
7226 ((void) ctx);
7227 ((void) cookie);
7228 ((void) cookie_len);
7229 ((void) cli_id);
7230 ((void) cli_id_len);
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007233}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007234#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007235
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007236/* Once ssl->out_hdr as the address of the beginning of the
7237 * next outgoing record is set, deduce the other pointers.
7238 *
7239 * Note: For TLS, we save the implicit record sequence number
7240 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7241 * and the caller has to make sure there's space for this.
7242 */
7243
7244static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7245 mbedtls_ssl_transform *transform )
7246{
7247#if defined(MBEDTLS_SSL_PROTO_DTLS)
7248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7249 {
7250 ssl->out_ctr = ssl->out_hdr + 3;
7251 ssl->out_len = ssl->out_hdr + 11;
7252 ssl->out_iv = ssl->out_hdr + 13;
7253 }
7254 else
7255#endif
7256 {
7257 ssl->out_ctr = ssl->out_hdr - 8;
7258 ssl->out_len = ssl->out_hdr + 3;
7259 ssl->out_iv = ssl->out_hdr + 5;
7260 }
7261
7262 /* Adjust out_msg to make space for explicit IV, if used. */
7263 if( transform != NULL &&
7264 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7265 {
7266 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7267 }
7268 else
7269 ssl->out_msg = ssl->out_iv;
7270}
7271
7272/* Once ssl->in_hdr as the address of the beginning of the
7273 * next incoming record is set, deduce the other pointers.
7274 *
7275 * Note: For TLS, we save the implicit record sequence number
7276 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7277 * and the caller has to make sure there's space for this.
7278 */
7279
7280static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7281 mbedtls_ssl_transform *transform )
7282{
7283#if defined(MBEDTLS_SSL_PROTO_DTLS)
7284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7285 {
7286 ssl->in_ctr = ssl->in_hdr + 3;
7287 ssl->in_len = ssl->in_hdr + 11;
7288 ssl->in_iv = ssl->in_hdr + 13;
7289 }
7290 else
7291#endif
7292 {
7293 ssl->in_ctr = ssl->in_hdr - 8;
7294 ssl->in_len = ssl->in_hdr + 3;
7295 ssl->in_iv = ssl->in_hdr + 5;
7296 }
7297
7298 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7299 if( transform != NULL &&
7300 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7301 {
7302 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7303 }
7304 else
7305 ssl->in_msg = ssl->in_iv;
7306}
7307
Paul Bakker5121ce52009-01-03 21:22:43 +00007308/*
7309 * Initialize an SSL context
7310 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007311void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7312{
7313 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7314}
7315
7316/*
7317 * Setup an SSL context
7318 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007319
7320static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7321{
7322 /* Set the incoming and outgoing record pointers. */
7323#if defined(MBEDTLS_SSL_PROTO_DTLS)
7324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7325 {
7326 ssl->out_hdr = ssl->out_buf;
7327 ssl->in_hdr = ssl->in_buf;
7328 }
7329 else
7330#endif /* MBEDTLS_SSL_PROTO_DTLS */
7331 {
7332 ssl->out_hdr = ssl->out_buf + 8;
7333 ssl->in_hdr = ssl->in_buf + 8;
7334 }
7335
7336 /* Derive other internal pointers. */
7337 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7338 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7339}
7340
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007341int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007342 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007343{
Paul Bakker48916f92012-09-16 19:57:18 +00007344 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007346 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007347
7348 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007349 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007350 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007351
7352 /* Set to NULL in case of an error condition */
7353 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007354
Angus Grattond8213d02016-05-25 20:56:48 +10007355 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7356 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 {
Angus Grattond8213d02016-05-25 20:56:48 +10007358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007359 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007360 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007361 }
7362
7363 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7364 if( ssl->out_buf == NULL )
7365 {
7366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007367 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007368 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007369 }
7370
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007371 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007372
Paul Bakker48916f92012-09-16 19:57:18 +00007373 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007374 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
7376 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007377
7378error:
7379 mbedtls_free( ssl->in_buf );
7380 mbedtls_free( ssl->out_buf );
7381
7382 ssl->conf = NULL;
7383
7384 ssl->in_buf = NULL;
7385 ssl->out_buf = NULL;
7386
7387 ssl->in_hdr = NULL;
7388 ssl->in_ctr = NULL;
7389 ssl->in_len = NULL;
7390 ssl->in_iv = NULL;
7391 ssl->in_msg = NULL;
7392
7393 ssl->out_hdr = NULL;
7394 ssl->out_ctr = NULL;
7395 ssl->out_len = NULL;
7396 ssl->out_iv = NULL;
7397 ssl->out_msg = NULL;
7398
k-stachowiak9f7798e2018-07-31 16:52:32 +02007399 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007400}
7401
7402/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007403 * Reset an initialized and used SSL context for re-use while retaining
7404 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007405 *
7406 * If partial is non-zero, keep data in the input buffer and client ID.
7407 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007408 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007409static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007410{
Paul Bakker48916f92012-09-16 19:57:18 +00007411 int ret;
7412
Hanno Becker7e772132018-08-10 12:38:21 +01007413#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7414 !defined(MBEDTLS_SSL_SRV_C)
7415 ((void) partial);
7416#endif
7417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007419
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007420 /* Cancel any possibly running timer */
7421 ssl_set_timer( ssl, 0 );
7422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if defined(MBEDTLS_SSL_RENEGOTIATION)
7424 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007425 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007426
7427 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7429 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007432
Paul Bakker7eb013f2011-10-06 12:37:39 +00007433 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007434 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007435
7436 ssl->in_msgtype = 0;
7437 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007439 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007440 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007443 ssl_dtls_replay_reset( ssl );
7444#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007445
7446 ssl->in_hslen = 0;
7447 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007448
7449 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007450
7451 ssl->out_msgtype = 0;
7452 ssl->out_msglen = 0;
7453 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7455 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007456 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007457#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007458
Hanno Becker19859472018-08-06 09:40:20 +01007459 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7460
Paul Bakker48916f92012-09-16 19:57:18 +00007461 ssl->transform_in = NULL;
7462 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007463
Hanno Becker78640902018-08-13 16:35:15 +01007464 ssl->session_in = NULL;
7465 ssl->session_out = NULL;
7466
Angus Grattond8213d02016-05-25 20:56:48 +10007467 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007468
7469#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007470 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007471#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7472 {
7473 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007474 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007475 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7478 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7481 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7484 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007485 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007486 }
7487#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007488
Paul Bakker48916f92012-09-16 19:57:18 +00007489 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 mbedtls_ssl_transform_free( ssl->transform );
7492 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007493 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007494 }
Paul Bakker48916f92012-09-16 19:57:18 +00007495
Paul Bakkerc0463502013-02-14 11:19:38 +01007496 if( ssl->session )
7497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 mbedtls_ssl_session_free( ssl->session );
7499 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007500 ssl->session = NULL;
7501 }
7502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007504 ssl->alpn_chosen = NULL;
7505#endif
7506
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007507#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007508#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007509 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007510#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007511 {
7512 mbedtls_free( ssl->cli_id );
7513 ssl->cli_id = NULL;
7514 ssl->cli_id_len = 0;
7515 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007516#endif
7517
Paul Bakker48916f92012-09-16 19:57:18 +00007518 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7519 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007520
7521 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007522}
7523
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007524/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007525 * Reset an initialized and used SSL context for re-use while retaining
7526 * all application-set variables, function pointers and data.
7527 */
7528int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7529{
7530 return( ssl_session_reset_int( ssl, 0 ) );
7531}
7532
7533/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007534 * SSL set accessors
7535 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007536void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007538 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007539}
7540
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007541void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007543 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007544}
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007547void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007550}
7551#endif
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007554void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007556 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007557}
7558#endif
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007561
Hanno Becker1841b0a2018-08-24 11:13:57 +01007562void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7563 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007564{
7565 ssl->disable_datagram_packing = !allow_packing;
7566}
7567
7568void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7569 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007571 conf->hs_timeout_min = min;
7572 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007573}
7574#endif
7575
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007576void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007577{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007578 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007579}
7580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007582void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007583 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007584 void *p_vrfy )
7585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007586 conf->f_vrfy = f_vrfy;
7587 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007590
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007591void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007592 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007593 void *p_rng )
7594{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007595 conf->f_rng = f_rng;
7596 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007597}
7598
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007599void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007600 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007601 void *p_dbg )
7602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007603 conf->f_dbg = f_dbg;
7604 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007605}
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007608 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007609 mbedtls_ssl_send_t *f_send,
7610 mbedtls_ssl_recv_t *f_recv,
7611 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007612{
7613 ssl->p_bio = p_bio;
7614 ssl->f_send = f_send;
7615 ssl->f_recv = f_recv;
7616 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007617}
7618
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007619#if defined(MBEDTLS_SSL_PROTO_DTLS)
7620void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7621{
7622 ssl->mtu = mtu;
7623}
7624#endif
7625
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007626void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007627{
7628 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007629}
7630
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007631void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7632 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007633 mbedtls_ssl_set_timer_t *f_set_timer,
7634 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007635{
7636 ssl->p_timer = p_timer;
7637 ssl->f_set_timer = f_set_timer;
7638 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007639
7640 /* Make sure we start with no timer running */
7641 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007642}
7643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007645void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007646 void *p_cache,
7647 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7648 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007649{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007650 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007651 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007652 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_CLI_C)
7657int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007658{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007659 int ret;
7660
7661 if( ssl == NULL ||
7662 session == NULL ||
7663 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007664 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007667 }
7668
Hanno Becker52055ae2019-02-06 14:30:46 +00007669 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7670 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007671 return( ret );
7672
Paul Bakker0a597072012-09-25 21:55:46 +00007673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007674
7675 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007678
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007679void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007680 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007682 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7684 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7685 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007686}
7687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007688void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007689 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007690 int major, int minor )
7691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007693 return;
7694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007696 return;
7697
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007698 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007699}
7700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007702void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007703 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007704{
7705 conf->cert_profile = profile;
7706}
7707
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007708/* Append a new keycert entry to a (possibly empty) list */
7709static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7710 mbedtls_x509_crt *cert,
7711 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007712{
niisato8ee24222018-06-25 19:05:48 +09007713 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007714
niisato8ee24222018-06-25 19:05:48 +09007715 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7716 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007717 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007718
niisato8ee24222018-06-25 19:05:48 +09007719 new_cert->cert = cert;
7720 new_cert->key = key;
7721 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007722
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007723 /* Update head is the list was null, else add to the end */
7724 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007725 {
niisato8ee24222018-06-25 19:05:48 +09007726 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007727 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007728 else
7729 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007730 mbedtls_ssl_key_cert *cur = *head;
7731 while( cur->next != NULL )
7732 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007733 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007734 }
7735
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007736 return( 0 );
7737}
7738
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007739int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007740 mbedtls_x509_crt *own_cert,
7741 mbedtls_pk_context *pk_key )
7742{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007743 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007744}
7745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007746void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007747 mbedtls_x509_crt *ca_chain,
7748 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007749{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007750 conf->ca_chain = ca_chain;
7751 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007754
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007755#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7756int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7757 mbedtls_x509_crt *own_cert,
7758 mbedtls_pk_context *pk_key )
7759{
7760 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7761 own_cert, pk_key ) );
7762}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007763
7764void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7765 mbedtls_x509_crt *ca_chain,
7766 mbedtls_x509_crl *ca_crl )
7767{
7768 ssl->handshake->sni_ca_chain = ca_chain;
7769 ssl->handshake->sni_ca_crl = ca_crl;
7770}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007771
7772void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7773 int authmode )
7774{
7775 ssl->handshake->sni_authmode = authmode;
7776}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007777#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7778
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007779#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007780/*
7781 * Set EC J-PAKE password for current handshake
7782 */
7783int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7784 const unsigned char *pw,
7785 size_t pw_len )
7786{
7787 mbedtls_ecjpake_role role;
7788
Janos Follath8eb64132016-06-03 15:40:57 +01007789 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7791
7792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7793 role = MBEDTLS_ECJPAKE_SERVER;
7794 else
7795 role = MBEDTLS_ECJPAKE_CLIENT;
7796
7797 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7798 role,
7799 MBEDTLS_MD_SHA256,
7800 MBEDTLS_ECP_DP_SECP256R1,
7801 pw, pw_len ) );
7802}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007803#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007806
7807static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7808{
7809 /* Remove reference to existing PSK, if any. */
7810#if defined(MBEDTLS_USE_PSA_CRYPTO)
7811 if( conf->psk_opaque != 0 )
7812 {
7813 /* The maintenance of the PSK key slot is the
7814 * user's responsibility. */
7815 conf->psk_opaque = 0;
7816 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007817 /* This and the following branch should never
7818 * be taken simultaenously as we maintain the
7819 * invariant that raw and opaque PSKs are never
7820 * configured simultaneously. As a safeguard,
7821 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007822#endif /* MBEDTLS_USE_PSA_CRYPTO */
7823 if( conf->psk != NULL )
7824 {
7825 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7826
7827 mbedtls_free( conf->psk );
7828 conf->psk = NULL;
7829 conf->psk_len = 0;
7830 }
7831
7832 /* Remove reference to PSK identity, if any. */
7833 if( conf->psk_identity != NULL )
7834 {
7835 mbedtls_free( conf->psk_identity );
7836 conf->psk_identity = NULL;
7837 conf->psk_identity_len = 0;
7838 }
7839}
7840
Hanno Becker7390c712018-11-15 13:33:04 +00007841/* This function assumes that PSK identity in the SSL config is unset.
7842 * It checks that the provided identity is well-formed and attempts
7843 * to make a copy of it in the SSL config.
7844 * On failure, the PSK identity in the config remains unset. */
7845static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7846 unsigned char const *psk_identity,
7847 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007848{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007849 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007850 if( psk_identity == NULL ||
7851 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007852 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007853 {
7854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7855 }
7856
Hanno Becker7390c712018-11-15 13:33:04 +00007857 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7858 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007859 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007860
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007861 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007862 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007863
7864 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007865}
7866
Hanno Becker7390c712018-11-15 13:33:04 +00007867int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7868 const unsigned char *psk, size_t psk_len,
7869 const unsigned char *psk_identity, size_t psk_identity_len )
7870{
7871 int ret;
7872 /* Remove opaque/raw PSK + PSK Identity */
7873 ssl_conf_remove_psk( conf );
7874
7875 /* Check and set raw PSK */
7876 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7878 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7879 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7880 conf->psk_len = psk_len;
7881 memcpy( conf->psk, psk, conf->psk_len );
7882
7883 /* Check and set PSK Identity */
7884 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7885 if( ret != 0 )
7886 ssl_conf_remove_psk( conf );
7887
7888 return( ret );
7889}
7890
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007891static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7892{
7893#if defined(MBEDTLS_USE_PSA_CRYPTO)
7894 if( ssl->handshake->psk_opaque != 0 )
7895 {
7896 ssl->handshake->psk_opaque = 0;
7897 }
7898 else
7899#endif /* MBEDTLS_USE_PSA_CRYPTO */
7900 if( ssl->handshake->psk != NULL )
7901 {
7902 mbedtls_platform_zeroize( ssl->handshake->psk,
7903 ssl->handshake->psk_len );
7904 mbedtls_free( ssl->handshake->psk );
7905 ssl->handshake->psk_len = 0;
7906 }
7907}
7908
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007909int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7910 const unsigned char *psk, size_t psk_len )
7911{
7912 if( psk == NULL || ssl->handshake == NULL )
7913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7914
7915 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7917
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007918 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007919
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007920 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007921 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007922
7923 ssl->handshake->psk_len = psk_len;
7924 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7925
7926 return( 0 );
7927}
7928
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007929#if defined(MBEDTLS_USE_PSA_CRYPTO)
7930int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007931 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007932 const unsigned char *psk_identity,
7933 size_t psk_identity_len )
7934{
Hanno Becker7390c712018-11-15 13:33:04 +00007935 int ret;
7936 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007937 ssl_conf_remove_psk( conf );
7938
Hanno Becker7390c712018-11-15 13:33:04 +00007939 /* Check and set opaque PSK */
7940 if( psk_slot == 0 )
7941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007942 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007943
7944 /* Check and set PSK Identity */
7945 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7946 psk_identity_len );
7947 if( ret != 0 )
7948 ssl_conf_remove_psk( conf );
7949
7950 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007951}
7952
7953int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007954 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007955{
7956 if( psk_slot == 0 || ssl->handshake == NULL )
7957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7958
7959 ssl_remove_psk( ssl );
7960 ssl->handshake->psk_opaque = psk_slot;
7961 return( 0 );
7962}
7963#endif /* MBEDTLS_USE_PSA_CRYPTO */
7964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007965void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007967 size_t),
7968 void *p_psk )
7969{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007970 conf->f_psk = f_psk;
7971 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007974
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007975#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007976
7977#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007978int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007979{
7980 int ret;
7981
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007982 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7983 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7984 {
7985 mbedtls_mpi_free( &conf->dhm_P );
7986 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007987 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007989
7990 return( 0 );
7991}
Hanno Becker470a8c42017-10-04 15:28:46 +01007992#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007993
Hanno Beckera90658f2017-10-04 15:29:08 +01007994int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7995 const unsigned char *dhm_P, size_t P_len,
7996 const unsigned char *dhm_G, size_t G_len )
7997{
7998 int ret;
7999
8000 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8001 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8002 {
8003 mbedtls_mpi_free( &conf->dhm_P );
8004 mbedtls_mpi_free( &conf->dhm_G );
8005 return( ret );
8006 }
8007
8008 return( 0 );
8009}
Paul Bakker5121ce52009-01-03 21:22:43 +00008010
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008011int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008012{
8013 int ret;
8014
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008015 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8016 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8017 {
8018 mbedtls_mpi_free( &conf->dhm_P );
8019 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008020 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008021 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008022
8023 return( 0 );
8024}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008025#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008026
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008027#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8028/*
8029 * Set the minimum length for Diffie-Hellman parameters
8030 */
8031void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8032 unsigned int bitlen )
8033{
8034 conf->dhm_min_bitlen = bitlen;
8035}
8036#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8037
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008038#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008039/*
8040 * Set allowed/preferred hashes for handshake signatures
8041 */
8042void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8043 const int *hashes )
8044{
8045 conf->sig_hashes = hashes;
8046}
Hanno Becker947194e2017-04-07 13:25:49 +01008047#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008048
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008049#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008050/*
8051 * Set the allowed elliptic curves
8052 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008053void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008054 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008056 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008057}
Hanno Becker947194e2017-04-07 13:25:49 +01008058#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008059
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008060#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008062{
Hanno Becker947194e2017-04-07 13:25:49 +01008063 /* Initialize to suppress unnecessary compiler warning */
8064 size_t hostname_len = 0;
8065
8066 /* Check if new hostname is valid before
8067 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008068 if( hostname != NULL )
8069 {
8070 hostname_len = strlen( hostname );
8071
8072 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8074 }
8075
8076 /* Now it's clear that we will overwrite the old hostname,
8077 * so we can free it safely */
8078
8079 if( ssl->hostname != NULL )
8080 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008081 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008082 mbedtls_free( ssl->hostname );
8083 }
8084
8085 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008086
Paul Bakker5121ce52009-01-03 21:22:43 +00008087 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008088 {
8089 ssl->hostname = NULL;
8090 }
8091 else
8092 {
8093 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008094 if( ssl->hostname == NULL )
8095 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008096
Hanno Becker947194e2017-04-07 13:25:49 +01008097 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008098
Hanno Becker947194e2017-04-07 13:25:49 +01008099 ssl->hostname[hostname_len] = '\0';
8100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008101
8102 return( 0 );
8103}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008104#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008105
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008106#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008107void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008109 const unsigned char *, size_t),
8110 void *p_sni )
8111{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008112 conf->f_sni = f_sni;
8113 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008118int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008119{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008120 size_t cur_len, tot_len;
8121 const char **p;
8122
8123 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008124 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8125 * MUST NOT be truncated."
8126 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008127 */
8128 tot_len = 0;
8129 for( p = protos; *p != NULL; p++ )
8130 {
8131 cur_len = strlen( *p );
8132 tot_len += cur_len;
8133
8134 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008136 }
8137
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008138 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008139
8140 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008141}
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008144{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008145 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008148
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008149void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008151 conf->max_major_ver = major;
8152 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008153}
8154
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008155void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008156{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008157 conf->min_major_ver = major;
8158 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008159}
8160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008162void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008163{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008164 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008165}
8166#endif
8167
Janos Follath088ce432017-04-10 12:42:31 +01008168#if defined(MBEDTLS_SSL_SRV_C)
8169void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8170 char cert_req_ca_list )
8171{
8172 conf->cert_req_ca_list = cert_req_ca_list;
8173}
8174#endif
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008177void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008179 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008180}
8181#endif
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008184void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008186 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008187}
8188#endif
8189
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008190#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008191void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008192{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008193 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008194}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008195#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008198int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008201 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008204 }
8205
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008206 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008207
8208 return( 0 );
8209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008213void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008214{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008215 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008220void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008221{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008222 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008223}
8224#endif
8225
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008226void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008227{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008228 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008229}
8230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008232void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008233{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008234 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008235}
8236
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008237void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008239 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008240}
8241
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008242void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008243 const unsigned char period[8] )
8244{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008245 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008250#if defined(MBEDTLS_SSL_CLI_C)
8251void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008252{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008253 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008254}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008255#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008256
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008257#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008258void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8259 mbedtls_ssl_ticket_write_t *f_ticket_write,
8260 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8261 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008262{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008263 conf->f_ticket_write = f_ticket_write;
8264 conf->f_ticket_parse = f_ticket_parse;
8265 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008266}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008269
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008270#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8271void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8272 mbedtls_ssl_export_keys_t *f_export_keys,
8273 void *p_export_keys )
8274{
8275 conf->f_export_keys = f_export_keys;
8276 conf->p_export_keys = p_export_keys;
8277}
8278#endif
8279
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008280#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008281void mbedtls_ssl_conf_async_private_cb(
8282 mbedtls_ssl_config *conf,
8283 mbedtls_ssl_async_sign_t *f_async_sign,
8284 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8285 mbedtls_ssl_async_resume_t *f_async_resume,
8286 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008287 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008288{
8289 conf->f_async_sign_start = f_async_sign;
8290 conf->f_async_decrypt_start = f_async_decrypt;
8291 conf->f_async_resume = f_async_resume;
8292 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008293 conf->p_async_config_data = async_config_data;
8294}
8295
Gilles Peskine8f97af72018-04-26 11:46:10 +02008296void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8297{
8298 return( conf->p_async_config_data );
8299}
8300
Gilles Peskine1febfef2018-04-30 11:54:39 +02008301void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008302{
8303 if( ssl->handshake == NULL )
8304 return( NULL );
8305 else
8306 return( ssl->handshake->user_async_ctx );
8307}
8308
Gilles Peskine1febfef2018-04-30 11:54:39 +02008309void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008310 void *ctx )
8311{
8312 if( ssl->handshake != NULL )
8313 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008314}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008315#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008316
Paul Bakker5121ce52009-01-03 21:22:43 +00008317/*
8318 * SSL get accessors
8319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008321{
8322 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8323}
8324
Hanno Becker8b170a02017-10-10 11:51:19 +01008325int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8326{
8327 /*
8328 * Case A: We're currently holding back
8329 * a message for further processing.
8330 */
8331
8332 if( ssl->keep_current_message == 1 )
8333 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008335 return( 1 );
8336 }
8337
8338 /*
8339 * Case B: Further records are pending in the current datagram.
8340 */
8341
8342#if defined(MBEDTLS_SSL_PROTO_DTLS)
8343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8344 ssl->in_left > ssl->next_record_offset )
8345 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008347 return( 1 );
8348 }
8349#endif /* MBEDTLS_SSL_PROTO_DTLS */
8350
8351 /*
8352 * Case C: A handshake message is being processed.
8353 */
8354
Hanno Becker8b170a02017-10-10 11:51:19 +01008355 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8356 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008358 return( 1 );
8359 }
8360
8361 /*
8362 * Case D: An application data message is being processed
8363 */
8364 if( ssl->in_offt != NULL )
8365 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008367 return( 1 );
8368 }
8369
8370 /*
8371 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008372 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008373 * we implement support for multiple alerts in single records.
8374 */
8375
8376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8377 return( 0 );
8378}
8379
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008380uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008381{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008382 if( ssl->session != NULL )
8383 return( ssl->session->verify_result );
8384
8385 if( ssl->session_negotiate != NULL )
8386 return( ssl->session_negotiate->verify_result );
8387
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008388 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008389}
8390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008392{
Paul Bakker926c8e42013-03-06 10:23:34 +01008393 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008394 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008397}
8398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008403 {
8404 switch( ssl->minor_ver )
8405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008407 return( "DTLSv1.0" );
8408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008410 return( "DTLSv1.2" );
8411
8412 default:
8413 return( "unknown (DTLS)" );
8414 }
8415 }
8416#endif
8417
Paul Bakker43ca69c2011-01-15 17:35:19 +00008418 switch( ssl->minor_ver )
8419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008421 return( "SSLv3.0" );
8422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008424 return( "TLSv1.0" );
8425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008427 return( "TLSv1.1" );
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008430 return( "TLSv1.2" );
8431
Paul Bakker43ca69c2011-01-15 17:35:19 +00008432 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008433 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008434 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008435}
8436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008438{
Hanno Becker3136ede2018-08-17 15:28:19 +01008439 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008441 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008442
Hanno Becker78640902018-08-13 16:35:15 +01008443 if( transform == NULL )
8444 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#if defined(MBEDTLS_ZLIB_SUPPORT)
8447 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8448 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008449#endif
8450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453 case MBEDTLS_MODE_GCM:
8454 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008455 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008457 transform_expansion = transform->minlen;
8458 break;
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008461
8462 block_size = mbedtls_cipher_get_block_size(
8463 &transform->cipher_ctx_enc );
8464
Hanno Becker3136ede2018-08-17 15:28:19 +01008465 /* Expansion due to the addition of the MAC. */
8466 transform_expansion += transform->maclen;
8467
8468 /* Expansion due to the addition of CBC padding;
8469 * Theoretically up to 256 bytes, but we never use
8470 * more than the block size of the underlying cipher. */
8471 transform_expansion += block_size;
8472
8473 /* For TLS 1.1 or higher, an explicit IV is added
8474 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008475#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8476 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008477 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008478#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008479
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008480 break;
8481
8482 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008485 }
8486
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008487 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008488}
8489
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008490#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8491size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8492{
8493 size_t max_len;
8494
8495 /*
8496 * Assume mfl_code is correct since it was checked when set
8497 */
Angus Grattond8213d02016-05-25 20:56:48 +10008498 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008499
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008500 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008501 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008502 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008503 {
Angus Grattond8213d02016-05-25 20:56:48 +10008504 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008505 }
8506
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008507 /* During a handshake, use the value being negotiated */
8508 if( ssl->session_negotiate != NULL &&
8509 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8510 {
8511 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8512 }
8513
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008514 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008515}
8516#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8517
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008518#if defined(MBEDTLS_SSL_PROTO_DTLS)
8519static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8520{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008521 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8522 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8523 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8524 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8525 return ( 0 );
8526
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008527 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8528 return( ssl->mtu );
8529
8530 if( ssl->mtu == 0 )
8531 return( ssl->handshake->mtu );
8532
8533 return( ssl->mtu < ssl->handshake->mtu ?
8534 ssl->mtu : ssl->handshake->mtu );
8535}
8536#endif /* MBEDTLS_SSL_PROTO_DTLS */
8537
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008538int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8539{
8540 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8541
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008542#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8543 !defined(MBEDTLS_SSL_PROTO_DTLS)
8544 (void) ssl;
8545#endif
8546
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008547#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8548 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8549
8550 if( max_len > mfl )
8551 max_len = mfl;
8552#endif
8553
8554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008555 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008556 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008557 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008558 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8559 const size_t overhead = (size_t) ret;
8560
8561 if( ret < 0 )
8562 return( ret );
8563
8564 if( mtu <= overhead )
8565 {
8566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8567 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8568 }
8569
8570 if( max_len > mtu - overhead )
8571 max_len = mtu - overhead;
8572 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008573#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008574
Hanno Becker0defedb2018-08-10 12:35:02 +01008575#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8576 !defined(MBEDTLS_SSL_PROTO_DTLS)
8577 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008578#endif
8579
8580 return( (int) max_len );
8581}
8582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583#if defined(MBEDTLS_X509_CRT_PARSE_C)
8584const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008585{
8586 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008587 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008588
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008589 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008594int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8595 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008596{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008597 if( ssl == NULL ||
8598 dst == NULL ||
8599 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008600 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008603 }
8604
Hanno Becker52055ae2019-02-06 14:30:46 +00008605 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008608
Paul Bakker5121ce52009-01-03 21:22:43 +00008609/*
Paul Bakker1961b702013-01-25 14:49:24 +01008610 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008615
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008616 if( ssl == NULL || ssl->conf == NULL )
8617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008624 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008626#endif
8627
Paul Bakker1961b702013-01-25 14:49:24 +01008628 return( ret );
8629}
8630
8631/*
8632 * Perform the SSL handshake
8633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008635{
8636 int ret = 0;
8637
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008638 if( ssl == NULL || ssl->conf == NULL )
8639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008646
8647 if( ret != 0 )
8648 break;
8649 }
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008652
8653 return( ret );
8654}
8655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#if defined(MBEDTLS_SSL_RENEGOTIATION)
8657#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008658/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008659 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008662{
8663 int ret;
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008666
8667 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8669 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008670
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008671 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008672 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008674 return( ret );
8675 }
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008678
8679 return( 0 );
8680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008682
8683/*
8684 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685 * - any side: calling mbedtls_ssl_renegotiate(),
8686 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8687 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008688 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008689 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008693{
8694 int ret;
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008697
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008698 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8699 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008700
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008701 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8702 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008704 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008706 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008708 ssl->handshake->out_msg_seq = 1;
8709 else
8710 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008711 }
8712#endif
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8715 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008720 return( ret );
8721 }
8722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008724
8725 return( 0 );
8726}
8727
8728/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008729 * Renegotiate current connection on client,
8730 * or request renegotiation on server
8731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008735
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008736 if( ssl == NULL || ssl->conf == NULL )
8737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008740 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008741 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008747
8748 /* Did we already try/start sending HelloRequest? */
8749 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008751
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008752 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008753 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008757 /*
8758 * On client, either start the renegotiation process or,
8759 * if already in progress, continue the handshake
8760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008765
8766 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008769 return( ret );
8770 }
8771 }
8772 else
8773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008777 return( ret );
8778 }
8779 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008781
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008782 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008783}
8784
8785/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008786 * Check record counters and renegotiate if they're above the limit.
8787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008789{
Andres AG2196c7f2016-12-15 17:01:16 +00008790 size_t ep_len = ssl_ep_len( ssl );
8791 int in_ctr_cmp;
8792 int out_ctr_cmp;
8793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8795 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008796 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008797 {
8798 return( 0 );
8799 }
8800
Andres AG2196c7f2016-12-15 17:01:16 +00008801 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8802 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008803 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008804 ssl->conf->renego_period + ep_len, 8 - ep_len );
8805
8806 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008807 {
8808 return( 0 );
8809 }
8810
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008815
8816/*
8817 * Receive application data decrypted from the SSL layer
8818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008820{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008821 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008822 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008823
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008824 if( ssl == NULL || ssl->conf == NULL )
8825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008830 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008833 return( ret );
8834
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008835 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008837 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008838 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008839 return( ret );
8840 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008841 }
8842#endif
8843
Hanno Becker4a810fb2017-05-24 16:27:30 +01008844 /*
8845 * Check if renegotiation is necessary and/or handshake is
8846 * in process. If yes, perform/continue, and fall through
8847 * if an unexpected packet is received while the client
8848 * is waiting for the ServerHello.
8849 *
8850 * (There is no equivalent to the last condition on
8851 * the server-side as it is not treated as within
8852 * a handshake while waiting for the ClientHello
8853 * after a renegotiation request.)
8854 */
8855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008857 ret = ssl_check_ctr_renegotiate( ssl );
8858 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8859 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008862 return( ret );
8863 }
8864#endif
8865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008869 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8870 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 return( ret );
8874 }
8875 }
8876
Hanno Beckere41158b2017-10-23 13:30:32 +01008877 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008878 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008879 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008880 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008881 if( ssl->f_get_timer != NULL &&
8882 ssl->f_get_timer( ssl->p_timer ) == -1 )
8883 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008884 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008885 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008886
Hanno Becker327c93b2018-08-15 13:56:18 +01008887 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008888 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008889 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8890 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008891
Hanno Becker4a810fb2017-05-24 16:27:30 +01008892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8893 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008894 }
8895
8896 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008898 {
8899 /*
8900 * OpenSSL sends empty messages to randomize the IV
8901 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008902 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008904 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008905 return( 0 );
8906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008908 return( ret );
8909 }
8910 }
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008915
Hanno Becker4a810fb2017-05-24 16:27:30 +01008916 /*
8917 * - For client-side, expect SERVER_HELLO_REQUEST.
8918 * - For server-side, expect CLIENT_HELLO.
8919 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8920 */
8921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008923 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008925 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008928
8929 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008931 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008932 {
8933 continue;
8934 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008937 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008938#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008939
Hanno Becker4a810fb2017-05-24 16:27:30 +01008940#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008941 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008945
8946 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008949 {
8950 continue;
8951 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008954 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008955#endif /* MBEDTLS_SSL_SRV_C */
8956
Hanno Becker21df7f92017-10-17 11:03:26 +01008957#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008958 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008959 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8960 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8961 ssl->conf->allow_legacy_renegotiation ==
8962 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8963 {
8964 /*
8965 * Accept renegotiation request
8966 */
Paul Bakker48916f92012-09-16 19:57:18 +00008967
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008968 /* DTLS clients need to know renego is server-initiated */
8969#if defined(MBEDTLS_SSL_PROTO_DTLS)
8970 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8971 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8972 {
8973 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8974 }
8975#endif
8976 ret = ssl_start_renegotiation( ssl );
8977 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8978 ret != 0 )
8979 {
8980 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8981 return( ret );
8982 }
8983 }
8984 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008985#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008986 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008987 /*
8988 * Refuse renegotiation
8989 */
8990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#if defined(MBEDTLS_SSL_PROTO_SSL3)
8994 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008995 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008996 /* SSLv3 does not have a "no_renegotiation" warning, so
8997 we send a fatal alert and abort the connection. */
8998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8999 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9000 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009001 }
9002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9004#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9005 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9006 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9009 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9010 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009011 {
9012 return( ret );
9013 }
Paul Bakker48916f92012-09-16 19:57:18 +00009014 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9017 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9020 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009021 }
Paul Bakker48916f92012-09-16 19:57:18 +00009022 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009023
Hanno Becker90333da2017-10-10 11:27:13 +01009024 /* At this point, we don't know whether the renegotiation has been
9025 * completed or not. The cases to consider are the following:
9026 * 1) The renegotiation is complete. In this case, no new record
9027 * has been read yet.
9028 * 2) The renegotiation is incomplete because the client received
9029 * an application data record while awaiting the ServerHello.
9030 * 3) The renegotiation is incomplete because the client received
9031 * a non-handshake, non-application data message while awaiting
9032 * the ServerHello.
9033 * In each of these case, looping will be the proper action:
9034 * - For 1), the next iteration will read a new record and check
9035 * if it's application data.
9036 * - For 2), the loop condition isn't satisfied as application data
9037 * is present, hence continue is the same as break
9038 * - For 3), the loop condition is satisfied and read_record
9039 * will re-deliver the message that was held back by the client
9040 * when expecting the ServerHello.
9041 */
9042 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009043 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009044#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009046 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009047 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009048 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009049 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009052 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009054 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009055 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9060 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009063 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009064 }
9065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9069 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009070 }
9071
9072 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009073
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009074 /* We're going to return something now, cancel timer,
9075 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009077 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009078
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009080 /* If we requested renego but received AppData, resend HelloRequest.
9081 * Do it now, after setting in_offt, to avoid taking this branch
9082 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009084 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009086 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009087 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009090 return( ret );
9091 }
9092 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009094#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009095 }
9096
9097 n = ( len < ssl->in_msglen )
9098 ? len : ssl->in_msglen;
9099
9100 memcpy( buf, ssl->in_offt, n );
9101 ssl->in_msglen -= n;
9102
9103 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009104 {
9105 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009106 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009107 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009109 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009110 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009111 /* more data available */
9112 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009116
Paul Bakker23986e52011-04-24 08:57:21 +00009117 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009118}
9119
9120/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009121 * Send application data to be encrypted by the SSL layer, taking care of max
9122 * fragment length and buffer size.
9123 *
9124 * According to RFC 5246 Section 6.2.1:
9125 *
9126 * Zero-length fragments of Application data MAY be sent as they are
9127 * potentially useful as a traffic analysis countermeasure.
9128 *
9129 * Therefore, it is possible that the input message length is 0 and the
9130 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009131 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009132static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009133 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009134{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009135 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9136 const size_t max_len = (size_t) ret;
9137
9138 if( ret < 0 )
9139 {
9140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9141 return( ret );
9142 }
9143
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009144 if( len > max_len )
9145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009150 "maximum fragment length: %d > %d",
9151 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009153 }
9154 else
9155#endif
9156 len = max_len;
9157 }
Paul Bakker887bd502011-06-08 13:10:54 +00009158
Paul Bakker5121ce52009-01-03 21:22:43 +00009159 if( ssl->out_left != 0 )
9160 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009161 /*
9162 * The user has previously tried to send the data and
9163 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9164 * written. In this case, we expect the high-level write function
9165 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009170 return( ret );
9171 }
9172 }
Paul Bakker887bd502011-06-08 13:10:54 +00009173 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009174 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009175 /*
9176 * The user is trying to send a message the first time, so we need to
9177 * copy the data into the internal buffers and setup the data structure
9178 * to keep track of partial writes
9179 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009180 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009182 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009183
Hanno Becker67bc7c32018-08-06 11:33:50 +01009184 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009187 return( ret );
9188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009189 }
9190
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009191 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009192}
9193
9194/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009195 * Write application data, doing 1/n-1 splitting if necessary.
9196 *
9197 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009198 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009199 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009202static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009203 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009204{
9205 int ret;
9206
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009207 if( ssl->conf->cbc_record_splitting ==
9208 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009209 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9211 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9212 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009213 {
9214 return( ssl_write_real( ssl, buf, len ) );
9215 }
9216
9217 if( ssl->split_done == 0 )
9218 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009219 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009220 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009221 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009222 }
9223
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009224 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9225 return( ret );
9226 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009227
9228 return( ret + 1 );
9229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009231
9232/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009233 * Write application data (public-facing wrapper)
9234 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009235int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009236{
9237 int ret;
9238
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009240
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009241 if( ssl == NULL || ssl->conf == NULL )
9242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9243
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009244#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009245 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9246 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009247 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009248 return( ret );
9249 }
9250#endif
9251
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009252 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009253 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009254 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009255 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009257 return( ret );
9258 }
9259 }
9260
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009261#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009262 ret = ssl_write_split( ssl, buf, len );
9263#else
9264 ret = ssl_write_real( ssl, buf, len );
9265#endif
9266
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009268
9269 return( ret );
9270}
9271
9272/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009273 * Notify the peer that the connection is being closed
9274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009276{
9277 int ret;
9278
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009279 if( ssl == NULL || ssl->conf == NULL )
9280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009283
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009284 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9290 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9291 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009294 return( ret );
9295 }
9296 }
9297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009299
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009300 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009301}
9302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009304{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009305 if( transform == NULL )
9306 return;
9307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009309 deflateEnd( &transform->ctx_deflate );
9310 inflateEnd( &transform->ctx_inflate );
9311#endif
9312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9314 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 mbedtls_md_free( &transform->md_ctx_enc );
9317 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009318
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009319 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009320}
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322#if defined(MBEDTLS_X509_CRT_PARSE_C)
9323static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009326
9327 while( cur != NULL )
9328 {
9329 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009331 cur = next;
9332 }
9333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009335
Hanno Becker0271f962018-08-16 13:23:47 +01009336#if defined(MBEDTLS_SSL_PROTO_DTLS)
9337
9338static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9339{
9340 unsigned offset;
9341 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9342
9343 if( hs == NULL )
9344 return;
9345
Hanno Becker283f5ef2018-08-24 09:34:47 +01009346 ssl_free_buffered_record( ssl );
9347
Hanno Becker0271f962018-08-16 13:23:47 +01009348 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009349 ssl_buffering_free_slot( ssl, offset );
9350}
9351
9352static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9353 uint8_t slot )
9354{
9355 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9356 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009357
9358 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9359 return;
9360
Hanno Beckere605b192018-08-21 15:59:07 +01009361 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009362 {
Hanno Beckere605b192018-08-21 15:59:07 +01009363 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009364 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009365 mbedtls_free( hs_buf->data );
9366 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009367 }
9368}
9369
9370#endif /* MBEDTLS_SSL_PROTO_DTLS */
9371
Gilles Peskine9b562d52018-04-25 20:32:43 +02009372void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009373{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009374 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9375
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009376 if( handshake == NULL )
9377 return;
9378
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009379#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9380 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9381 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009382 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009383 handshake->async_in_progress = 0;
9384 }
9385#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9386
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009387#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9388 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9389 mbedtls_md5_free( &handshake->fin_md5 );
9390 mbedtls_sha1_free( &handshake->fin_sha1 );
9391#endif
9392#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9393#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009394#if defined(MBEDTLS_USE_PSA_CRYPTO)
9395 psa_hash_abort( &handshake->fin_sha256_psa );
9396#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009397 mbedtls_sha256_free( &handshake->fin_sha256 );
9398#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009399#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009400#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009402 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009403#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009404 mbedtls_sha512_free( &handshake->fin_sha512 );
9405#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009406#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009407#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409#if defined(MBEDTLS_DHM_C)
9410 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412#if defined(MBEDTLS_ECDH_C)
9413 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009414#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009415#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009416 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009417#if defined(MBEDTLS_SSL_CLI_C)
9418 mbedtls_free( handshake->ecjpake_cache );
9419 handshake->ecjpake_cache = NULL;
9420 handshake->ecjpake_cache_len = 0;
9421#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009422#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009423
Janos Follath4ae5c292016-02-10 11:27:43 +00009424#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9425 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009426 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009428#endif
9429
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009430#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9431 if( handshake->psk != NULL )
9432 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009433 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009434 mbedtls_free( handshake->psk );
9435 }
9436#endif
9437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9439 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009440 /*
9441 * Free only the linked list wrapper, not the keys themselves
9442 * since the belong to the SNI callback
9443 */
9444 if( handshake->sni_key_cert != NULL )
9445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009447
9448 while( cur != NULL )
9449 {
9450 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009452 cur = next;
9453 }
9454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009456
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009457#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009458 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009459#endif
9460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461#if defined(MBEDTLS_SSL_PROTO_DTLS)
9462 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009463 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009464 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009465#endif
9466
Hanno Becker4a63ed42019-01-08 11:39:35 +00009467#if defined(MBEDTLS_ECDH_C) && \
9468 defined(MBEDTLS_USE_PSA_CRYPTO)
9469 psa_destroy_key( handshake->ecdh_psa_privkey );
9470#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9471
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009472 mbedtls_platform_zeroize( handshake,
9473 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009474}
9475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009477{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009478 if( session == NULL )
9479 return;
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009482 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009483#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009484
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009485#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009487#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009489 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009490}
9491
Paul Bakker5121ce52009-01-03 21:22:43 +00009492/*
9493 * Free an SSL context
9494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009496{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009497 if( ssl == NULL )
9498 return;
9499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009501
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009502 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009503 {
Angus Grattond8213d02016-05-25 20:56:48 +10009504 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009506 }
9507
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009508 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009509 {
Angus Grattond8213d02016-05-25 20:56:48 +10009510 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009512 }
9513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009515 if( ssl->compress_buf != NULL )
9516 {
Angus Grattond8213d02016-05-25 20:56:48 +10009517 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009519 }
9520#endif
9521
Paul Bakker48916f92012-09-16 19:57:18 +00009522 if( ssl->transform )
9523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 mbedtls_ssl_transform_free( ssl->transform );
9525 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009526 }
9527
9528 if( ssl->handshake )
9529 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009530 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9532 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 mbedtls_free( ssl->handshake );
9535 mbedtls_free( ssl->transform_negotiate );
9536 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009537 }
9538
Paul Bakkerc0463502013-02-14 11:19:38 +01009539 if( ssl->session )
9540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 mbedtls_ssl_session_free( ssl->session );
9542 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009543 }
9544
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009545#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009546 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009547 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009548 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009550 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9554 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9557 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009558 }
9559#endif
9560
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009561#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009563#endif
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009566
Paul Bakker86f04f42013-02-14 11:20:09 +01009567 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009568 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009569}
9570
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009571/*
9572 * Initialze mbedtls_ssl_config
9573 */
9574void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9575{
9576 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9577}
9578
Simon Butcherc97b6972015-12-27 23:48:17 +00009579#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009580static int ssl_preset_default_hashes[] = {
9581#if defined(MBEDTLS_SHA512_C)
9582 MBEDTLS_MD_SHA512,
9583 MBEDTLS_MD_SHA384,
9584#endif
9585#if defined(MBEDTLS_SHA256_C)
9586 MBEDTLS_MD_SHA256,
9587 MBEDTLS_MD_SHA224,
9588#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009589#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009590 MBEDTLS_MD_SHA1,
9591#endif
9592 MBEDTLS_MD_NONE
9593};
Simon Butcherc97b6972015-12-27 23:48:17 +00009594#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009595
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009596static int ssl_preset_suiteb_ciphersuites[] = {
9597 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9598 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9599 0
9600};
9601
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009602#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009603static int ssl_preset_suiteb_hashes[] = {
9604 MBEDTLS_MD_SHA256,
9605 MBEDTLS_MD_SHA384,
9606 MBEDTLS_MD_NONE
9607};
9608#endif
9609
9610#if defined(MBEDTLS_ECP_C)
9611static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9612 MBEDTLS_ECP_DP_SECP256R1,
9613 MBEDTLS_ECP_DP_SECP384R1,
9614 MBEDTLS_ECP_DP_NONE
9615};
9616#endif
9617
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009618/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009619 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009620 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009621int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009622 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009623{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009624#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009625 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009626#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009627
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009628 /* Use the functions here so that they are covered in tests,
9629 * but otherwise access member directly for efficiency */
9630 mbedtls_ssl_conf_endpoint( conf, endpoint );
9631 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009632
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009633 /*
9634 * Things that are common to all presets
9635 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009636#if defined(MBEDTLS_SSL_CLI_C)
9637 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9638 {
9639 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9640#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9641 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9642#endif
9643 }
9644#endif
9645
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009646#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009647 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009648#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009649
9650#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9651 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9652#endif
9653
9654#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9655 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9656#endif
9657
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009658#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9659 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9660#endif
9661
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009662#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009663 conf->f_cookie_write = ssl_cookie_write_dummy;
9664 conf->f_cookie_check = ssl_cookie_check_dummy;
9665#endif
9666
9667#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9668 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9669#endif
9670
Janos Follath088ce432017-04-10 12:42:31 +01009671#if defined(MBEDTLS_SSL_SRV_C)
9672 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9673#endif
9674
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009675#if defined(MBEDTLS_SSL_PROTO_DTLS)
9676 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9677 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9678#endif
9679
9680#if defined(MBEDTLS_SSL_RENEGOTIATION)
9681 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009682 memset( conf->renego_period, 0x00, 2 );
9683 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009684#endif
9685
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009686#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9687 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9688 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009689 const unsigned char dhm_p[] =
9690 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9691 const unsigned char dhm_g[] =
9692 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9693
Hanno Beckera90658f2017-10-04 15:29:08 +01009694 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9695 dhm_p, sizeof( dhm_p ),
9696 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009697 {
9698 return( ret );
9699 }
9700 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009701#endif
9702
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009703 /*
9704 * Preset-specific defaults
9705 */
9706 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009707 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009708 /*
9709 * NSA Suite B
9710 */
9711 case MBEDTLS_SSL_PRESET_SUITEB:
9712 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9713 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9714 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9715 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9716
9717 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9718 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9719 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9720 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9721 ssl_preset_suiteb_ciphersuites;
9722
9723#if defined(MBEDTLS_X509_CRT_PARSE_C)
9724 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009725#endif
9726
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009727#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009728 conf->sig_hashes = ssl_preset_suiteb_hashes;
9729#endif
9730
9731#if defined(MBEDTLS_ECP_C)
9732 conf->curve_list = ssl_preset_suiteb_curves;
9733#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009734 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009735
9736 /*
9737 * Default
9738 */
9739 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009740 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9741 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9742 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9743 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9744 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9745 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9746 MBEDTLS_SSL_MIN_MINOR_VERSION :
9747 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009748 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9749 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9750
9751#if defined(MBEDTLS_SSL_PROTO_DTLS)
9752 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9753 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9754#endif
9755
9756 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9757 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9758 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9759 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9760 mbedtls_ssl_list_ciphersuites();
9761
9762#if defined(MBEDTLS_X509_CRT_PARSE_C)
9763 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9764#endif
9765
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009766#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009767 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009768#endif
9769
9770#if defined(MBEDTLS_ECP_C)
9771 conf->curve_list = mbedtls_ecp_grp_id_list();
9772#endif
9773
9774#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9775 conf->dhm_min_bitlen = 1024;
9776#endif
9777 }
9778
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009779 return( 0 );
9780}
9781
9782/*
9783 * Free mbedtls_ssl_config
9784 */
9785void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9786{
9787#if defined(MBEDTLS_DHM_C)
9788 mbedtls_mpi_free( &conf->dhm_P );
9789 mbedtls_mpi_free( &conf->dhm_G );
9790#endif
9791
9792#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9793 if( conf->psk != NULL )
9794 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009795 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009796 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009797 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009798 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009799 }
9800
9801 if( conf->psk_identity != NULL )
9802 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009803 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009804 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009805 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009806 conf->psk_identity_len = 0;
9807 }
9808#endif
9809
9810#if defined(MBEDTLS_X509_CRT_PARSE_C)
9811 ssl_key_cert_free( conf->key_cert );
9812#endif
9813
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009814 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009815}
9816
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009817#if defined(MBEDTLS_PK_C) && \
9818 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009819/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824#if defined(MBEDTLS_RSA_C)
9825 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9826 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#if defined(MBEDTLS_ECDSA_C)
9829 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9830 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009833}
9834
Hanno Becker7e5437a2017-04-28 17:15:26 +01009835unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9836{
9837 switch( type ) {
9838 case MBEDTLS_PK_RSA:
9839 return( MBEDTLS_SSL_SIG_RSA );
9840 case MBEDTLS_PK_ECDSA:
9841 case MBEDTLS_PK_ECKEY:
9842 return( MBEDTLS_SSL_SIG_ECDSA );
9843 default:
9844 return( MBEDTLS_SSL_SIG_ANON );
9845 }
9846}
9847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009849{
9850 switch( sig )
9851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_RSA_C)
9853 case MBEDTLS_SSL_SIG_RSA:
9854 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856#if defined(MBEDTLS_ECDSA_C)
9857 case MBEDTLS_SSL_SIG_ECDSA:
9858 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009859#endif
9860 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009862 }
9863}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009864#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009865
Hanno Becker7e5437a2017-04-28 17:15:26 +01009866#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9867 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9868
9869/* Find an entry in a signature-hash set matching a given hash algorithm. */
9870mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9871 mbedtls_pk_type_t sig_alg )
9872{
9873 switch( sig_alg )
9874 {
9875 case MBEDTLS_PK_RSA:
9876 return( set->rsa );
9877 case MBEDTLS_PK_ECDSA:
9878 return( set->ecdsa );
9879 default:
9880 return( MBEDTLS_MD_NONE );
9881 }
9882}
9883
9884/* Add a signature-hash-pair to a signature-hash set */
9885void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9886 mbedtls_pk_type_t sig_alg,
9887 mbedtls_md_type_t md_alg )
9888{
9889 switch( sig_alg )
9890 {
9891 case MBEDTLS_PK_RSA:
9892 if( set->rsa == MBEDTLS_MD_NONE )
9893 set->rsa = md_alg;
9894 break;
9895
9896 case MBEDTLS_PK_ECDSA:
9897 if( set->ecdsa == MBEDTLS_MD_NONE )
9898 set->ecdsa = md_alg;
9899 break;
9900
9901 default:
9902 break;
9903 }
9904}
9905
9906/* Allow exactly one hash algorithm for each signature. */
9907void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9908 mbedtls_md_type_t md_alg )
9909{
9910 set->rsa = md_alg;
9911 set->ecdsa = md_alg;
9912}
9913
9914#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9915 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9916
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009917/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009918 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009921{
9922 switch( hash )
9923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924#if defined(MBEDTLS_MD5_C)
9925 case MBEDTLS_SSL_HASH_MD5:
9926 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928#if defined(MBEDTLS_SHA1_C)
9929 case MBEDTLS_SSL_HASH_SHA1:
9930 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#if defined(MBEDTLS_SHA256_C)
9933 case MBEDTLS_SSL_HASH_SHA224:
9934 return( MBEDTLS_MD_SHA224 );
9935 case MBEDTLS_SSL_HASH_SHA256:
9936 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938#if defined(MBEDTLS_SHA512_C)
9939 case MBEDTLS_SSL_HASH_SHA384:
9940 return( MBEDTLS_MD_SHA384 );
9941 case MBEDTLS_SSL_HASH_SHA512:
9942 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009943#endif
9944 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009946 }
9947}
9948
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009949/*
9950 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9951 */
9952unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9953{
9954 switch( md )
9955 {
9956#if defined(MBEDTLS_MD5_C)
9957 case MBEDTLS_MD_MD5:
9958 return( MBEDTLS_SSL_HASH_MD5 );
9959#endif
9960#if defined(MBEDTLS_SHA1_C)
9961 case MBEDTLS_MD_SHA1:
9962 return( MBEDTLS_SSL_HASH_SHA1 );
9963#endif
9964#if defined(MBEDTLS_SHA256_C)
9965 case MBEDTLS_MD_SHA224:
9966 return( MBEDTLS_SSL_HASH_SHA224 );
9967 case MBEDTLS_MD_SHA256:
9968 return( MBEDTLS_SSL_HASH_SHA256 );
9969#endif
9970#if defined(MBEDTLS_SHA512_C)
9971 case MBEDTLS_MD_SHA384:
9972 return( MBEDTLS_SSL_HASH_SHA384 );
9973 case MBEDTLS_MD_SHA512:
9974 return( MBEDTLS_SSL_HASH_SHA512 );
9975#endif
9976 default:
9977 return( MBEDTLS_SSL_HASH_NONE );
9978 }
9979}
9980
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009981#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009982/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009983 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009984 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009985 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009986int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009989
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009990 if( ssl->conf->curve_list == NULL )
9991 return( -1 );
9992
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009993 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009994 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009995 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009996
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009997 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009998}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009999#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010000
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010001#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010002/*
10003 * Check if a hash proposed by the peer is in our list.
10004 * Return 0 if we're willing to use it, -1 otherwise.
10005 */
10006int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10007 mbedtls_md_type_t md )
10008{
10009 const int *cur;
10010
10011 if( ssl->conf->sig_hashes == NULL )
10012 return( -1 );
10013
10014 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10015 if( *cur == (int) md )
10016 return( 0 );
10017
10018 return( -1 );
10019}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010020#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022#if defined(MBEDTLS_X509_CRT_PARSE_C)
10023int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10024 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010025 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010026 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010027{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010028 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010030 int usage = 0;
10031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010033 const char *ext_oid;
10034 size_t ext_len;
10035#endif
10036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10038 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010039 ((void) cert);
10040 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010041 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010042#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10045 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010046 {
10047 /* Server part of the key exchange */
10048 switch( ciphersuite->key_exchange )
10049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 case MBEDTLS_KEY_EXCHANGE_RSA:
10051 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010052 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010053 break;
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10056 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10057 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10058 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010059 break;
10060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10062 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010063 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010064 break;
10065
10066 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 case MBEDTLS_KEY_EXCHANGE_NONE:
10068 case MBEDTLS_KEY_EXCHANGE_PSK:
10069 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10070 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010071 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010072 usage = 0;
10073 }
10074 }
10075 else
10076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10078 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010079 }
10080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010082 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010083 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010084 ret = -1;
10085 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010086#else
10087 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10091 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10094 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010095 }
10096 else
10097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10099 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010100 }
10101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010103 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010104 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010105 ret = -1;
10106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010108
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010109 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010112
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010113/*
10114 * Convert version numbers to/from wire format
10115 * and, for DTLS, to/from TLS equivalent.
10116 *
10117 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010118 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010119 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10120 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010123 unsigned char ver[2] )
10124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#if defined(MBEDTLS_SSL_PROTO_DTLS)
10126 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010129 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10130
10131 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10132 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10133 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010134 else
10135#else
10136 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010137#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010138 {
10139 ver[0] = (unsigned char) major;
10140 ver[1] = (unsigned char) minor;
10141 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010142}
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010145 const unsigned char ver[2] )
10146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147#if defined(MBEDTLS_SSL_PROTO_DTLS)
10148 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010149 {
10150 *major = 255 - ver[0] + 2;
10151 *minor = 255 - ver[1] + 1;
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010154 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10155 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010156 else
10157#else
10158 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010159#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010160 {
10161 *major = ver[0];
10162 *minor = ver[1];
10163 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010164}
10165
Simon Butcher99000142016-10-13 17:21:01 +010010166int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10167{
10168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10169 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10170 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10171
10172 switch( md )
10173 {
10174#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10175#if defined(MBEDTLS_MD5_C)
10176 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010177 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010178#endif
10179#if defined(MBEDTLS_SHA1_C)
10180 case MBEDTLS_SSL_HASH_SHA1:
10181 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10182 break;
10183#endif
10184#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10185#if defined(MBEDTLS_SHA512_C)
10186 case MBEDTLS_SSL_HASH_SHA384:
10187 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10188 break;
10189#endif
10190#if defined(MBEDTLS_SHA256_C)
10191 case MBEDTLS_SSL_HASH_SHA256:
10192 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10193 break;
10194#endif
10195 default:
10196 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10197 }
10198
10199 return 0;
10200#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10201 (void) ssl;
10202 (void) md;
10203
10204 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10205#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10206}
10207
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010208#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10209 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10210int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10211 unsigned char *output,
10212 unsigned char *data, size_t data_len )
10213{
10214 int ret = 0;
10215 mbedtls_md5_context mbedtls_md5;
10216 mbedtls_sha1_context mbedtls_sha1;
10217
10218 mbedtls_md5_init( &mbedtls_md5 );
10219 mbedtls_sha1_init( &mbedtls_sha1 );
10220
10221 /*
10222 * digitally-signed struct {
10223 * opaque md5_hash[16];
10224 * opaque sha_hash[20];
10225 * };
10226 *
10227 * md5_hash
10228 * MD5(ClientHello.random + ServerHello.random
10229 * + ServerParams);
10230 * sha_hash
10231 * SHA(ClientHello.random + ServerHello.random
10232 * + ServerParams);
10233 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010234 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010235 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010237 goto exit;
10238 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010239 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010240 ssl->handshake->randbytes, 64 ) ) != 0 )
10241 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010243 goto exit;
10244 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010245 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010246 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010248 goto exit;
10249 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010250 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010251 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010253 goto exit;
10254 }
10255
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010256 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010257 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010259 goto exit;
10260 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010261 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010262 ssl->handshake->randbytes, 64 ) ) != 0 )
10263 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010265 goto exit;
10266 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010267 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010268 data_len ) ) != 0 )
10269 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010271 goto exit;
10272 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010273 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010274 output + 16 ) ) != 0 )
10275 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010277 goto exit;
10278 }
10279
10280exit:
10281 mbedtls_md5_free( &mbedtls_md5 );
10282 mbedtls_sha1_free( &mbedtls_sha1 );
10283
10284 if( ret != 0 )
10285 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10286 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10287
10288 return( ret );
10289
10290}
10291#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10292 MBEDTLS_SSL_PROTO_TLS1_1 */
10293
10294#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10295 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010296
10297#if defined(MBEDTLS_USE_PSA_CRYPTO)
10298int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10299 unsigned char *hash, size_t *hashlen,
10300 unsigned char *data, size_t data_len,
10301 mbedtls_md_type_t md_alg )
10302{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010303 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010304 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010305 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10306
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010308
10309 if( ( status = psa_hash_setup( &hash_operation,
10310 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010311 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010312 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010313 goto exit;
10314 }
10315
Andrzej Kurek814feff2019-01-14 04:35:19 -050010316 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10317 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010318 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010319 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010320 goto exit;
10321 }
10322
Andrzej Kurek814feff2019-01-14 04:35:19 -050010323 if( ( status = psa_hash_update( &hash_operation,
10324 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010325 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010326 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010327 goto exit;
10328 }
10329
Andrzej Kurek814feff2019-01-14 04:35:19 -050010330 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10331 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010332 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010333 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010334 goto exit;
10335 }
10336
10337exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010338 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010339 {
10340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10341 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010342 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010343 {
10344 case PSA_ERROR_NOT_SUPPORTED:
10345 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010346 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010347 case PSA_ERROR_BUFFER_TOO_SMALL:
10348 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10349 case PSA_ERROR_INSUFFICIENT_MEMORY:
10350 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10351 default:
10352 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10353 }
10354 }
10355 return( 0 );
10356}
10357
10358#else
10359
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010360int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010361 unsigned char *hash, size_t *hashlen,
10362 unsigned char *data, size_t data_len,
10363 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010364{
10365 int ret = 0;
10366 mbedtls_md_context_t ctx;
10367 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010368 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010369
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010371
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010372 mbedtls_md_init( &ctx );
10373
10374 /*
10375 * digitally-signed struct {
10376 * opaque client_random[32];
10377 * opaque server_random[32];
10378 * ServerDHParams params;
10379 * };
10380 */
10381 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10382 {
10383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10384 goto exit;
10385 }
10386 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10387 {
10388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10389 goto exit;
10390 }
10391 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10392 {
10393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10394 goto exit;
10395 }
10396 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10397 {
10398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10399 goto exit;
10400 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010401 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010402 {
10403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10404 goto exit;
10405 }
10406
10407exit:
10408 mbedtls_md_free( &ctx );
10409
10410 if( ret != 0 )
10411 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10412 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10413
10414 return( ret );
10415}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010416#endif /* MBEDTLS_USE_PSA_CRYPTO */
10417
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010418#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10419 MBEDTLS_SSL_PROTO_TLS1_2 */
10420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421#endif /* MBEDTLS_SSL_TLS_C */